Module: MemoryModel::Base::Attributable
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Dirty
- Included in:
- MemoryModel::Base
- Defined in:
- lib/memory_model/base/attributable.rb
Instance Method Summary collapse
- #has_attribute?(key) ⇒ Boolean
- #inspect ⇒ Object
- #read_attribute(name) ⇒ Object (also: #[])
- #write_attribute(name, value) ⇒ Object (also: #[]=)
Instance Method Details
#has_attribute?(key) ⇒ Boolean
13 14 15 16 17 18 19 20 |
# File 'lib/memory_model/base/attributable.rb', line 13 def has_attribute?(key) case value = @attributes[key] when NilClass, String !value.nil? else value.present? end end |
#inspect ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/memory_model/base/attributable.rb', line 22 def inspect inspection = if @attributes fields.reduce([]) { |array, name| array << "#{name}: #{attribute_for_inspect(name)}" if has_attribute?(name) array }.join(", ") else "not initialized" end "#<#{[self.class, inspection].join(' ')}>" end |
#read_attribute(name) ⇒ Object Also known as: []
34 35 36 |
# File 'lib/memory_model/base/attributable.rb', line 34 def read_attribute(name) @attributes[name] end |
#write_attribute(name, value) ⇒ Object Also known as: []=
40 41 42 43 44 45 46 47 48 |
# File 'lib/memory_model/base/attributable.rb', line 40 def write_attribute(name, value) raise MemoryModel::InvalidFieldError, "#{name} is not a valid field" unless fields.include? name raise MemoryModel::FieldReadOnlyError, "#{name} is read only" if fields[name].[:readonly] send "#{name}_will_change!" unless value == read_attribute(name) || new_record? @attributes[name] = value end |