Sha256: 21415485ec13b7e94de1d92d744247537ed040b628228fc326b5ce4caa77dbff

Contents?: true

Size: 762 Bytes

Versions: 5

Compression:

Stored size: 762 Bytes

Contents

module Id
  module Model
    module Builder

      def initialize(model, data={})
        @model = model
        @data = data
      end

      def build
        model.new data
      end

      private

      def set(f, value)
        self.class.new(model, data.merge(f.to_s => ensure_hash(value)))
      end

      def ensure_hash(value)
        case value
        when Id::Model then value.data
        when Array then value.map { |v| ensure_hash(v) }
        else value end
      end

      attr_reader :model, :data

      def self.included(base)
        base.extend(FieldBuilder)
      end

      module FieldBuilder

        def field(f)
          define_method f do |value|
            set(f, value)
          end
        end

      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
id-0.0.6 lib/id/model/builder.rb
id-0.0.5 lib/id/model/builder.rb
id-0.0.4 lib/id/model/builder.rb
id-0.0.3 lib/id/model/builder.rb
id-0.0.2 lib/id/model/builder.rb