Class: Ecoportal::API::V2::Page::Component::SelectionField
Constant Summary
Common::Content::StringDigest::MAX_HASH_LABEL
Common::Content::DoubleModel::NOT_USED
Common::Content::ClassHelpers::NOT_USED
Instance Attribute Summary
#_key, #_parent
Instance Method Summary
collapse
-
#add_option(value:, name: nil, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
-
#configure(*conf) ⇒ Object
-
#deselect(value_name, by_name: false) ⇒ Object
-
#name ⇒ Object
-
#names ⇒ Object
-
#numeric!(&block) ⇒ Object
-
#numeric? ⇒ Boolean
-
#options_by_name(value: false) ⇒ Hash
Of key name and value: 1.
-
#options_by_value(name: false) ⇒ Hash
Of key value and value: 1.
-
#ordered_options ⇒ Object
-
#select(value_name, by_name: false) ⇒ Object
-
#selected(by_name: false, by_value: false, value: false, name: false) ⇒ Object
-
#switch_type!(&block) ⇒ Object
-
#text!(&block) ⇒ Object
-
#text? ⇒ Boolean
-
#to_s(name: true, delimiter: "\n") ⇒ Object
-
#value ⇒ Object
-
#values ⇒ Object
#attached?, #bindings, #bindings?, #forces, get_class, #indexable_label, #multi_section?, new_doc, #ooze, #ref, #ref_backend, #section
#hash_label, #indexable_label
#_doc_key, #as_json, #as_update, #consolidate!, #dirty?, #doc, embeds_many, embeds_one, enforce!, #initialize, #key, #key=, key?, new_uuid, #original_doc, pass_reader, pass_writer, passarray, passboolean, passdate, passforced, passkey, passthrough, #print_pretty, #replace_doc, #reset!, #root, #to_json
#inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant, #to_time, #used_param?
Instance Method Details
#add_option(value:, name: nil, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 94
def add_option(value:, name: nil, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
opt_doc = options.items_class.new_doc
options.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_option_weights!
end
end
|
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 149
def configure(*conf)
conf.each_with_object([]) do |cnf, unused|
case cnf
when :flat
self.flat = true
when :multiple
self.multiple = true
when :single
self.multiple = false
when :other
self.other = true
when Hash
supported = [:flat, :options, :type]
unless (rest = hash_except(cnf.dup, *supported)).empty?
unused.push(rest)
end
if cnf.key?(:flat) then self.flat = cnf[:flat] end
if cnf.key?(:options)
if opts = cnf[:options]
configure_options opts
end
end
if cnf.key?(:type)
if cnf[:type] == :str
self.text!
elsif cnf[:type] == :num
self.numeric!
else
end
end
else
unused.push(cnf)
end
end.yield_self do |unused|
super(*unused)
end
end
|
#deselect(value_name, by_name: false) ⇒ Object
43
44
45
46
47
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 43
def deselect(value_name, by_name: false)
if opt = find_option(value_name, by_name: by_name)
opt.selected = false
end
end
|
#name ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 82
def name
if multiple
selected.map {|opt| opt.name}
else
selected&.name
end
end
|
#names ⇒ Object
90
91
92
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 90
def names
[name].flatten.compact
end
|
#numeric!(&block) ⇒ Object
21
22
23
24
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 21
def numeric!(&block)
ordered_options.each {|opt| opt.numeric!(&block)}
self.data_type = "num"
end
|
#numeric? ⇒ Boolean
13
14
15
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 13
def numeric?
self.data_type == "num"
end
|
#options_by_name(value: false) ⇒ Hash
Returns of key name and value:
- option if
value is false
- value if
value is true.
127
128
129
130
131
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 127
def options_by_name(value: false)
options_hash(by_name: true) do |option|
value ? option.value : option
end
end
|
#options_by_value(name: false) ⇒ Hash
Returns of key value and value:
- option if
name is false
- name if
name is true.
117
118
119
120
121
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 117
def options_by_value(name: false)
options_hash do |option|
name ? option.name : option
end
end
|
#ordered_options ⇒ Object
107
108
109
110
111
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 107
def ordered_options
options.sort_by.with_index do |option, index|
[option.weight, index]
end
end
|
#select(value_name, by_name: false) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 35
def select(value_name, by_name: false)
opt = find_option(value_name, by_name: by_name)
sel = selected
return true if !multiple && opt == sel
sel.selected = false if !multiple && sel
opt.selected = true unless !opt
end
|
#selected(by_name: false, by_value: false, value: false, name: false) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 49
def selected(by_name: false, by_value: false, value: false, name: false)
case
when by_value
elems = [selected].flatten.compact
options_hash(elems) do |option|
name ? option.name : option
end
when by_name
elems = [selected].flatten.compact
options_hash(elems, by_name: true) do |option|
value ? option.value : option
end
else
if multiple
ordered_options.select {|opt| opt.selected}
else
options.find {|opt| opt.selected}
end
end
end
|
#switch_type!(&block) ⇒ Object
31
32
33
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 31
def switch_type!(&block)
numeric? ? text!(&block) : numeric!(&block)
end
|
#text!(&block) ⇒ Object
26
27
28
29
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 26
def text!(&block)
ordered_options.each {|opt| opt.text!(&block)}
self.data_type = "str"
end
|
#text? ⇒ Boolean
17
18
19
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 17
def text?
self.data_type == "txt"
end
|
#to_s(name: true, delimiter: "\n") ⇒ Object
133
134
135
136
137
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 133
def to_s(name: true, delimiter: "\n")
[selected].flatten.compact.map do |opt|
name ? opt.name : opt.value
end.join(delimiter)
end
|
#value ⇒ Object
70
71
72
73
74
75
76
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 70
def value
if multiple
selected.map {|opt| opt.value}
else
selected&.value
end
end
|
#values ⇒ Object
78
79
80
|
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 78
def values
[value].flatten.compact
end
|