Class: Shi::Args::Value::Measure

Inherits:
Object
  • Object
show all
Defined in:
lib/shi/args/values.rb

Constant Summary collapse

KS =
{
  :in => 288,
  :px => 288 / 96,
  :pt => 288 / 72,
  :pc => 288 / 6,
  :mm => 288 / 25.4,
  :cm => 288 / 2.54,
  :Q => 288 / (25.4 * 4)
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, number, unit) ⇒ Measure

Returns a new instance of Measure.

Parameters:

  • value (String)
  • number (Numeric)
  • unit (Symnol, String)


92
93
94
95
96
# File 'lib/shi/args/values.rb', line 92

def initialize value, number, unit
  @value = value
  @number = number
  @unit = unit.intern
end

Instance Attribute Details

#numberNumeric (readonly)

Returns:

  • (Numeric)


84
85
86
# File 'lib/shi/args/values.rb', line 84

def number
  @number
end

#unitSymbol (readonly)

Returns:

  • (Symbol)


87
88
89
# File 'lib/shi/args/values.rb', line 87

def unit
  @unit
end

#valueString (readonly)

Returns:

  • (String)


81
82
83
# File 'lib/shi/args/values.rb', line 81

def value
  @value
end

Class Method Details

.px(number) ⇒ String

Parameters:

  • number (Numeric)

Returns:

  • (String)


75
76
77
# File 'lib/shi/args/values.rb', line 75

def px number
  new "#{number}px", number, :px
end

Instance Method Details

#to(unit) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/shi/args/values.rb', line 103

def to unit
  self_k = KS[@unit]
  if self_k.nil?
    raise ArgumentError, "Invalid unit for conversion: #{@unit.inspect}!"
  end
  unit_k = KS[unit.intern]
  if unit_k.nil?
    raise ArgumentError, "Invalid unit for conversion: #{unit.inspect}!"
  end
  r_number = @number * self_k / unit_k
  r_value = "#{r_number}#{unit}"
  Measure::new r_value, r_number, unit
end

#to_pxObject



117
118
119
# File 'lib/shi/args/values.rb', line 117

def to_px
  to(:px).number
end

#to_sString

Returns:

  • (String)


99
100
101
# File 'lib/shi/args/values.rb', line 99

def to_s
  @value
end