Class: Plyushkin::Property
- Inherits:
-
Object
- Object
- Plyushkin::Property
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/plyushkin/property.rb
Constant Summary collapse
- DEFAULT_CALLBACK =
lambda{}
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #all(opts = {}) ⇒ Object
- #create(attr = {}) ⇒ Object
- #dirty? ⇒ Boolean
- #empty? ⇒ Boolean
-
#initialize(name, opts = {}) ⇒ Property
constructor
A new instance of Property.
- #insert_position(datetime) ⇒ Object
- #last ⇒ Object
- #mark_persisted ⇒ Object
- #nil? ⇒ Boolean
- #trigger_callback(sym) ⇒ Object
- #value_hashes ⇒ Object
- #value_type ⇒ Object
Constructor Details
#initialize(name, opts = {}) ⇒ Property
Returns a new instance of Property.
14 15 16 17 18 19 20 21 |
# File 'lib/plyushkin/property.rb', line 14 def initialize(name, opts = {}) @values = [] @value_type = opts[:type] @callbacks = opts[:callbacks] @filter = opts[:filter] @ignore_unchanged_values = opts[:ignore_unchanged_values] @name = name.to_s end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/plyushkin/property.rb', line 6 def name @name end |
Class Method Details
Instance Method Details
#all(opts = {}) ⇒ Object
46 47 48 |
# File 'lib/plyushkin/property.rb', line 46 def all(opts = {}) (@filter && !opts[:unfiltered]) ? @values.select(&@filter) : @values end |
#create(attr = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/plyushkin/property.rb', line 23 def create(attr={}) #write a spec for this option ignore_invalid = attr.delete(:ignore_invalid) value = value_type.new(attr) if @ignore_unchanged_values last = @values.last return last if last && last.equal_value?(value) end return if !value.valid? && ignore_invalid @values.insert(insert_position(value.date), value) #write a spec for this option trigger_callback(:after_create) if value.new_record? @dirty = true value end |
#dirty? ⇒ Boolean
93 94 95 |
# File 'lib/plyushkin/property.rb', line 93 def dirty? @dirty end |
#empty? ⇒ Boolean
54 55 56 |
# File 'lib/plyushkin/property.rb', line 54 def empty? @values.empty? end |
#insert_position(datetime) ⇒ Object
58 59 60 61 |
# File 'lib/plyushkin/property.rb', line 58 def insert_position(datetime) index = @values.rindex{|v| v.date < datetime} index.nil? ? 0 : index + 1 end |
#last ⇒ Object
50 51 52 |
# File 'lib/plyushkin/property.rb', line 50 def last @values.last || Plyushkin::NilValue.new end |
#mark_persisted ⇒ Object
97 98 99 100 |
# File 'lib/plyushkin/property.rb', line 97 def mark_persisted @dirty = false all.each(&:mark_persisted) end |
#nil? ⇒ Boolean
89 90 91 |
# File 'lib/plyushkin/property.rb', line 89 def nil? last.is_a?(Plyushkin::NilValue) end |
#trigger_callback(sym) ⇒ Object
42 43 44 |
# File 'lib/plyushkin/property.rb', line 42 def trigger_callback(sym) @callbacks.fetch(sym, DEFAULT_CALLBACK).call if @callbacks end |
#value_hashes ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/plyushkin/property.rb', line 76 def value_hashes all_value_hashes = [] all.each do |value| value_hash = {} value_type.persisted_attributes.each do |attr| value_hash[attr] = value.send(attr) end all_value_hashes << value_hash end all_value_hashes end |
#value_type ⇒ Object
63 64 65 |
# File 'lib/plyushkin/property.rb', line 63 def value_type @value_type ||= Plyushkin::StringValue end |