Module: Attrio::ClassMethods

Defined in:
lib/attrio.rb

Instance Method Summary collapse

Instance Method Details

#attrioObject



24
25
26
# File 'lib/attrio.rb', line 24

def attrio
  @attrio ||= {}
end

#const_missing(name) ⇒ Object



65
66
67
# File 'lib/attrio.rb', line 65

def const_missing(name)
  Attrio::AttributesParser.cast_type(name) || super
end

#define_attributes(options = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/attrio.rb', line 28

def define_attributes(options = {}, &block)
  as = options.delete(:as) || :attributes
  self.attrio[as] = options

  class_eval(<<-EOS, __FILE__, __LINE__ + 1)
    @#{as} ||= {}

    class << self
      def #{as}(attributes = [])
        attributes = Helpers.to_a(attributes).flatten
        return @#{as} if attributes.empty?

        attributes = @#{as}.keys & attributes
        @#{as}.select{ |k,v| attributes.include?(k) }
      end

      def inherited(subclass)
        subclass.instance_variable_set("@#{as}", instance_variable_get("@#{as}").dup)
      end
    end

    def #{as}(attributes = [])
      # self.class.#{as}(attributes)

      attributes = Helpers.to_a(attributes).flatten
      return @#{as} if attributes.empty?

      attributes = @#{as}.keys & attributes
      @#{as}.select{ |k,v| attributes.include?(k) }
    end
  EOS

  self.define_attrio_reset(as)

  Attrio::AttributesParser.new(self, as, &block)
end