Module: MemoryModel::Base::Attributes
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Dirty
- Included in:
- MemoryModel::Base
- Defined in:
- lib/memory_model/base/attributes.rb
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(hash) ⇒ Object
- #has_attribute?(key) ⇒ Boolean
- #inspect ⇒ Object
- #read_attribute(name) ⇒ Object (also: #[])
- #write_attribute(name, value) ⇒ Object (also: #[]=)
Instance Method Details
#attributes ⇒ Object
18 19 20 |
# File 'lib/memory_model/base/attributes.rb', line 18 def attributes @attributes ||= HashWithIndifferentAccess.new end |
#attributes=(hash) ⇒ Object
22 23 24 25 26 |
# File 'lib/memory_model/base/attributes.rb', line 22 def attributes=(hash) hash.reduce({}) do |hash, (attr, value)| hash.merge attr => write_attribute(attr, value) end end |
#has_attribute?(key) ⇒ Boolean
28 29 30 31 32 33 34 35 |
# File 'lib/memory_model/base/attributes.rb', line 28 def has_attribute?(key) case value = @attributes[key] when NilClass, String !value.nil? else value.present? end end |
#inspect ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/memory_model/base/attributes.rb', line 37 def inspect inspection = fields.reduce([]) { |array, field| name = field.name array << "#{name}: #{attribute_for_inspect(name)}" if has_attribute?(name) array }.join(", ") inspection = ' ' + inspection if inspection.present? super.sub /^(#<[a-z:0-9]+).*>/i, "\\1#{inspection}>" end |
#read_attribute(name) ⇒ Object Also known as: []
47 48 49 |
# File 'lib/memory_model/base/attributes.rb', line 47 def read_attribute(name) attributes[name] end |
#write_attribute(name, value) ⇒ Object Also known as: []=
53 54 55 56 57 58 59 |
# File 'lib/memory_model/base/attributes.rb', line 53 def write_attribute(name, value) raise InvalidFieldError, name unless fields.include? name raise ReadOnlyFieldError, name if fields[name].[:readonly] && persisted? send "#{name}_will_change!" unless read_attribute(name) == value || new_record? attributes[name] = value end |