Module: SimpleModel::Attributes
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::AttributeMethods, ExtendCore
- Included in:
- Base
- Defined in:
- lib/simple_model/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
Instance Method Summary collapse
- #get(attr) ⇒ Object (also: #read)
- #get_attribute(attr) ⇒ Object
- #get_attribute?(attr) ⇒ Boolean
- #initialize(*attrs) ⇒ Object
-
#initialized?(attr) ⇒ Boolean
Returns true if attribute has been initialized.
-
#set(*attrs) ⇒ Object
(also: #set_attributes)
Accepts a hash where the keys are methods and the values are values to be set.
- #set_attribute(attr, val) ⇒ Object
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/simple_model/attributes.rb', line 6 def attributes @attributes end |
Instance Method Details
#get(attr) ⇒ Object Also known as: read
25 26 27 |
# File 'lib/simple_model/attributes.rb', line 25 def get(attr) send(attr) end |
#get_attribute(attr) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/simple_model/attributes.rb', line 50 def get_attribute(attr) val = attributes[attr] = self.class.defined_attributes[attr] || {} if (.key?(:default) && (!initialized?(attr) || (![:allow_blank] && val.blank?))) val = attributes[attr] = fetch_default_value([:default]) end if [:on_get] [:on_get].call(self,val) else val end end |
#get_attribute?(attr) ⇒ Boolean
63 64 65 66 67 68 69 70 71 |
# File 'lib/simple_model/attributes.rb', line 63 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
8 9 10 11 12 13 14 |
# File 'lib/simple_model/attributes.rb', line 8 def initialize(*attrs) attrs = attrs. attrs = attributes_for_init(attrs) attrs = self.class.before_initialize.call(self,attrs) if self.class.before_initialize set(attrs) self.class.after_initialize.call(self) if self.class.after_initialize end |
#initialized?(attr) ⇒ Boolean
Returns true if attribute has been initialized
21 22 23 |
# File 'lib/simple_model/attributes.rb', line 21 def initialized?(attr) attributes.key?(attr) 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)
32 33 34 35 36 |
# File 'lib/simple_model/attributes.rb', line 32 def set(*attrs) attrs..each do |attr,val| send("#{attr}=",val) end end |
#set_attribute(attr, val) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/simple_model/attributes.rb', line 39 def set_attribute(attr,val) = self.class.defined_attributes[attr] || {} if allow_attribute_action?(val,) val = fetch_default_value([:default]) if (![:allow_blank] && .key?(:default) && val.blank?) val = [:on_set].call(self,val) if [:on_set] #(!options.key?(:on_set) || (val.blank? && !options[:allow_blank]) ) send("#{attr}_will_change!") if (initialized?(attr) && val != attributes[attr]) attributes[attr] = val [:after_set].call(self,val) if [:after_set] end end |