Class: Habaki::Length
- Includes:
- Comparable
- Defined in:
- lib/habaki/value.rb
Overview
<length> value type in px, pt etc
Instance Attribute Summary collapse
Attributes inherited from Value
Instance Method Summary collapse
- #*(other) ⇒ Length
- #+(other) ⇒ Length
- #-(other) ⇒ Length
- #/(other) ⇒ Length
- #<=>(other) ⇒ Integer
- #==(other) ⇒ Boolean
-
#absolute? ⇒ Boolean
is dimension absolute ?.
-
#initialize(data = nil, unit = nil) ⇒ Length
constructor
A new instance of Length.
- #read_from_katana(val) ⇒ void private
-
#relative? ⇒ Boolean
is dimension relative ?.
- #string(format = Formatter::Base.new) ⇒ String
-
#to_em(default_px = 16.0) ⇒ Float?
absolute value to em.
- #to_f ⇒ Float
-
#to_px(ppi = 96) ⇒ Float?
absolute value to pixel.
Methods inherited from Value
Methods inherited from Node
Constructor Details
#initialize(data = nil, unit = nil) ⇒ Length
Returns a new instance of Length.
49 50 51 52 |
# File 'lib/habaki/value.rb', line 49 def initialize(data = nil, unit = nil) @data = data @unit = unit end |
Instance Attribute Details
#unit ⇒ Symbol
47 48 49 |
# File 'lib/habaki/value.rb', line 47 def unit @unit end |
Instance Method Details
#*(other) ⇒ Length
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/habaki/value.rb', line 131 def *(other) case other when Integer, Float Length.new((@data * other).round(3), @unit) when Percentage Length.new((@data * other.data / 100.0).round(3), @unit) else raise ArgumentError, "cannot multiply #{self.class} with #{other.class}" end end |
#+(other) ⇒ Length
109 110 111 112 113 114 115 116 117 |
# File 'lib/habaki/value.rb', line 109 def +(other) case other when Length raise ArgumentError, "cannot addition with different units" unless @unit == other.unit Length.new(@data + other.data, @unit) else raise ArgumentError, "cannot addition #{self.class} with #{other.class}" end end |
#-(other) ⇒ Length
120 121 122 123 124 125 126 127 128 |
# File 'lib/habaki/value.rb', line 120 def -(other) case other when Length raise ArgumentError, "cannot substract with different units" unless @unit == other.unit Length.new(@data - other.data, @unit) else raise ArgumentError, "cannot substract #{self.class} with #{other.class}" end end |
#/(other) ⇒ Length
143 144 145 146 147 148 149 150 |
# File 'lib/habaki/value.rb', line 143 def /(other) case other when Integer, Float Length.new((@data / other).round(3), @unit) else raise ArgumentError, "cannot divide #{self.class} with #{other.class}" end end |
#<=>(other) ⇒ Integer
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/habaki/value.rb', line 155 def <=>(other) raise ArgumentError, "cannot compare #{self.class} with #{other.class}" unless other.is_a?(Length) if @unit == other.unit @data <=> other.data elsif absolute? && other.absolute? to_px <=> other.to_px else nil end end |
#==(other) ⇒ Boolean
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/habaki/value.rb', line 167 def ==(other) return false unless other.is_a?(Length) if @unit == other.unit @data == other.data elsif absolute? && other.absolute? to_px == other.to_px else false end end |
#absolute? ⇒ Boolean
is dimension absolute ?
56 57 58 |
# File 'lib/habaki/value.rb', line 56 def absolute? [:px, :cm, :mm, :in, :pt, :pc].include?(@unit) end |
#read_from_katana(val) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
186 187 188 189 190 |
# File 'lib/habaki/value.rb', line 186 def read_from_katana(val) @data = val.value @unit = val.unit @unit = nil if @unit == :dimension end |
#relative? ⇒ Boolean
is dimension relative ?
62 63 64 |
# File 'lib/habaki/value.rb', line 62 def relative? [:em, :ex, :ch, :rem, :vw, :vh, :vmin, :vmax].include?(@unit) end |
#string(format = Formatter::Base.new) ⇒ String
180 181 182 |
# File 'lib/habaki/value.rb', line 180 def string(format = Formatter::Base.new) @unit ? "#{data_i_or_f}#{@unit}" : @data end |
#to_em(default_px = 16.0) ⇒ Float?
absolute value to em
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/habaki/value.rb', line 90 def to_em(default_px = 16.0) return 0.0 if default_px == 0.0 if absolute? to_px / default_px else if @unit == :em @data else raise TypeError, "cannot convert #{to_s} to em" end end end |
#to_f ⇒ Float
104 105 106 |
# File 'lib/habaki/value.rb', line 104 def to_f @data.is_a?(Float) ? @data : 0.0 end |
#to_px(ppi = 96) ⇒ Float?
absolute value to pixel
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/habaki/value.rb', line 68 def to_px(ppi = 96) case @unit when :px @data when :cm (@data / 2.54) * ppi when :mm ((@data / 10.0) / 2.54) * ppi when :in @data * ppi when :pt @data / (72.0 / ppi) when :pc (@data * 12.0) / (72.0 / ppi) else # relative raise TypeError, "cannot convert relative #{to_s} to px" end end |