Sha256: e0aa0779e31248bb8aa721c9333e9ac04adefbbc035a8d8c9d535968b85bb979

Contents?: true

Size: 755 Bytes

Versions: 5

Compression:

Stored size: 755 Bytes

Contents

  module Disposable
  class Twin
    # Twin that uses a hash to populate.
    #
    #   Twin.new(id: 1)
    module Struct
      def read_value_for(dfn, options)
        name = dfn.name
        @model[name.to_s] || @model[name.to_sym] # TODO: test sym vs. str.
      end

      def sync_hash_representer # TODO: make this without representable, please.
        Sync.hash_representer(self.class) do |dfn|
          dfn.merge!(
            prepare:       lambda { |model, *| model },
            serialize: lambda { |model, *| model.sync! },
            representable: true
          ) if dfn[:twin]
        end
      end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
disposable-0.1.15 lib/disposable/twin/struct.rb
disposable-0.1.14 lib/disposable/twin/struct.rb
disposable-0.1.13 lib/disposable/twin/struct.rb
disposable-0.1.12 lib/disposable/twin/struct.rb
disposable-0.1.11 lib/disposable/twin/struct.rb