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 |
# File 'lib/active_data/model/attributes/base.rb', line 8 def initialize name, owner @name, @owner = name, 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
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_data/model/attributes/base.rb', line 58 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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_data/model/attributes/base.rb', line 69 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
42 43 44 |
# File 'lib/active_data/model/attributes/base.rb', line 42 def query !(read.respond_to?(:zero?) ? read.zero? : read.blank?) end |
#read ⇒ Object
30 31 32 |
# File 'lib/active_data/model/attributes/base.rb', line 30 def read @value_cache end |
#read_before_type_cast ⇒ Object
34 35 36 |
# File 'lib/active_data/model/attributes/base.rb', line 34 def read_before_type_cast @value_cache end |
#readonly? ⇒ Boolean
54 55 56 |
# File 'lib/active_data/model/attributes/base.rb', line 54 def readonly? !!(readonly.is_a?(Proc) ? evaluate(&readonly) : readonly) end |
#reflection ⇒ Object
12 13 14 |
# File 'lib/active_data/model/attributes/base.rb', line 12 def reflection @owner.class._attributes[name] end |
#reset ⇒ Object
26 27 28 |
# File 'lib/active_data/model/attributes/base.rb', line 26 def reset remove_variable(:value, :value_before_type_cast) end |
#typecast(value) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/active_data/model/attributes/base.rb', line 46 def typecast value if value.instance_of?(type) value else typecaster.call(value, self) unless value.nil? end end |
#value_present? ⇒ Boolean
38 39 40 |
# File 'lib/active_data/model/attributes/base.rb', line 38 def value_present? !read.nil? && !(read.respond_to?(:empty?) && read.empty?) end |
#write(value) ⇒ Object
21 22 23 24 |
# File 'lib/active_data/model/attributes/base.rb', line 21 def write value return if readonly? write_value value end |
#write_value(value) ⇒ Object
16 17 18 19 |
# File 'lib/active_data/model/attributes/base.rb', line 16 def write_value value reset @value_cache = value end |