Sha256: 3aacbc5ef966de4856d0299d15b8003f1b2167cb8c09617a74c863558ee6b8cf

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Disposable
  class Twin
    # Read all properties at twin initialization time from model.
    # Simply pass through all properties from the model to the respective twin writer method.
    # This will result in all twin properties/collection items being twinned, and collections
    # being Collection to expose the desired public API.
    module Setup
      # test is in incoming hash? is nil on incoming model?

      def initialize(model, options={})
        @fields = {}
        @model  = model
        @mapper = mapper_for(model) # mapper for model.

        setup_properties!(model, options)
      end

    private
      def mapper_for(model)
        model
      end

      def setup_properties!(model, options)
        schema.each do |dfn|
          next if dfn[:readable] == false

          name  = dfn.name
          value = options[name.to_sym] || mapper.send(name) # model.title.

          setup_write!(dfn, value)
        end

        merge_options!(options) # FIXME: call writer!!!!!!!!!!
        # from_hash(options) # assigns known properties from options.
      end

      def setup_write!(dfn, value)
        send(dfn.setter, value)
      end

      def merge_options!(options)
        # TODO: ask charliesome if that is faster with return unless options.size.
        options.each { |k, v| @fields[k.to_s] = v } # TODO: this merges every option, not only defined ones.
      end


      # Including this will _not_ use the property's setter in Setup and allow you to override it.
      module SkipSetter
        def setup_write!(dfn, value)
          write_property(dfn.name, value, dfn)
        end
      end
    end # Setup
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
disposable-0.1.5 lib/disposable/twin/setup.rb