Class: Ecoportal::API::V2::Page::Component::SelectionOptions
Constant Summary
Common::Content::DoubleModel::Diffable::DIFF_CLASS
Common::Content::DoubleModel::Base::NOT_USED
Instance Attribute Summary
#_parent, #_parent_key
Instance Method Summary
collapse
#initialize
#include_missing
#initialize, new_uuid
#as_update, #dirty?
Instance Method Details
#add(value:, name: nil, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 20
def add(value:, name: nil, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
opt_doc = items_class.new_doc
upsert!(opt_doc, pos: pos, before: before, after: after) do |option|
option.name = name
option.value = value
if (prev = previous_option(option))
option.weight = prev.weight
end
yield(option) if block_given?
fix_weights!
end
end
|
#by_name(value: false) ⇒ Hash
Returns of key name and value:
- option if
value is false
- value if
value is true.
65
66
67
68
69
|
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 65
def by_name(value: false)
hash(by_name: true) do |option|
value ? option.value : option
end
end
|
#by_value(name: false) ⇒ Hash
Returns of key value and value:
- option if
name is false
- name if
name is true.
55
56
57
58
59
|
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 55
def by_value(name: false)
hash do |option|
name ? option.name : option
end
end
|
#component ⇒ Object
12
13
14
|
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 12
def component
_parent
end
|
#find_option(value_name, by_name: false) ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 37
def find_option(value_name, by_name: false)
if by_name
find {|opt| same_string?(opt.name, value_name)}
else
find {|opt| opt.value == value_name}
end
end
|
#hash(elems = ordered, by_name: false) ⇒ Object
71
72
73
74
75
76
|
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 71
def hash(elems = ordered, by_name: false)
elems.each_with_object({}) do |option, hash|
key = by_name ? option.name : option.value
hash[key] = block_given?? yield(option) : option
end
end
|
#ooze ⇒ Object
16
17
18
|
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 16
def ooze
component.ooze
end
|
#ordered ⇒ Object
45
46
47
48
49
|
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 45
def ordered
sort_by.with_index do |option, index|
[option.weight, index]
end
end
|