class Plyushkin::Property
Constants
- DEFAULT_CALLBACK
Attributes
name[R]
Public Class Methods
load(name, type, values, opts={})
click to toggle source
# File lib/plyushkin/property.rb, line 67 def self.load(name, type, values, opts={}) opts[:type] = type prop = Plyushkin::Property.new(name, opts).tap do |p| values.each { |value| load_value(p, value) } end prop end
new(name, opts = {})
click to toggle source
# 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
Private Class Methods
load_value(property, value)
click to toggle source
# File lib/plyushkin/property.rb, line 104 def self.load_value(property, value) property.create(value.merge(:new_record => false)) end
Public Instance Methods
all(opts = {})
click to toggle source
# File lib/plyushkin/property.rb, line 46 def all(opts = {}) (@filter && !opts[:unfiltered]) ? @values.select(&@filter) : @values end
create(attr={})
click to toggle source
# 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?()
click to toggle source
# File lib/plyushkin/property.rb, line 93 def dirty? @dirty end
empty?()
click to toggle source
# File lib/plyushkin/property.rb, line 54 def empty? @values.empty? end
insert_position(datetime)
click to toggle source
# 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()
click to toggle source
# File lib/plyushkin/property.rb, line 50 def last @values.last || Plyushkin::NilValue.new end
mark_persisted()
click to toggle source
# File lib/plyushkin/property.rb, line 97 def mark_persisted @dirty = false all.each(&:mark_persisted) end
nil?()
click to toggle source
# File lib/plyushkin/property.rb, line 89 def nil? last.is_a?(Plyushkin::NilValue) end
trigger_callback(sym)
click to toggle source
# File lib/plyushkin/property.rb, line 42 def trigger_callback(sym) @callbacks.fetch(sym, DEFAULT_CALLBACK).call if @callbacks end
value_hashes()
click to toggle source
# 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()
click to toggle source
# File lib/plyushkin/property.rb, line 63 def value_type @value_type ||= Plyushkin::StringValue end