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 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
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
name
and value is 1. -
#by_reference(only_winner: false) ⇒ Hash
Where key is a section or a component and value is eitheran
Array
of 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
true
ifobj
is referred in the bindings,false
otherwise.
Methods inherited from Common::Content::CollectionModel
#[], #_doc_key, #_doc_pos, #_items, #clear, #delete!, #each, #empty?, #include?, #initialize, #items_class, items_key, items_key=, klass?, #length, new_item, new_item_class_based?, #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, read_only!, read_only?, #read_only?, #replace_doc, #reset!, #resolved_doc_key, #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, #uid, #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
- As there's no position property, it will upsert to the array of bindings
Creates a new binding
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 114 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 |
#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_winner
istrue
- an
Array
of bindings with same name, otherwise.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 25 def by_name(only_winner: false) self.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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 40 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 self.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 self._parent end |
#get_by_id(id) ⇒ Ecoportal::API::V2::Page::Force::Binding
Returns binding with id
.
75 76 77 78 79 |
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 75 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
.
95 96 97 98 99 100 |
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 95 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.
64 65 66 67 68 69 70 71 |
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 64 def get_by_reference(obj) unless obj.is_a?(Ecoportal::API::V2::Page::Section) || obj.is_a?(Ecoportal::API::V2::Page::Component) raise ArgumentError.new("Expected either a Ecoportal::API::V2::Page::Section or a Ecoportal::API::V2::Page::Component. Given: #{obj.class}") 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
.
85 86 87 88 89 |
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 85 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 self._parent.ooze end |
#reference?(obj) ⇒ Boolean
Returns true
if obj
is referred in the bindings, false
otherwise.
58 59 60 |
# File 'lib/ecoportal/api/v2/page/force/bindings.rb', line 58 def reference?(obj) get_by_reference(obj).count > 0 end |