Sha256: 172aba564fc4e40e70fdcf22d20ff214ecaa3323635a9e405f9f4b3bc884f6c7

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

  module Disposable
  class Twin
    # Twin that uses a hash to populate.
    #
    #   Twin.new(id: 1)
    module Struct
      def setup_properties!(options={})
        hash_representer.new(self).from_hash(@model.merge(options))
      end

      def hash_representer
        Class.new(schema) do
          include Representable::Hash
          include Representable::Hash::AllowSymbols

          representable_attrs.each do |dfn|
            dfn.merge!(
              prepare:       lambda { |model, *| model },
              instance:      lambda { |model, *| model }, # FIXME: this is because Representable thinks this is typed? in Deserializer.
              representable: false,
            ) if dfn[:twin]
          end
        end
      end

      def sync_hash_representer
        hash_representer.clone.tap do |rpr|
          rpr.representable_attrs.each do |dfn|
            dfn.merge!(
              serialize: lambda { |model, *| model.sync! },
              representable: true
            ) if dfn[:twin]
          end
        end
      end

      def sync(options={})
        sync_hash_representer.new(self).to_hash
      end
      alias_method :sync!, :sync
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
disposable-0.1.9 lib/disposable/twin/struct.rb
disposable-0.1.8 lib/disposable/twin/struct.rb
disposable-0.1.7 lib/disposable/twin/struct.rb