Class: GeneralUnits::Box

Inherits:
Object
  • Object
show all
Defined in:
lib/general_units/derivatives/box.rb,
lib/general_units/derivatives/box/packer.rb

Defined Under Namespace

Classes: Packer

Constant Summary collapse

VALUES =
%w{length width height}

Instance Method Summary collapse

Constructor Details

#initialize(length = 0, width = 0, height = 0, unit) ⇒ Box

Returns a new instance of Box.



15
16
17
18
19
20
# File 'lib/general_units/derivatives/box.rb', line 15

def initialize(length = 0, width = 0, height = 0, unit)
  if unit = valid_unit(unit)
    VALUES.each {|v| instance_variable_set(:"@#{v}", validate_dimension_value(eval(v), unit))}
    @unit = unit
  end
end

Instance Method Details

#*(other_object) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/general_units/derivatives/box.rb', line 114

def *(other_object)
  other_object = validate_capacity_or_length(other_object)
  case other_object
  when Length, Volume then Box.new(*values.map {|v| v * other_object}, unit)
  when Box then Box.new(*VALUES.map {|v| eval(v) * other_object.send(v)}, unit)
  end
end

#+(other_object) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/general_units/derivatives/box.rb', line 98

def +(other_object)
  other_object = validate_capacity_or_length(other_object)
  case other_object
  when Length, Volume then Box.new(*values.map {|v| v + other_object/3}, unit)
  when Box then Box.new(*VALUES.map {|v| eval(v) + other_object.send(v)}, unit)
  end
end

#-(other_object) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/general_units/derivatives/box.rb', line 106

def -(other_object)
  other_object = validate_capacity_or_length(other_object)
  case other_object
  when Length, Volume then Box.new(*values.map {|v| v - other_object/3}, unit)
  when Box then Box.new(*VALUES.map {|v| eval(v) - other_object.send(v)}, unit)
  end
end

#-@Object



83
84
85
# File 'lib/general_units/derivatives/box.rb', line 83

def -@
  Box.new(*values.map {|v| -v}, unit)    
end

#/(other_object) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/general_units/derivatives/box.rb', line 122

def /(other_object)
  other_object = validate_capacity_or_length(other_object)
  case other_object
  when Length, Volume then Box.new(*values.map {|v| v / other_object}, unit)
  when Box then Box.new(*VALUES.map {|v| eval(v) / other_object.send(v)}, unit)
  end
end

#==(other_object) ⇒ Object



87
88
89
90
91
92
# File 'lib/general_units/derivatives/box.rb', line 87

def ==(other_object)
  other_object = validate_box(other_object)
  length == other_object.length && width == other_object.width && height == other_object.height
rescue
  false
end

#amountObject



30
31
32
# File 'lib/general_units/derivatives/box.rb', line 30

def amount
  volume.amount
end

#attributesObject



22
23
24
# File 'lib/general_units/derivatives/box.rb', line 22

def attributes
  {:length => length, :width => width, :height => height, :unit => unit}
end

#concat_with(other_box, &block) ⇒ Object



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
175
176
177
# File 'lib/general_units/derivatives/box.rb', line 150

def concat_with(other_box, &block)
  #other_box = other_box.convert_to(unit)
  
  length_1, width_1, height_1 = *values.sort.reverse
  length_2, width_2, height_2 = *other_box.values.sort.reverse

  x1 = (length_1 - length_2).abs
  length = [length_1, length_2].max

  x2 = (width_1 - width_2).abs
  width = [width_1, width_2].max

  height = height_1 + height_2

  if x1 > 0
    y1 = width
    z1 = length_1 > length_2 ? height_2 : height_1
    yield(Box.new(x1, y1, z1, unit)) if block_given?
  end

  if x2 > 0
    y2 = ((length_1 > length_2) && (width_1 > width_2)) ? length-x1 : length
    z2 = width_1 > width_2 ? height_2 : height_1
    yield(Box.new(x2, y2, z2, unit)) if block_given?
  end
  
  Box.new(length, width, height, unit).convert_to(unit)
end

#convert_to(unit) ⇒ Object



42
43
44
# File 'lib/general_units/derivatives/box.rb', line 42

def convert_to(unit)
  Box.new(*values.map {|v| v.convert_to(unit).amount}, unit)
end

#eql?(other_object) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/general_units/derivatives/box.rb', line 94

def eql?(other_object)
  self == other_object
end

#estimated_spaces_with(other_box, &block) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/general_units/derivatives/box.rb', line 179

def estimated_spaces_with(other_box, &block)
  #other_box = other_box.convert_to(unit)
  
  if includes?(other_box)
    length_1, width_1, height_1 = *values.sort.reverse
    length_2, width_2, height_2 = *other_box.values.sort.reverse
    
    estimated_spaces = []
    
    x1 = (length_1 - length_2).abs
    if x1 > 0
      space1 = Box.new(x1, width_1, height_1, unit)
      estimated_spaces << space1
      yield(space1) if block_given?
    end
  
    x2 = (width_1 - width_2).abs
    if x2 > 0
      space2 = Box.new(length_1 - x1, x2, height_1, unit)
      estimated_spaces << space2
      yield(space2) if block_given?
    end
  
    x3 = (height_1 - height_2).abs
    if x3 > 0
      space3 = Box.new(length_2, width_2, x3, unit)
      estimated_spaces << space3
      yield(space3) if block_given?
    end
    
    estimated_spaces
  end
end

#has_space?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/general_units/derivatives/box.rb', line 38

def has_space?
  length > 0 && width > 0 && height > 0
end

#includes?(other_object) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/general_units/derivatives/box.rb', line 75

def includes?(other_object)
  other_object = validate_box(other_object)
  eval VALUES.permutation.to_a.map {|values_names| "(length >= other_object.#{values_names[0]} && width >= other_object.#{values_names[1]} && height >= other_object.#{values_names[2]})"}.join("||")
end

#inspectObject



66
67
68
# File 'lib/general_units/derivatives/box.rb', line 66

def inspect
  "<#{self.class.name} length=#{length} width=#{width} height=#{height} unit=#{unit}>"
end

#max_faceObject



62
63
64
# File 'lib/general_units/derivatives/box.rb', line 62

def max_face
  two_max_values[0] * two_max_values[1]
end

#multiplicator(sum) ⇒ Object

ARITHMETICS END ###



132
133
134
135
136
# File 'lib/general_units/derivatives/box.rb', line 132

def multiplicator(sum)
  GeneralUnits::Arithmetics.three_factors_of(sum).map do |v|
    Box.new(v[0]*length, v[1]*width, v[2]*height, unit)
  end
end

#multiply_to_optimal(sum) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/general_units/derivatives/box.rb', line 142

def multiply_to_optimal(sum)
  if sum.odd?
    [multiply_to_strong_box(sum-1), self]
  else
    [multiply_to_strong_box(sum)]
  end.compact
end

#multiply_to_strong_box(sum) ⇒ Object



138
139
140
# File 'lib/general_units/derivatives/box.rb', line 138

def multiply_to_strong_box(sum)
  multiplicator(sum).min_by {|box| box.values.max - box.values.min}
end

#same_size?(other_object) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/general_units/derivatives/box.rb', line 70

def same_size?(other_object)
  other_object = validate_box(other_object)
  eval VALUES.permutation.to_a.map {|values_names| "(length == other_object.#{values_names[0]} && width == other_object.#{values_names[1]} && height == other_object.#{values_names[2]})"}.join("||")
end

#to_s(*args) ⇒ Object



46
47
48
49
50
51
# File 'lib/general_units/derivatives/box.rb', line 46

def to_s(*args)
  options = args.extract_options!
  value = values.map {|d| d.rounded(options[:round])}.join("x")
  unit_string = unit.to_s(options.merge(:count => values.last.rounded), :format => :short)
  "#{value} #{unit_string}"
end

#to_volumeObject



53
54
55
# File 'lib/general_units/derivatives/box.rb', line 53

def to_volume
  volume
end

#two_max_valuesObject



57
58
59
60
# File 'lib/general_units/derivatives/box.rb', line 57

def two_max_values
  sorted = values.sort.reverse
  sorted.first(2)
end

#valuesObject



26
27
28
# File 'lib/general_units/derivatives/box.rb', line 26

def values
  VALUES.map {|v| self.send(v)}
end

#volumeObject



34
35
36
# File 'lib/general_units/derivatives/box.rb', line 34

def volume
  Volume.new(length * width * height, :"cubic_#{unit.code}")
end