Class: CosSinCalc::Triangle::Formatter
- Inherits:
-
Object
- Object
- CosSinCalc::Triangle::Formatter
- Defined in:
- lib/cossincalc/triangle/formatter.rb,
lib/cossincalc/triangle/formatter/latex.rb
Defined Under Namespace
Classes: Latex
Constant Summary collapse
- UNIT_FACTORS =
{ :radian => Math::PI, :degree => 180.0, :gon => 200.0 }
Instance Attribute Summary collapse
-
#precision ⇒ Object
readonly
References to associated triangle object and data precision.
-
#triangle ⇒ Object
readonly
References to associated triangle object and data precision.
Class Method Summary collapse
-
.convert_angle(value, unit, reverse = false) ⇒ Object
Converts the given value from the unit specified to radians.
-
.parse(value) ⇒ Object
Converts an input value to a number.
-
.parse_angle(value, from_unit) ⇒ Object
Converts an input angle value in some unit to a radian number.
Instance Method Summary collapse
-
#angle(v) ⇒ Object
Returns the size of the angle to the given variable, rounded and converted to the prefered unit.
-
#initialize(triangle, precision = 2) ⇒ Formatter
constructor
A new instance of Formatter.
Constructor Details
#initialize(triangle, precision = 2) ⇒ Formatter
Returns a new instance of Formatter.
8 9 10 11 |
# File 'lib/cossincalc/triangle/formatter.rb', line 8 def initialize(triangle, precision = 2) @triangle = triangle @precision = precision end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (private)
64 65 66 67 68 69 70 |
# File 'lib/cossincalc/triangle/formatter.rb', line 64 def method_missing(name, *args) if [:side, :altitude, :median, :angle_bisector, :area, :circumference].include?(name) format(t.send(name, *args), @precision) else super(name, *args) end end |
Instance Attribute Details
#precision ⇒ Object (readonly)
References to associated triangle object and data precision.
6 7 8 |
# File 'lib/cossincalc/triangle/formatter.rb', line 6 def precision @precision end |
#triangle ⇒ Object (readonly)
References to associated triangle object and data precision.
6 7 8 |
# File 'lib/cossincalc/triangle/formatter.rb', line 6 def triangle @triangle end |
Class Method Details
.convert_angle(value, unit, reverse = false) ⇒ Object
Converts the given value from the unit specified to radians. If reverse is true, the value will be converted from radians to the specified unit.
30 31 32 33 34 35 |
# File 'lib/cossincalc/triangle/formatter.rb', line 30 def self.convert_angle(value, unit, reverse = false) return nil if value.nil? factor = (Math::PI / UNIT_FACTORS[unit.to_sym]) value * (reverse ? 1.0 / factor : factor) end |
.parse(value) ⇒ Object
Converts an input value to a number.
14 15 16 17 18 19 20 21 |
# File 'lib/cossincalc/triangle/formatter.rb', line 14 def self.parse(value) return value if value.is_a? Float return nil if value.nil? || value.to_s !~ /\S/ value = value.to_s.gsub(/[^\d.,]+/, '').split(/[^\d]+/) value.push('.' + value.pop) if value.length > 1 value.join.to_f end |
.parse_angle(value, from_unit) ⇒ Object
Converts an input angle value in some unit to a radian number.
24 25 26 |
# File 'lib/cossincalc/triangle/formatter.rb', line 24 def self.parse_angle(value, from_unit) value.is_a?(Float) ? value : convert_angle(parse(value), from_unit) end |
Instance Method Details
#angle(v) ⇒ Object
Returns the size of the angle to the given variable, rounded and converted to the prefered unit.
38 39 40 |
# File 'lib/cossincalc/triangle/formatter.rb', line 38 def angle(v) format(Formatter.convert_angle(t.angle(v), t.angles.unit, true), @precision) end |