Sha256: 3d300f5ab44ac1c1d9b7b7dea0797bf74eee5f7919ba787c7af77a7579b6d9b7

Contents?: true

Size: 859 Bytes

Versions: 6

Compression:

Stored size: 859 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 { |options| options[:input] },
            serialize: lambda { |options| options[:input].sync! },
            representable: true
          ) if dfn[:nested]
        end
      end

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

      # So far, hashes can't be persisted separately.
      def save!
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
disposable-0.3.1 lib/disposable/twin/struct.rb
disposable-0.3.0 lib/disposable/twin/struct.rb
disposable-0.2.6 lib/disposable/twin/struct.rb
disposable-0.2.5 lib/disposable/twin/struct.rb
disposable-0.2.4 lib/disposable/twin/struct.rb
disposable-0.2.3 lib/disposable/twin/struct.rb