Class: Eletro::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/eletro/part.rb

Direct Known Subclasses

Capacitor, Diode, Inductor, Resistor, Source, Transistor, Wire

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/eletro/part.rb', line 49

def method_missing(*args)
  txt = args.join
  if txt =~ /p\d+/
    if txt =~ /=/
      var, value = txt.split("=")
      instance_variable_set("@#{var}", value)
      self.class.instance_eval do
        define_method(var, lambda { instance_variable_get("@#{var}") })
      end
    else
      respond_to?(txt) ? send(args.join) : nil
    end
  else
    super
    # raise NoMethodError.new("undefined method `#{args.join}' " +
    # "for #{self.inspect}:#{self.class.name}")
  end

end

Instance Attribute Details

#iObject

Returns the value of attribute i.



4
5
6
# File 'lib/eletro/part.rb', line 4

def i
  @i
end

#p0Object

Returns the value of attribute p0.



4
5
6
# File 'lib/eletro/part.rb', line 4

def p0
  @p0
end

#p1Object

Returns the value of attribute p1.



4
5
6
# File 'lib/eletro/part.rb', line 4

def p1
  @p1
end

#unitObject

Returns the value of attribute unit.



4
5
6
# File 'lib/eletro/part.rb', line 4

def unit
  @unit
end

#vObject

Returns the value of attribute v.



4
5
6
# File 'lib/eletro/part.rb', line 4

def v
  @v
end

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/eletro/part.rb', line 4

def value
  @value
end

Instance Method Details

#*(other) ⇒ Object



14
15
16
# File 'lib/eletro/part.rb', line 14

def *(other)
  self.class.new(self.value*other)
end

#+(other) ⇒ Object



18
19
20
# File 'lib/eletro/part.rb', line 18

def +(other)
  self.class.new(self.value+other)
end

#-(other) ⇒ Object



26
27
28
# File 'lib/eletro/part.rb', line 26

def -(other)
  self.class.new(self.value-other)
end

#/(other) ⇒ Object



22
23
24
# File 'lib/eletro/part.rb', line 22

def /(other)
  self.class.new(self.value/other)
end

#absObject



30
31
32
# File 'lib/eletro/part.rb', line 30

def abs
  self.value.abs
end

#coerce(other) ⇒ Object



43
# File 'lib/eletro/part.rb', line 43

def coerce(other);      [other, @value];    end

#roundObject



45
46
47
# File 'lib/eletro/part.rb', line 45

def round
  # todo, table standard values, find nearest...
end

#to_fObject



34
# File 'lib/eletro/part.rb', line 34

def to_f;      @value;         end

#to_iObject



35
# File 'lib/eletro/part.rb', line 35

def to_i;      @value.to_i;    end

#to_sObject



38
39
40
# File 'lib/eletro/part.rb', line 38

def to_s
  "#{@value}#{unit}"
end