Class: ExtentCalculator

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/extent_calculator.rb

Constant Summary collapse

Unit_conversions =
{
  :inches => {
    :centimeters => 2.54,
    :feet => 1.0/12.0,
    :meters => 0.0254
  },
  :centimeters => {
    :inches => 0.393701,
    :feet => 0.0328084,
    :meters => 0.01
  },
  :feet => {
    :inches => 12.0,
    :centimeters => 2.54*12.0,
    :meters => 0.3048
  },
  :meters => {
    :inches => 39.3701,
    :feet => 3.28084,
    :centimeters => 100.0
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, strict = false, calculate = true) ⇒ ExtentCalculator

Returns a new instance of ExtentCalculator.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'backend/app/model/extent_calculator.rb', line 28

def initialize(obj, strict = false, calculate = true)
  @root_object = obj
  @resource = obj.respond_to?(:root_record_id) ? obj.class.root_model[obj.root_record_id] : nil
  @strict = strict
  @calculated_extent = nil
  @volume = false
  @units = nil
  @decimal_places_in_published_extent = 2
  @container_count = 0
  @container_without_profile_count = 0
  @containers = {}
  @calculated = false
  @timestamp = nil

  if AppConfig.has_key?(:container_management_extent_calculator)
    cfg = AppConfig[:container_management_extent_calculator]
    @volume = cfg.has_key?(:report_volume) && cfg[:report_volume]
    @decimal_places_in_published_extent = cfg[:decimal_places] if cfg.has_key?(:decimal_places)
    self.units = cfg[:unit] if cfg.has_key?(:unit)
  end

  total_extent if calculate
end

Instance Attribute Details

#unitsObject

Returns the value of attribute units



3
4
5
# File 'backend/app/model/extent_calculator.rb', line 3

def units
  @units
end

Instance Method Details

#containers(name = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'backend/app/model/extent_calculator.rb', line 68

def containers(name = nil)
  if name
    con = @containers[name]
    {:count => con[:count], :extent => published_extent(con[:extent])}
  else
    pub_containers = {}
    @containers.each do |k, con|
      pub_containers[k] = {:count => con[:count], :extent => published_extent(con[:extent])}
    end
    pub_containers
  end
end

#to_hashObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'backend/app/model/extent_calculator.rb', line 127

def to_hash
  {
    :object => {:uri => @root_object.uri, :jsonmodel_type => @root_object.class.my_jsonmodel.record_type,
      :title => @root_object.title || @root_object.display_string},
    :resource => @resource ? {:uri => @resource.uri, :title => @resource.title} : nil,
    :total_extent => published_extent,
    :container_count => @container_count,
    :container_without_profile_count => @container_without_profile_count,
    :units => @units,
    :volume => @volume,
    :containers => containers,
    :timestamp => @timestamp.iso8601
  }
end

#total_extent(recalculate = false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'backend/app/model/extent_calculator.rb', line 82

def total_extent(recalculate = false)
  return published_extent if @calculated_extent && !recalculate

  extent = 0

  topcon_rlshp = SubContainer.find_relationship(:top_container_link)
  rel_ids = @root_object.object_graph.ids_for(topcon_rlshp)

  DB.open do |db|
    db[:top_container_link_rlshp].filter(:id => rel_ids).select(:top_container_id).
      distinct().map {|hash| hash[:top_container_id]}.each do |tc_id|

      @container_count += 1

      if (rec = TopContainer[tc_id].related_records(:top_container_profile))
        key = @volume ? rec.name : rec.display_string
        @containers[key] ||= {:count => 0, :extent => 0.0}
        @containers[key][:count] += 1
        ext = if @volume
                vol = 1.0
                [:width, :height, :depth].each {|dim| vol = rec.send(dim).to_f * vol }
                convert(vol, rec.dimension_units.intern)
              else
                convert(rec.send(rec.extent_dimension.intern).to_f, rec.dimension_units.intern)
              end
        @containers[key][:extent] += ext
        extent += ext
      else
        # top container does not have a container profile
        if @strict
          raise "Container without Profile found"
        else
          @container_without_profile_count += 1
        end
      end
    end
  end

  @calculated = true
  @calculated_extent = extent
  @timestamp = Time.now
  published_extent
end