Class: Ecoportal::API::V2::Page::Force::Bindings
- Inherits:
-
Common::Content::CollectionModel
- Object
- Common::BaseModel
- Common::Content::DoubleModel
- Common::Content::CollectionModel
- Ecoportal::API::V2::Page::Force::Bindings
- 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
Instance Method Summary collapse
-
#add(reference, name:, pos: NOT_USED, before: NOT_USED, after: NOT_USED) {|binding| ... } ⇒ Ecoportal::API::V2::Page::Force::Binding
Creates a new
binding. -
#by_name(only_winner: false) ⇒ Hash
Where key is
nameand value is 1. -
#by_reference(only_winner: false) ⇒ Hash
Where key is a section or a component and value is eitheran
Arrayof bindings. - #force ⇒ Object
-
#get_by_id(id) ⇒ Ecoportal::API::V2::Page::Force::Binding
Binding with
id. -
#get_by_name(name, type: nil) ⇒ Array<Ecoportal::API::V2::Page::Force::Binding>
The bindings matching
name. -
#get_by_reference(obj) ⇒ Array<Ecoportal::API::V2::Page::Force::Binding>
Binding to the component or section.
-
#get_by_type(type = Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE) ⇒ Array<Ecoportal::API::V2::Page::Force::Binding>
The bindings of type
type. - #ooze ⇒ Object
-
#reference?(obj) ⇒ Boolean
trueifobjis referred in the bindings,falseotherwise.
Methods inherited from Common::Content::CollectionModel
Methods included from Common::Content::Includer
Methods inherited from Common::Content::DoubleModel
Methods included from Common::Content::DoubleModel::Diffable
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
- As there's no position property, it will upsert to the array of bindings
Creates a new binding
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
first local binding name will shadow later ones
Returns where key is name and value is
- a single binding, if
only_winneristrue - an
Arrayof 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
first local binding name will shadow later ones.
Returns 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 |
#force ⇒ Object
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.
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.
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.
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.
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 |
#ooze ⇒ Object
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.
61 62 63 |
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 61 def reference?(obj) get_by_reference(obj).count.positive? end |