Class: Eletro::Resistor

Inherits:
Part
  • Object
show all
Includes:
ColorCode
Defined in:
lib/eletro/resistor.rb

Constant Summary

Constants included from ColorCode

ColorCode::CODE, ColorCode::COLORS, ColorCode::PRECISION

Instance Attribute Summary

Attributes included from ColorCode

#color

Attributes inherited from Part

#i, #p0, #p1, #v, #value

Instance Method Summary collapse

Methods included from ColorCode

#bold, #calc, #color2value, #parse_color_code, #rgblize, #value2color

Methods inherited from Part

#*, #+, #-, #/, #abs, #coerce, #method_missing, #round, #to_f, #to_i, #to_s

Constructor Details

#initialize(*args) ⇒ Resistor

Returns a new instance of Resistor.



11
12
13
# File 'lib/eletro/resistor.rb', line 11

def initialize(*args)
  parse_color_code(args.join)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Eletro::Part

Instance Method Details

#formatObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/eletro/resistor.rb', line 33

def format
  v = if value < 1000
    "%g" % @value
  elsif value < 1000000
    "%gk" % (@value/1000)
  else
    "%gm" % (@value/1000000)
  end
  out = "#{v}#{unit}"
  out += " ± #{@precision}%" if @precision
  out
end

#multObject



29
30
31
# File 'lib/eletro/resistor.rb', line 29

def mult
  { 'k' => 10e2, 'm' => 10e5 }
end

#parse(txt) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/eletro/resistor.rb', line 15

def parse txt
  if m = txt.match(/#{mult.keys.join('|')}/)
    txt.gsub!(/#{m[0]}/, ".")
    m = mult[m[0]]
  end
  num = txt.to_f
  num *= m if m
  num
end

#pretty_outputObject



46
47
48
49
50
51
# File 'lib/eletro/resistor.rb', line 46

def pretty_output
  out = format
  out += "   --"
  @color.each { |c| out +=  COLOR ? rgblize(c) : c }
  out + "--"
end

#unitObject



25
26
27
# File 'lib/eletro/resistor.rb', line 25

def unit
  "Ω"
end