Class: Nerpin::Nrpn::Base
- Inherits:
-
Object
- Object
- Nerpin::Nrpn::Base
- Defined in:
- lib/nerpin/nrpn.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(id, key, min, max) ⇒ Base
constructor
A new instance of Base.
- #sample ⇒ Object
- #value(value) ⇒ Object
- #value_at(scale) ⇒ Object
Constructor Details
#initialize(id, key, min, max) ⇒ Base
Returns a new instance of Base.
32 33 34 35 36 37 |
# File 'lib/nerpin/nrpn.rb', line 32 def initialize(id, key, min, max) @id = id @key = key @min = min @max = max end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/nerpin/nrpn.rb', line 6 def id @id end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/nerpin/nrpn.rb', line 6 def key @key end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
6 7 8 |
# File 'lib/nerpin/nrpn.rb', line 6 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
6 7 8 |
# File 'lib/nerpin/nrpn.rb', line 6 def min @min end |
Class Method Details
.find_by_key(key) ⇒ Object
9 10 11 |
# File 'lib/nerpin/nrpn.rb', line 9 def find_by_key(key) nrpns[key] end |
.nrpns ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/nerpin/nrpn.rb', line 13 def nrpns return @nrpns if @nrpns data = JSON.parse( open( File.dirname( File.(__FILE__) ) + '/../../data/nrpn/%s.json' % self.name.split(/::/).last.downcase ).read ) @nrpns = Hash[ data.map do |k, v| [k.to_sym, new(v['id'], k.to_sym, v['min'], v['max'])] end ].freeze end |
Instance Method Details
#sample ⇒ Object
47 48 49 |
# File 'lib/nerpin/nrpn.rb', line 47 def sample value(rand(min.abs + max + (min < 0 ? 2 : 1)) - min.abs - (min < 0 ? 1 : 0)) end |
#value(value) ⇒ Object
39 40 41 |
# File 'lib/nerpin/nrpn.rb', line 39 def value(value) raise 'subclass must override this' end |
#value_at(scale) ⇒ Object
43 44 45 |
# File 'lib/nerpin/nrpn.rb', line 43 def value_at(scale) value((((min.abs + max) * scale) - min.abs).to_i) end |