Sha256: 29595181f8af9d08319e680f1ada9d9cb269fc38077b481b5dc94138afc605fe

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Id
  module Model
    attr_reader :data

    def initialize(data = {})
      @data = Hashifier.hashify(data)
    end

    def set(values)
      self.class.new(data.merge(Hashifier.hashify(values)))
    end

    def unset(*keys)
      self.class.new(data.except(*keys.map(&:to_s)))
    end

    private

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

    def memoize(f, &b)
      instance_variable_get("@#{f}") || instance_variable_set("@#{f}", b.call)
    end

    module Descriptor

      def field(f, options={})
        (options[:optional] ? FieldOption : Field).new(self, f, options).define
      end

      def has_one(f, options={})
        (options[:optional] ? HasOneOption : HasOne).new(self, f, options).define
      end

      def has_many(f, options={})
        HasMany.new(self, f, options).define
      end

      def compound_field(f, fields, options={})
        CompoundField.new(self, f, fields, options).define
      end

      def builder
        builder_class.new(self)
      end

      private

      def builder_class
        @builder_class ||= Class.new { include Builder }
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
id-0.0.8 lib/id/model.rb