Class: GeneralUnits::Box
- Inherits:
-
Object
- Object
- GeneralUnits::Box
- 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
- #*(other_object) ⇒ Object
- #+(other_object) ⇒ Object
- #-(other_object) ⇒ Object
- #-@ ⇒ Object
- #/(other_object) ⇒ Object
- #==(other_object) ⇒ Object
- #amount ⇒ Object
- #attributes ⇒ Object
- #concat_with(other_box, &block) ⇒ Object
- #convert_to(unit) ⇒ Object
- #eql?(other_object) ⇒ Boolean
- #estimated_spaces_with(other_box, &block) ⇒ Object
- #formatted(round = nil, &block) ⇒ Object
- #has_space? ⇒ Boolean
- #includes?(other_object) ⇒ Boolean
-
#initialize(length = 0, width = 0, height = 0, unit) ⇒ Box
constructor
A new instance of Box.
- #inspect ⇒ Object
- #max_face ⇒ Object
-
#multiplicator(sum) ⇒ Object
ARITHMETICS END ###.
- #multiply_to_optimal(sum) ⇒ Object
- #multiply_to_strong_box(sum) ⇒ Object
- #same_size?(other_object) ⇒ Boolean
- #to_s(round = nil) ⇒ Object
- #to_volume ⇒ Object
- #two_max_values ⇒ Object
- #values ⇒ Object
- #volume ⇒ Object
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
119 120 121 122 123 124 125 |
# File 'lib/general_units/derivatives/box.rb', line 119 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
103 104 105 106 107 108 109 |
# File 'lib/general_units/derivatives/box.rb', line 103 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
111 112 113 114 115 116 117 |
# File 'lib/general_units/derivatives/box.rb', line 111 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
88 89 90 |
# File 'lib/general_units/derivatives/box.rb', line 88 def -@ Box.new(*values.map {|v| -v}, unit) end |
#/(other_object) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/general_units/derivatives/box.rb', line 127 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
92 93 94 95 96 97 |
# File 'lib/general_units/derivatives/box.rb', line 92 def ==(other_object) other_object = validate_box(other_object) length == other_object.length && width == other_object.width && height == other_object.height rescue false end |
#amount ⇒ Object
30 31 32 |
# File 'lib/general_units/derivatives/box.rb', line 30 def amount volume.amount end |
#attributes ⇒ Object
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
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/general_units/derivatives/box.rb', line 155 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
99 100 101 |
# File 'lib/general_units/derivatives/box.rb', line 99 def eql?(other_object) self == other_object end |
#estimated_spaces_with(other_box, &block) ⇒ Object
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 212 213 214 215 216 |
# File 'lib/general_units/derivatives/box.rb', line 184 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 |
#formatted(round = nil, &block) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/general_units/derivatives/box.rb', line 50 def formatted(round = nil, &block) if block_given? yield to_s(round), unit else "#{to_s(round)} #{unit.short}" end end |
#has_space? ⇒ 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
80 81 82 83 |
# File 'lib/general_units/derivatives/box.rb', line 80 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 |
#inspect ⇒ Object
71 72 73 |
# File 'lib/general_units/derivatives/box.rb', line 71 def inspect "<#{self.class.name} length=#{length} width=#{width} height=#{height} unit=#{unit}>" end |
#max_face ⇒ Object
67 68 69 |
# File 'lib/general_units/derivatives/box.rb', line 67 def max_face two_max_values[0] * two_max_values[1] end |
#multiplicator(sum) ⇒ Object
ARITHMETICS END ###
137 138 139 140 141 |
# File 'lib/general_units/derivatives/box.rb', line 137 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
147 148 149 150 151 152 153 |
# File 'lib/general_units/derivatives/box.rb', line 147 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
143 144 145 |
# File 'lib/general_units/derivatives/box.rb', line 143 def multiply_to_strong_box(sum) multiplicator(sum).min_by {|box| box.values.max - box.values.min} end |
#same_size?(other_object) ⇒ Boolean
75 76 77 78 |
# File 'lib/general_units/derivatives/box.rb', line 75 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(round = nil) ⇒ Object
46 47 48 |
# File 'lib/general_units/derivatives/box.rb', line 46 def to_s(round = nil) values.map {|d| d.to_s(round)}.join("x") end |
#to_volume ⇒ Object
58 59 60 |
# File 'lib/general_units/derivatives/box.rb', line 58 def to_volume volume end |
#two_max_values ⇒ Object
62 63 64 65 |
# File 'lib/general_units/derivatives/box.rb', line 62 def two_max_values sorted = values.sort.reverse sorted.first(2) end |
#values ⇒ Object
26 27 28 |
# File 'lib/general_units/derivatives/box.rb', line 26 def values VALUES.map {|v| self.send(v)} end |