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 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(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


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 121

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, 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

#by_name(only_winner: false) ⇒ Hash

Note:

first local binding name will shadow later ones

Returns where key is name and value is

  1. a single binding, if only_winner is true
  2. an Array of bindings with same name, otherwise.

Parameters:

  • only_winner (Boolean) (defaults to: false)

    specifies if retrieving multiple bindings with same name or just the winner

Returns:

  • (Hash)

    where key is name and value is

    1. a single binding, if only_winner is true
    2. an Array of bindings with same name, otherwise


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

def by_name(only_winner: false)
  each_with_object({}) do |binding, hash|
    if winner_only
      hash[binding.name] ||= binding
    else
      hash[binding.name] ||= []
      hash[binding.name].push(binding)
    end
  end
end

#by_reference(only_winner: false) ⇒ Hash

Note:

first local binding name will shadow later ones.

Returns where key is a section or a component and value is eitheran Array of bindings.

Parameters:

  • only_winner (Boolean) (defaults to: false)

    specifies if shadowed bindings (inactive) should be excluded (true)

Returns:

  • (Hash)

    where key is a section or a component and value is eitheran Array of bindings.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 42

def by_reference(only_winner: false)
  if only_winner
    by_name(only_winner: true).each_with_object({}) do |(_name, binds), hash|
      if binds.is_a?(Array)
        binds.each {|binding| (hash[binding.reference] ||= []).push(binding)}
      else
        (hash[binds.reference] ||= []).push(binds)
      end
    end
  else
    each_with_object({}) do |binding, hash|
      (hash[binding.reference] ||= []).push(binding)
    end
  end
end

#forceObject



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

def force
  _parent
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:



82
83
84
85
86
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 82

def get_by_id(id)
  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:



102
103
104
105
106
107
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 102

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_reference(obj) ⇒ Array<Ecoportal::API::V2::Page::Force::Binding>

Returns binding to the component or section.

Parameters:

Returns:



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 68

def get_by_reference(obj)
  unless obj.is_a?(Ecoportal::API::V2::Page::Section) || obj.is_a?(Ecoportal::API::V2::Page::Component)
    msg  = 'Expected either a Ecoportal::API::V2::Page::Section '
    msg << 'or a Ecoportal::API::V2::Page::Component. '
    msg << "Given: #{obj.class}"
    raise ArgumentError, msg
  end
  self.select do |bind|
    bind.reference == obj
  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:



92
93
94
95
96
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 92

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
  _parent.ooze
end

#reference?(obj) ⇒ Boolean

Returns true if obj is referred in the bindings, false otherwise.

Parameters:

Returns:

  • (Boolean)

    true if obj is referred in the bindings, false otherwise



61
62
63
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 61

def reference?(obj)
  get_by_reference(obj).count.positive?
end