Module: Disposable::Twin::Sync::ToNestedHash::ClassMethods

Defined in:
lib/disposable/twin/sync.rb

Instance Method Summary collapse

Instance Method Details

#nested_hash_representerObject

Create a hash representer on-the-fly to serialize the form to a hash.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/disposable/twin/sync.rb', line 60

def nested_hash_representer
  @nested_hash_representer ||= Class.new(representer_class) do
    include Representable::Hash

    representable_attrs.each do |dfn|
      dfn.merge!(
        readable: true, # the nested hash contains all fields.
        as:       dfn[:private_name] # nested hash keys by model property names.
      )

      dfn.merge!(
        prepare:       lambda { |model, *| model }, # TODO: why do we need that here?
        serialize:     lambda { |form, args| form.to_nested_hash },
      ) if dfn[:twin]

      self
    end
  end
end