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

Common::Content::DoubleModel::Diffable::DIFF_CLASS

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

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

  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


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:

  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


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

#componentObject



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

#oozeObject



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

def ooze
  component.ooze
end

#orderedObject



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