Class: Ecoportal::API::V2::Page::Force::Bindings

Inherits:
Common::Content::CollectionModel show all
Defined in:
lib/ecoportal/api/v2/page/force/bindings.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

Instance Method Summary collapse

Methods inherited from Common::Content::CollectionModel

#[], #_doc_key, #_doc_pos, #_items, #delete!, doc_class, #each, #empty?, #include?, #initialize, #items_class, items_key, items_key=, klass?, #length, new_item, #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, #replace_doc, #reset!, #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, #used_param?

Constructor Details

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

Instance Method Details

#add(reference, name:, pos: NOT_USED, before: NOT_USED, after: NOT_USED) {|binding| ... } ⇒ Ecoportal::API::V2::Page::Force::Binding

Note:
  • As there's no position property, it will upsert to the array of bindings

Creates a new binding

Parameters:

Yields:

  • (binding)

    do some stuff with binding

Yield Parameters:

Returns:

Raises:

  • (ArgumentError)

    when reference is neither of Component or Section

  • (Exception)

    when

    1. reference is a field missing in ooze.components
    2. reference is a section missing in ooze.sections


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 57

def add(reference, name:, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
  binding_doc = binding_class.new_doc

  type = case reference
          when Ecoportal::API::V2::Page::Component
            unless ooze.components.include?(reference)
              msg  = "The field '#{reference.label}' (#{reference.id}) is not present in ooze.components.\n"
              msg += "Review your script (i.e. @var where you store previous ooze runs)."
              raise msg
            end
            Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE
          when Ecoportal::API::V2::Page::Section
            unless ooze.sections.include?(reference)
              msg  = "The section '#{reference.heading}' (#{reference.id}) is not present in ooze.sections.\n"
              msg += "Review your script (i.e. @var where you store previous ooze runs)."
              raise msg
            end
            Ecoportal::API::V2::Page::Force::Binding::SECTION_TYPE
          else
            msg = "You can only create bindings with Component and Section. Given: #{reference.class}"
            raise ArgumentError.new(msg)
          end

  position = scope_position(pos: pos, before: before, after: after)
  upsert!(binding_doc, pos: position) do |bind|
    bind.name         = name
    bind.reference_id = reference.id
    bind.type         = type
    yield(bind) if block_given?
  end
end

#get_by_id(id) ⇒ Ecoportal::API::V2::Page::Force::Binding

Returns binding with id.

Parameters:

  • id (String)

    the id of the binding to find.

Returns:



18
19
20
21
22
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 18

def get_by_id(id)
  self.find do |bind|
    bind.id == id
  end
end

#get_by_name(name, type: nil) ⇒ Array<Ecoportal::API::V2::Page::Force::Binding>

Returns the bindings matching name.

Parameters:

  • type (String) (defaults to: nil)

    target type of binding

    1. Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE
    2. Ecoportal::API::V2::Page::Force::Binding::SECTION_TYPE

Returns:



38
39
40
41
42
43
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 38

def get_by_name(name, type: nil)
  pool = type ? get_by_type(type) : self
  pool.select do |bind|
    same_string?(bind.name, name)
  end
end

#get_by_type(type = Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE) ⇒ Array<Ecoportal::API::V2::Page::Force::Binding>

Returns the bindings of type type.

Parameters:

  • type (String) (defaults to: Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE)

    target type of binding

    1. Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE
    2. Ecoportal::API::V2::Page::Force::Binding::SECTION_TYPE

Returns:



28
29
30
31
32
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 28

def get_by_type(type = Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE)
  self.select do |bind|
    bind.type.downcase == type.to_s.strip.downcase
  end
end

#oozeObject



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

def ooze
  self._parent.ooze
end