Module: Superstore::AttributeMethods

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
Included in:
Base
Defined in:
lib/superstore.rb,
lib/superstore/attribute_methods.rb,
lib/superstore/attribute_methods/dirty.rb,
lib/superstore/attribute_methods/definition.rb,
lib/superstore/attribute_methods/primary_key.rb,
lib/superstore/attribute_methods/typecasting.rb

Defined Under Namespace

Modules: ClassMethods, Dirty, PrimaryKey, Typecasting Classes: Definition

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object



80
81
82
83
84
85
# File 'lib/superstore/attribute_methods.rb', line 80

def method_missing(method_id, *args, &block)
  self.class.define_attribute_methods unless self.class.attribute_methods_generated?

  match = match_attribute_method?(method_id.to_s)
  match ? attribute_missing(match, *args, &block) : super
end

Instance Method Details

#attribute_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/superstore/attribute_methods.rb', line 62

def attribute_exists?(name)
  @attributes.key?(name.to_s)
end

#attribute_present?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/superstore/attribute_methods.rb', line 57

def attribute_present?(attribute)
  value = _read_attribute(attribute)
  !value.nil? && !(value.respond_to?(:empty?) && value.empty?)
end

#attributesObject



66
67
68
69
70
71
72
# File 'lib/superstore/attribute_methods.rb', line 66

def attributes
  results = {}
  @attributes.each_key do |key|
    results[key] = read_attribute(key)
  end
  results
end

#attributes=(attributes) ⇒ Object



74
75
76
77
78
# File 'lib/superstore/attribute_methods.rb', line 74

def attributes=(attributes)
  attributes.each do |(name, value)|
    send("#{name}=", value)
  end
end

#read_attribute(name) ⇒ Object Also known as: _read_attribute



46
47
48
49
50
51
52
53
54
# File 'lib/superstore/attribute_methods.rb', line 46

def read_attribute(name)
  name = name.to_s unless name.is_a?(String)

  if name == self.class.primary_key
    send(name)
  else
    @attributes[name]
  end
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/superstore/attribute_methods.rb', line 87

def respond_to?(*args)
  self.class.define_attribute_methods unless self.class.attribute_methods_generated?
  super
end

#write_attribute(name, value) ⇒ Object



42
43
44
# File 'lib/superstore/attribute_methods.rb', line 42

def write_attribute(name, value)
  @attributes[name.to_s] = self.class.typecast_attribute(name, value)
end