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



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/superstore/attribute_methods.rb', line 85

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

  match = if ActiveRecord.version >= Gem::Version.new('5.0')
    # Active Record 5.0
    matched_attribute_method(method_id.to_s)
  else
    # Active Record 4.2
    match_attribute_method?(method_id.to_s)
  end
  match ? attribute_missing(match, *args, &block) : super
end

Instance Method Details

#attribute_present?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#attributesObject



71
72
73
74
75
76
77
# File 'lib/superstore/attribute_methods.rb', line 71

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

#attributes=(attributes) ⇒ Object



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

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

#has_attribute?(name) ⇒ Boolean Also known as: attribute_exists?

Returns:

  • (Boolean)


66
67
68
# File 'lib/superstore/attribute_methods.rb', line 66

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

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



50
51
52
53
54
55
56
57
58
# File 'lib/superstore/attribute_methods.rb', line 50

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)


98
99
100
101
# File 'lib/superstore/attribute_methods.rb', line 98

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

#write_attribute(name, value) ⇒ Object



46
47
48
# File 'lib/superstore/attribute_methods.rb', line 46

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