Module: HasSubResources

Included in:
GeoEngineer::Environment, GeoEngineer::Project, GeoEngineer::Resource, GeoEngineer::SubResource
Defined in:
lib/geoengineer/utils/has_sub_resources.rb

Overview

HasSubResources provides methods for a object to contain subresources

Instance Method Summary collapse

Instance Method Details

#assign_block(name, *args, &block) ⇒ Object

This overrides assign_block from HasAttributes when a block is passed to an attribute it becomes a SubResource


7
8
9
10
11
# File 'lib/geoengineer/utils/has_sub_resources.rb', line 7

def assign_block(name, *args, &block)
  sr = GeoEngineer::SubResource.new(self, name, &block)
  subresources << sr
  sr
end

#attribute_missing(name) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/geoengineer/utils/has_sub_resources.rb', line 13

def attribute_missing(name)
  all = false
  if name.start_with?('all_')
    name = name[4..-1]
    all = true
  end
  srl = subresources.select { |s| s.type == name.to_s }

  if srl.empty?
    return [] if all
    return nil
  else
    return srl if all
    return srl.first
  end
end

#delete_all_subresources(type) ⇒ Object


40
41
42
# File 'lib/geoengineer/utils/has_sub_resources.rb', line 40

def delete_all_subresources(type)
  @_subresources = subresources.select { |s| s.type != type.to_s }
end

#delete_subresources_where(&block) ⇒ Object


35
36
37
38
# File 'lib/geoengineer/utils/has_sub_resources.rb', line 35

def delete_subresources_where(&block)
  # Only leave sub resources that dont
  @_subresources = subresources.reject(&block)
end

#subresourcesObject


30
31
32
33
# File 'lib/geoengineer/utils/has_sub_resources.rb', line 30

def subresources
  @_subresources = [] unless @_subresources
  @_subresources
end