Class: Leafy::FieldValueCollection
- Inherits:
-
Object
- Object
- Leafy::FieldValueCollection
- Includes:
- Enumerable
- Defined in:
- lib/leafy/field_value_collection.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](index) ⇒ Object
- #each ⇒ Object
-
#initialize(leafy_fields, values = {}) ⇒ FieldValueCollection
constructor
A new instance of FieldValueCollection.
- #size ⇒ Object
- #values ⇒ Object
- #values=(attributes = {}) ⇒ Object
Constructor Details
#initialize(leafy_fields, values = {}) ⇒ FieldValueCollection
Returns a new instance of FieldValueCollection.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/leafy/field_value_collection.rb', line 23 def initialize(leafy_fields, values = {}) @leafy_fields = leafy_fields @leafy_field_values = leafy_fields.map do |custom_field| Leafy::FieldValue.new( id: custom_field.id, name: custom_field.name, raw: values[custom_field.id], type: custom_field.type ) end end |
Class Method Details
.dump(field_values_collection) ⇒ Object
50 51 52 |
# File 'lib/leafy/field_value_collection.rb', line 50 def self.dump(field_values_collection) JSON.dump(field_values_collection.map { |field_value| [field_value.id, field_value.raw] }.to_h) end |
.load(leafy_fields, data) ⇒ Object
54 55 56 |
# File 'lib/leafy/field_value_collection.rb', line 54 def self.load(leafy_fields, data) Leafy::FieldValueCollection.new(leafy_fields, JSON.load(data)) end |
Instance Method Details
#[](index) ⇒ Object
15 16 17 |
# File 'lib/leafy/field_value_collection.rb', line 15 def [](index) to_a[index] end |
#each ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/leafy/field_value_collection.rb', line 7 def each if block_given? @leafy_field_values.each { |i| yield i } else @leafy_field_values.each end end |
#size ⇒ Object
19 20 21 |
# File 'lib/leafy/field_value_collection.rb', line 19 def size count end |
#values ⇒ Object
35 36 37 38 39 40 |
# File 'lib/leafy/field_value_collection.rb', line 35 def values inject({}) do |acc, field_value| acc[field_value.id] = field_value.value acc end end |
#values=(attributes = {}) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/leafy/field_value_collection.rb', line 42 def values=(attributes = {}) attributes = attributes.dup.to_a.map { |pair| [pair[0].to_s, pair[1]]}.to_h @leafy_field_values.each do |field_value| field_value.value = attributes[field_value.id] end end |