Class: ActiveData::Model::Attributes::Base
- Inherits:
-
Object
- Object
- ActiveData::Model::Attributes::Base
- Defined in:
- lib/active_data/model/attributes/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
Instance Method Summary collapse
-
#initialize(name, owner) ⇒ Base
constructor
A new instance of Base.
- #inspect_attribute ⇒ Object
- #pollute ⇒ Object
- #query ⇒ Object
- #read ⇒ Object
- #read_before_type_cast ⇒ Object
- #readonly? ⇒ Boolean
- #reflection ⇒ Object
- #reset ⇒ Object
- #typecast(value) ⇒ Object
- #value_present? ⇒ Boolean
- #write(value) ⇒ Object
- #write_value(value) ⇒ Object
Constructor Details
#initialize(name, owner) ⇒ Base
Returns a new instance of Base.
8 9 10 11 |
# File 'lib/active_data/model/attributes/base.rb', line 8 def initialize(name, owner) @name = name @owner = owner end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/active_data/model/attributes/base.rb', line 5 def name @name end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
5 6 7 |
# File 'lib/active_data/model/attributes/base.rb', line 5 def owner @owner end |
Instance Method Details
#inspect_attribute ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_data/model/attributes/base.rb', line 59 def inspect_attribute value = case read when Date, Time, DateTime %("#{read.to_s(:db)}") else inspection = read.inspect inspection.size > 100 ? inspection.truncate(50) : inspection end "#{name}: #{value}" end |
#pollute ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/active_data/model/attributes/base.rb', line 70 def pollute pollute = owner.class.dirty? && !owner.send(:attribute_changed?, name) if pollute previous_value = read result = yield if previous_value != read || ( read.respond_to?(:changed?) && read.changed? ) owner.send(:set_attribute_was, name, previous_value) end result else yield end end |
#query ⇒ Object
43 44 45 |
# File 'lib/active_data/model/attributes/base.rb', line 43 def query !(read.respond_to?(:zero?) ? read.zero? : read.blank?) end |
#read ⇒ Object
31 32 33 |
# File 'lib/active_data/model/attributes/base.rb', line 31 def read @value_cache end |
#read_before_type_cast ⇒ Object
35 36 37 |
# File 'lib/active_data/model/attributes/base.rb', line 35 def read_before_type_cast @value_cache end |
#readonly? ⇒ Boolean
55 56 57 |
# File 'lib/active_data/model/attributes/base.rb', line 55 def readonly? !!(readonly.is_a?(Proc) ? evaluate(&readonly) : readonly) end |
#reflection ⇒ Object
13 14 15 |
# File 'lib/active_data/model/attributes/base.rb', line 13 def reflection @owner.class._attributes[name] end |
#reset ⇒ Object
27 28 29 |
# File 'lib/active_data/model/attributes/base.rb', line 27 def reset remove_variable(:value, :value_before_type_cast) end |
#typecast(value) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/active_data/model/attributes/base.rb', line 47 def typecast(value) if value.instance_of?(type) value else typecaster.call(value, self) unless value.nil? end end |
#value_present? ⇒ Boolean
39 40 41 |
# File 'lib/active_data/model/attributes/base.rb', line 39 def value_present? !read.nil? && !(read.respond_to?(:empty?) && read.empty?) end |
#write(value) ⇒ Object
22 23 24 25 |
# File 'lib/active_data/model/attributes/base.rb', line 22 def write(value) return if readonly? write_value value end |
#write_value(value) ⇒ Object
17 18 19 20 |
# File 'lib/active_data/model/attributes/base.rb', line 17 def write_value(value) reset @value_cache = value end |