Class: Ecoportal::API::V2::Page::Forces

Inherits:
Common::Content::CollectionModel show all
Defined in:
lib/ecoportal/api/v2/page/forces.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(name, pos: NOT_USED, before: NOT_USED, after: NOT_USED) {|force| ... } ⇒ Ecoportal::API::V2::Page::Force

Note:
  • It won't fix weights unless all the forces of the ooze are present
  • This means that it doesn't fix forces weights on stages, as shared forces could change order in other stages

Creates a new force

Yields:

  • (force)

    to do some stuff with the force

Yield Parameters:

Returns:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ecoportal/api/v2/page/forces.rb', line 50

def add(name, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
  force_doc = force_class.new_doc
  upsert!(force_doc) do |force| #, pos: pos, before: before, after: after) do |section|

    force.name  = name
    if weight = scope_weight(force, pos: pos, before: before, after: after)
      force.weight = weight
    end
    fix_weights!
    yield(force) if block_given?
  end
end

#bindings_by_reference(only_winner: false) ⇒ Hash

Helper to optimize binding searches

Returns:

  • (Hash)

    where

    • key is a section or a component
    • value is Array<Ecoportal::API::V2::Page::Force::Binding>.


20
21
22
23
24
25
26
# File 'lib/ecoportal/api/v2/page/forces.rb', line 20

def bindings_by_reference(only_winner: false)
  self.each_with_object({}) do |force, out|
    force.bindings.by_reference(only_winner: only_winner).each do |ref, bindings|
      (out[ref] ||= []).push(*bindings)
    end
  end
end

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



29
30
31
32
33
# File 'lib/ecoportal/api/v2/page/forces.rb', line 29

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

#get_by_name(name) ⇒ Array<Ecoportal::API::V2::Page::Force>



36
37
38
39
40
# File 'lib/ecoportal/api/v2/page/forces.rb', line 36

def get_by_name(name)
  self.select do |force|
    same_string?(force.name, name)
  end
end

#oozeObject



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

def ooze
  self._parent.ooze
end

#orderedArray<Ecoportal::API::V2::Page::Force>

Gets the forces ordered by weight (as they appear in the page)

Returns:



64
65
66
67
68
# File 'lib/ecoportal/api/v2/page/forces.rb', line 64

def ordered
  self.sort_by.with_index do |force, index|
    [force.weight, index]
  end
end