Class: Ecoportal::API::V2::Page::Section

Inherits:
Common::Content::DoubleModel show all
Defined in:
lib/ecoportal/api/v2/page/section.rb

Constant Summary collapse

INITIAL_WEIGHT =
9999

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

Class Method Summary collapse

Instance Method Summary collapse

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::DoubleModel

Class Method Details

.new_doc(split: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ecoportal/api/v2/page/section.rb', line 9

def new_doc(split: false)
  {
    "id"         => new_uuid,
    "type"       => split ? "split" : "content",
    "weight"     => INITIAL_WEIGHT
  }.tap do |out|
    component_ids = if split
        {
          "left_component_ids" => [],
          "right_component_ids" => []
        }
      else
        {
          "component_ids" => []
        }
      end
    out.merge!(component_ids)
  end
end

Instance Method Details

#add_component(field, before: nil, after: nil, side: nil) ⇒ Object

Note:
  • To the specified side, when split section (default :left)
  • To the end if after is not specified
  • If after is specified, it searches field
    • On the specific side, if specified (and split section)
    • And adds the field after it, when found, or at the end if after is not found

Adds field to the section

Parameters:

Raises:

  • (ArgumentError)

    if field is not a Component

  • (Exception)

    if the field does not belong to ooze.components

  • (Exception)

    if the field belongs to another section



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ecoportal/api/v2/page/section.rb', line 143

def add_component(field, before: nil, after: nil, side: nil)
  unless field.is_a?(Ecoportal::API::V2::Page::Component)
    msg = "Expected Ecoportal::API::V2::Page::Component. Given: #{field.class}"
    raise ArgumentError.new(msg)
  end
  unless ooze.components.include?(field)
    msg  = "The field '#{field.label}' (#{field.id}) is not present in ooze.components.\n"
    msg += "Review your script (i.e. @var where you store previous ooze runs)."
    raise msg
  end
  # IMPORTANT NOTE: The code below creates objects, because field.section does a search on section.component_ids
  if field.section == self
    puts "Field with id '#{field.id}' already belongs to this section"
  elsif sec = field.section
    # Field belongs to another section
    raise "Field with id '#{field.id}' belongs to section '#{sec.heading || "Unnamed"}' (id: '#{sec.id}')"
  end

  if before
    before_fld = to_component(before, side: side)
  elsif after
    after_fld  = to_component(after, side: side)
  end

  if split?
    ids_ary = side == :right ? right_component_ids : left_component_ids
  else
    ids_ary = component_ids
  end
  ids_ary.insert_one(field.id, before: before_fld&.id, after: after_fld&.id)
  self
end

#all_component_idsArray<String>

Returns all the ids of the components/fields in this section.

Returns:

  • (Array<String>)

    all the ids of the components/fields in this section



81
82
83
84
# File 'lib/ecoportal/api/v2/page/section.rb', line 81

def all_component_ids
  return component_ids.to_a unless split?
  left_component_ids.to_a | right_component_ids.to_a
end

#attached?Boolean

Returns whether or not the section appears in an ooze instance.

Returns:

  • (Boolean)

    whether or not the section appears in an ooze instance



71
72
73
# File 'lib/ecoportal/api/v2/page/section.rb', line 71

def attached?
  !ooze.stages? || stages.any? {|stg| stg.section?(self)}
end

#bindingsArray<Ecoportal::API::V2::Page::Force::Binding] the force bindings if any.

Returns Array<Ecoportal::API::V2::Page::Force::Binding] the force bindings if any.

Returns:



52
53
54
55
56
# File 'lib/ecoportal/api/v2/page/section.rb', line 52

def bindings
  ooze.forces.each_with_object([]) do |force, out|
    out.push(*force.bindings.get_by_reference(self))
  end
end

#bindings?Boolean

Returns true if the section is bound to any force, false otherwise.

Returns:

  • (Boolean)

    true if the section is bound to any force, false otherwise



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

def bindings?
  force.count > 0
end

#component?(com_or_id) ⇒ Boolean

Returns whether or not a component/field belongs to this section.

Parameters:

Returns:

  • (Boolean)

    whether or not a component/field belongs to this section.

Raises:

  • (ArgumentError)

    if com_or_id is not of any of the allowed types



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ecoportal/api/v2/page/section.rb', line 89

def component?(com_or_id)
  case com_or_id
  when Ecoportal::API::V2::Page::Component
    component?(com_or_id.id)
  when String
    all_component_ids.include?(com_or_id)
  else
    msg = "Missuse: com_or_id should be a Component or a String. Given: #{com_or_id.class}"
    raise ArgumentError.new(msg)
  end
end

#components(side: nil) ⇒ Array<Ecoportal::API::V2::Page::Component>

It looks up the components that belong to this section.



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ecoportal/api/v2/page/section.rb', line 103

def components(side: nil)
  case side
  when :right
    components_right
  when :left
    components_left
  when NilClass
    components_by_id(*all_component_ids)
  else
    raise "Side should be one of [nil, :right, :left]. Given: #{side}"
  end
end

#components_leftArray<Ecoportal::API::V2::Page::Component>

It looks up the components that belong to the left side of this section.

Returns:

Raises:

  • (Exception)

    if this is not a split section



119
120
121
122
# File 'lib/ecoportal/api/v2/page/section.rb', line 119

def components_left
  raise "You are trying to retrieve side components in a Split Section" unless split?
  components_by_id(*left_component_ids)
end

#components_rightArray<Ecoportal::API::V2::Page::Component>

It looks up the components that belong to the right side of this section.

Returns:

Raises:

  • (Exception)

    if this is not a split section



127
128
129
130
# File 'lib/ecoportal/api/v2/page/section.rb', line 127

def components_right
  raise "You are trying to retrieve side components in a Split Section" unless split?
  components_by_id(*right_component_ids)
end

#forcesArray<Ecoportal::API::V2::Page::Force] the forces this section is bound to if any.

Returns Array<Ecoportal::API::V2::Page::Force] the forces this section is bound to if any.

Returns:



59
60
61
62
63
# File 'lib/ecoportal/api/v2/page/section.rb', line 59

def forces
  ooze.forces.select do |force|
    force.bindings.reference?(self)
  end
end

#oozeObject



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

def ooze
  self._parent.ooze
end

#shared?Boolean

Returns true if the section belongs to more than 1 stage, false otherwise.

Returns:

  • (Boolean)

    true if the section belongs to more than 1 stage, false otherwise



47
48
49
# File 'lib/ecoportal/api/v2/page/section.rb', line 47

def shared?
  stages.count > 1
end

#split?Boolean

Returns whether or not this section is a split section.

Returns:

  • (Boolean)

    whether or not this section is a split section



76
77
78
# File 'lib/ecoportal/api/v2/page/section.rb', line 76

def split?
  doc && doc["type"] == "split"
end

#stagesArray<Ecoportal::API::V2::Page::Stage>

Returns the stage(s) this section belongs to.

Returns:



42
43
44
# File 'lib/ecoportal/api/v2/page/section.rb', line 42

def stages
  ooze.stages.select {|stg| stg.section?(self)}
end