Sha256: 06f8137d66d85ef2a57f06e708bd195e24d75fa6d68924ede0f311615ee9dd9f

Contents?: true

Size: 770 Bytes

Versions: 3

Compression:

Stored size: 770 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 define_setter(f)
          define_method f do |value|
            set(f, value)
          end
        end

      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
id-0.0.9 lib/id/model/builder.rb
id-0.0.8 lib/id/model/builder.rb
id-0.0.7 lib/id/model/builder.rb