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, #_read_only
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  
  #attached?, #bindings, #bindings?, #delete!, #forces, get_class, #indexable_label, #move, #multi_section?, new_doc, #ooze, #ref, #ref_backend, #replace, #replace_bindings, #section, #unattach!
  
  
  
  
  
  
  
  
  
  #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, read_only!, read_only?, #read_only?, #replace_doc, #reset!, #resolved_doc_key, #root, #to_json
  
  
  
  
  
  
  
  
  
  #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant, #to_time, #uid, #used_param?
  
  
  
  
  
  
  
  
  
    Instance Method Details
    
      
  
  
    #add_option(**kargs, &block)  ⇒ Object 
  
  
  
  
    | 
98
99
100 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 98
def add_option(**kargs, &block)
  options.add(**kargs, &block)
end | 
 
    
      
  
  
    | 
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 132
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 
  
  
  
  
    | 
46
47
48
49
50 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 46
def deselect(value_name, by_name: false)
  if opt = options.find_option(value_name, by_name: by_name)
    opt.selected = false
  end
end | 
 
    
      
  
  
    #empty?  ⇒ Boolean 
  
  
  
  
    | 
12
13
14 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 12
def empty?
  values.empty?
end | 
 
    
      
  
  
    #name  ⇒ Object 
  
  
  
  
    | 
85
86
87
88
89
90
91 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 85
def name
  if multiple
    selected.map {|opt| opt.name}
  else
    selected&.name
  end
end | 
 
    
      
  
  
    #names  ⇒ Object 
  
  
  
  
    | 
93
94
95 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 93
def names
  [name].flatten.compact
end | 
 
    
      
  
  
    #numeric!(&block)  ⇒ Object 
  
  
  
  
    | 
24
25
26
27 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 24
def numeric!(&block)
  options.ordered.each {|opt| opt.numeric!(&block)}
  self.data_type = "num"
end | 
 
    
      
  
  
    #numeric?  ⇒ Boolean 
  
  
  
  
    | 
16
17
18 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 16
def numeric?
  self.data_type == "num"
end | 
 
    
      
  
  
    #options_by_name(value: false)  ⇒ Object 
  
  
  
  
    | 
112
113
114 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 112
def options_by_name(value: false)
  options.by_name(value: value)
end | 
 
    
      
  
  
    #options_by_value(name: false)  ⇒ Object 
  
  
  
  
    | 
107
108
109 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 107
def options_by_value(name: false)
  options.by_value(name: name)
end | 
 
    
      
  
  
    #ordered_options  ⇒ Object 
  
  
  
  
    | 
102
103
104 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 102
def ordered_options
  options.ordered
end | 
 
    
      
  
  
    #select(value_name, by_name: false)  ⇒ Object 
  
  
  
  
    | 
38
39
40
41
42
43
44 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 38
def select(value_name, by_name: false)
  opt = options.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 
  
  
  
  
    | 
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 52
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
      options.ordered.select {|opt| opt.selected}
    else
      options.find {|opt| opt.selected}
    end
  end
end | 
 
    
      
  
  
    #switch_type!(&block)  ⇒ Object 
  
  
  
  
    | 
34
35
36 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 34
def switch_type!(&block)
  numeric? ? text!(&block) : numeric!(&block)
end | 
 
    
      
  
  
    #text!(&block)  ⇒ Object 
  
  
  
  
    | 
29
30
31
32 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 29
def text!(&block)
  options.ordered.each {|opt| opt.text!(&block)}
  self.data_type = "str"
end | 
 
    
      
  
  
    #text?  ⇒ Boolean 
  
  
  
  
    | 
20
21
22 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 20
def text?
  self.data_type == "txt"
end | 
 
    
      
  
  
    #to_s(value: true, delimiter: "\n")  ⇒ Object 
  
  
  
  
    | 
116
117
118
119
120 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 116
def to_s(value: true, delimiter: "\n")
  [selected].flatten.compact.map do |opt|
    value ? opt.value : opt.name
  end.join(delimiter)
end | 
 
    
      
  
  
    #value  ⇒ Object 
  
  
  
  
    | 
73
74
75
76
77
78
79 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 73
def value
  if multiple
    selected.map {|opt| opt.value}
  else
    selected&.value
  end
end | 
 
    
      
  
  
    #values  ⇒ Object 
  
  
  
  
    | 
81
82
83 | # File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 81
def values
  [value].flatten.compact
end |