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 included from Common::Content::DoubleModel::Base

Common::Content::DoubleModel::Base::NOT_USED

Instance Attribute Summary

Attributes included from Common::Content::DoubleModel::Parented

#_parent, #_parent_key

Instance Method Summary collapse

Methods inherited from Common::Content::CollectionModel

#initialize

Methods included from Common::Content::Includer

#include_missing

Methods inherited from Common::Content::DoubleModel

#initialize, new_uuid

Methods included from Common::Content::DoubleModel::DiffableModel

#as_update, #dirty?

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