Class: Ecoportal::API::V2::Page::Component::SelectionOptions

Inherits:
Common::Content::CollectionModel show all
Defined in:
lib/ecoportal/api/v2/page/component/selection_options.rb

Constant Summary

Constants inherited from Common::Content::DoubleModel

Common::Content::DoubleModel::NOT_USED

Constants included from Common::Content::ClassHelpers

Common::Content::ClassHelpers::NOT_USED

Instance Attribute Summary

Attributes inherited from Common::Content::DoubleModel

#_key, #_parent, #_read_only

Instance Method Summary collapse

Methods inherited from Common::Content::CollectionModel

#[], #_doc_key, #_doc_pos, #_items, #clear, #delete!, #each, #empty?, #include?, #initialize, #items_class, items_key, items_key=, klass?, #length, new_item, new_item_class_based?, #present?, #upsert!, #values_at

Methods inherited from Common::Content::DoubleModel

#_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

Methods included from Common::Content::ClassHelpers

#inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant, #to_time, #uid, #used_param?

Constructor Details

This class inherits a constructor from Ecoportal::API::Common::Content::CollectionModel

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
# 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:

  1. option if value is false
  2. value if value is true.

Parameters:

  • value (Boolean) (defaults to: false)

    whether or not the values of the Hash should be the values

Returns:

  • (Hash)

    of key name and value:

    1. option if value is false
    2. value if value is true


61
62
63
64
65
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 61

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:

  1. option if name is false
  2. name if name is true.

Parameters:

  • name (Boolean) (defaults to: false)

    whether or not the values of the Hash should be the names

Returns:

  • (Hash)

    of key value and value:

    1. option if name is false
    2. name if name is true


51
52
53
54
55
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 51

def by_value(name: false)
  hash do |option|
    name ? option.name : option
  end
end

#componentObject



12
13
14
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 12

def component
  self._parent
end

#find_option(value_name, by_name: false) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 33

def find_option(value_name, by_name: false)
  if by_name
    opt = self.find {|opt| same_string?(opt.name, value_name)}
  else
    opt = self.find {|opt| opt.value == value_name}
  end
end

#hash(elems = ordered, by_name: false) ⇒ Object



67
68
69
70
71
72
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 67

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

#oozeObject



16
17
18
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 16

def ooze
  component.ooze
end

#orderedObject



41
42
43
44
45
# File 'lib/ecoportal/api/v2/page/component/selection_options.rb', line 41

def ordered
  self.sort_by.with_index do |option, index|
    [option.weight, index]
  end
end