Sha256: 7e31d7914a0e925a38746069aa241bcb083b5961697888a38c0eb1862ddefa5e

Contents?: true

Size: 986 Bytes

Versions: 3

Compression:

Stored size: 986 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.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

3 entries across 3 versions & 1 rubygems

Version Path
mongoo-0.5.2 lib/mongoo/attributes/describe_dsl.rb
mongoo-0.5.1 lib/mongoo/attributes/describe_dsl.rb
mongoo-0.5.0 lib/mongoo/attributes/describe_dsl.rb