Module: SimpleModel::Attributes
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Dirty, ExtendCore
- Included in:
- Base
- Defined in:
- lib/simple_model/attributes.rb,
lib/simple_model/attributes/default_value_helpers.rb
Defined Under Namespace
Modules: ClassMethods, DefaultValueHelpers
Constant Summary
collapse
- DEFAULT_ATTRIBUTE_SETTINGS =
{:attributes_method => :attributes,
:allow_blank => false
}.freeze
- AVAILABLE_ATTRIBUTE_METHODS =
{
:has_attribute => {:alias => :has_attributes, :options => {:allow_blank => true}},
:has_boolean => {:cast_to => :to_b, :alias => :has_booleans, :options => {:allow_blank => true, :boolean => true}},
:has_date => {:cast_to => :to_date, :alias => :has_dates} ,
:has_decimal => {:cast_to => :to_d, :alias => :has_decimals},
:has_float => {:cast_to => :to_f, :alias => :has_floats},
:has_int => {:cast_to => :to_i, :alias => :has_ints},
:has_time => {:cast_to => :to_time, :alias => :has_times}
}.freeze
Instance Method Summary
collapse
Instance Method Details
#attribute(attr) ⇒ Object
Rails 3.2 + required when searching for attributes in from inherited classes/models
118
119
120
|
# File 'lib/simple_model/attributes.rb', line 118
def attribute(attr)
get_attribute(attr)
end
|
#attribute_defined?(attr) ⇒ Boolean
113
114
115
|
# File 'lib/simple_model/attributes.rb', line 113
def attribute_defined?(attr)
self.class.attribute_defined?(attr)
end
|
#attributes ⇒ Object
31
32
33
|
# File 'lib/simple_model/attributes.rb', line 31
def attributes
@attributes ||= new_attribute_store
end
|
#attributes=(attrs) ⇒ Object
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/simple_model/attributes.rb', line 35
def attributes=attrs
if attrs.class.name == "Hash"
if config.attributes_store == :symbol
attrs.symbolize_keys!
else
attrs.stringify_keys!
end
end
@attributes = attrs
end
|
#delete_attributes(*attrs) ⇒ Object
Also known as:
delete_attribute
130
131
132
133
134
135
|
# File 'lib/simple_model/attributes.rb', line 130
def delete_attributes(*attrs)
clear_attribute_changes(attrs) if respond_to?(:clear_attribute_changes,true) attrs.each do |attr|
attributes.delete(to_attr_key(attr))
end
end
|
#fetch_attribute_options(attr) ⇒ Object
99
100
101
|
# File 'lib/simple_model/attributes.rb', line 99
def fetch_attribute_options(attr)
self.class.defined_attributes[attr] || {}
end
|
#get(attr) ⇒ Object
Also known as:
read
51
52
53
|
# File 'lib/simple_model/attributes.rb', line 51
def get(attr)
send(attr)
end
|
#get_attribute(attr, opts = nil) ⇒ Object
TODO: Returning just val in Rails 3 HashWithIndifferentAccess causes weird issue where if value is an array the array is reset, revert to return to just val when Rails 3 support is dropped to improve performance
91
92
93
94
95
96
97
|
# File 'lib/simple_model/attributes.rb', line 91
def get_attribute(attr,opts=nil)
opts ||= fetch_attribute_options(attr)
attr_key = to_attr_key(attr)
val = attributes[attr_key]
attributes[attr_key] = fetch_default_value(opts[:default]) unless skip_get_default?(attr,opts,val)
val = attributes[attr_key]
end
|
#get_attribute?(attr) ⇒ Boolean
103
104
105
106
107
108
109
110
111
|
# File 'lib/simple_model/attributes.rb', line 103
def get_attribute?(attr)
return false unless val = get_attribute(attr)
if val.respond_to?(:blank?)
return !val.blank?
elsif val.respond_to?(:to_b)
return val.to_b
end
!val.nil?
end
|
#initialize(attrs = {}) ⇒ Object
22
23
24
25
26
27
28
29
|
# File 'lib/simple_model/attributes.rb', line 22
def initialize(attrs={})
attrs ||= {}
attrs = self.class.before_initialize.call(self,attrs) if self.class.before_initialize
set(attrs)
defaults = default_attributes_for_init
set(defaults)
self.class.after_initialize.call(self) if self.class.after_initialize
end
|
#initialized?(attr) ⇒ Boolean
Returns true if attribute has been initialized
47
48
49
|
# File 'lib/simple_model/attributes.rb', line 47
def initialized?(attr)
attributes.has_key?(to_attr_key(attr))
end
|
#process_on_set(on_set, val) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/simple_model/attributes.rb', line 81
def process_on_set(on_set,val)
if on_set.is_a?(Symbol)
val.__send__(on_set)
else
on_set.call(self,val)
end
end
|
#raw_attribute(attr) ⇒ Object
122
123
124
|
# File 'lib/simple_model/attributes.rb', line 122
def raw_attribute(attr)
attributes[to_attr_key(attr)]
end
|
#reset_attributes ⇒ Object
138
139
140
|
# File 'lib/simple_model/attributes.rb', line 138
def reset_attributes
@attributes = new_attribute_store
end
|
#set(*attrs) ⇒ Object
Also known as:
set_attributes
Accepts a hash where the keys are methods and the values are values to be set. set(:foo => “bar”, :dime => 0.1)
58
59
60
61
62
|
# File 'lib/simple_model/attributes.rb', line 58
def set(*attrs)
attrs..each do |attr,val|
send("#{attr}=",val)
end
end
|
#set_attribute(attr, val, opts = nil) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/simple_model/attributes.rb', line 65
def set_attribute(attr,val,opts=nil)
opts ||= fetch_attribute_options(attr)
attr_key = to_attr_key(attr)
if allow_attribute_action?(val,opts)
ab = opts[:allow_blank]
val = fetch_default_value(opts[:default]) unless skip_set_default?(attr,opts,val,ab)
unless (opts[:boolean] ? (!ab && val.blank? && (val != false)) : (!ab && val.blank?))
val = process_on_set(opts[:on_set],val) if opts.has_key?(:on_set)
send("#{attr}_will_change!") if initialized?(attr) && val != attributes[attr_key]
attributes[attr_key] = val
end
val
end
end
|
#set_raw_attribute(attr, val) ⇒ Object
126
127
128
|
# File 'lib/simple_model/attributes.rb', line 126
def set_raw_attribute(attr,val)
attributes[to_attr_key(attr)] = val
end
|