Class: Plyushkin::Property

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/plyushkin/property.rb

Constant Summary collapse

DEFAULT_CALLBACK =
lambda{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/plyushkin/property.rb', line 6

def name
  @name
end

Class Method Details

.load(name, type, values, opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
# 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

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

Returns:

  • (Boolean)


93
94
95
# File 'lib/plyushkin/property.rb', line 93

def dirty?
  @dirty
end

#empty?Boolean

Returns:

  • (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

#lastObject



50
51
52
# File 'lib/plyushkin/property.rb', line 50

def last
  @values.last || Plyushkin::NilValue.new
end

#mark_persistedObject



97
98
99
100
# File 'lib/plyushkin/property.rb', line 97

def mark_persisted
  @dirty = false
  all.each(&:mark_persisted)
end

#nil?Boolean

Returns:

  • (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_hashesObject



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_typeObject



63
64
65
# File 'lib/plyushkin/property.rb', line 63

def value_type
  @value_type ||= Plyushkin::StringValue
end