module Attrio::ClassMethods

Public Instance Methods

attrio() click to toggle source
# File lib/attrio.rb, line 24
def attrio
  @attrio ||= {}
end
const_missing(name) click to toggle source
Calls superclass method
# File lib/attrio.rb, line 65
def const_missing(name)
  Attrio::AttributesParser.cast_type(name) || super
end
define_attributes(options = {}, &block) click to toggle source
# 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