Представляемая жемчужина: проблемы с обработкой вложенных документов (хэшей)
Я использую гем "Представимый" и следую инструкциям в файле readme для доступа к свойствам во вложенном документе.
Представитель и тест:
# app/representers/nhl/game_representer.rb
require 'representable/hash'
module Nhl::GameRepresenter
include Representable::Hash
include Representable::Hash::AllowSymbols
property :stats_id, as: :eventId
nested :venue do
property :venue_id, as: :venueId
end
end
# test/models/nhl/game_test.rb
class Nhl::GameTest < ActiveSupport::TestCase
context 'initializing w/Representer' do
should 'map attributes correctly' do
real_hash = {
:eventId => 1553247,
venue: {
:venueId=>28,
:name=>"Air Canada Centre"
}
}
game = Nhl::Game.new.extend(Nhl::GameRepresenter).from_hash(real_hash)
assert_equal 1553247, game.stats_id
assert_equal 28, game.venue_id
end
end
end
Здесь первое утверждение проходит, а второе (место проведения) не выполняется. Кажется, что мой nested
блок просто ничего не делает, потому что game.venue_id
в конечном итоге nil
,
Оглядываясь назад, я понял, что там написано:
Msgstr "Обратите внимание, что:: вложенный внутри реализован с использованием Decorator."
... и пример кода использует Class SongRepresenter < Representable::Decorator
, Поэтому я переписал мой представитель так:
class Nhl::GameRepresenter < Representable::Decorator # <--- change to class
include Representable::Hash
include Representable::Hash::AllowSymbols
property :stats_id, as: :eventId
nested :venue do
property :venue_id, as: :venueId
end
end
В моем тесте я затем настроил game
объект как это:
game = Nhl::GameRepresenter.new(Nhl::Game.new).from_hash(real_hash)
Тем не менее, я все еще получаю ту же проблему: game.venue # => nil
Может ли кто-нибудь с большим опытом использования этого драгоценного камня указать, что я делаю неправильно?