Class: Habaki::Length

Inherits:
Value show all
Includes:
Comparable
Defined in:
lib/habaki/value.rb

Overview

<length> value type in px, pt etc

Instance Attribute Summary collapse

Attributes inherited from Value

#data

Instance Method Summary collapse

Methods inherited from Value

#eql?, #hash

Methods inherited from Node

#to_s

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

#unitSymbol

Returns:

  • (Symbol)


47
48
49
# File 'lib/habaki/value.rb', line 47

def unit
  @unit
end

Instance Method Details

#*(other) ⇒ Length

Returns:



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

Returns:



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

Returns:



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

Returns:



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

Returns:

  • (Integer)

Raises:

  • (ArgumentError)


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

Returns:

  • (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 ?

Returns:

  • (Boolean)


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 ?

Returns:

  • (Boolean)


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

Parameters:

Returns:



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

Returns:

  • (Float, nil)


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_fFloat

Returns:

  • (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

Returns:

  • (Float, nil)


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