Sha256: 772f8d824f6153a22ebb73d5c6a9e5ec773fc6763dfaae34c68a91c6feb3251e

Contents?: true

Size: 1008 Bytes

Versions: 5

Compression:

Stored size: 1008 Bytes

Contents

module Mongoo
  module Attributes
    module DescribeDsl
      def attribute(name, opts={})
        raise ArgumentError.new("missing :type") unless opts[:type]
        @klass.attributes[name.to_s] = opts
        true
      end

      def define_attribute_methods
        @klass.attributes_tree(only_definable: true).each do |name, val|
          if val.is_a?(Hash)
            blk = Proc.new { Mongoo::AttributeProxy.new(val, [name], self) }
            @klass.send(:define_method, name, &blk)
          else
            blk = Proc.new { get(name) }
            @klass.send(:define_method, name, &blk)
            blk = Proc.new { |val| set(name, val) }
            @klass.send(:define_method, "#{name}=", &blk)
          end
        end
      end # define_attribute_methods
      protected :define_attribute_methods
    end # DescribeDsl
  end # Attributes
end # Mongoo

class Mongoo::DescribeDsl
  include Mongoo::Attributes::DescribeDsl
end

Mongoo::DescribeDsl.after_describe << :define_attribute_methods

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoo-0.5.7 lib/mongoo/attributes/describe_dsl.rb
mongoo-0.5.6 lib/mongoo/attributes/describe_dsl.rb
mongoo-0.5.5 lib/mongoo/attributes/describe_dsl.rb
mongoo-0.5.4 lib/mongoo/attributes/describe_dsl.rb
mongoo-0.5.3 lib/mongoo/attributes/describe_dsl.rb