Class: Nerpin::Nrpn::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nerpin/nrpn.rb

Direct Known Subclasses

Micron

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/nerpin/nrpn.rb', line 6

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/nerpin/nrpn.rb', line 6

def key
  @key
end

#maxObject (readonly)

Returns the value of attribute max.



6
7
8
# File 'lib/nerpin/nrpn.rb', line 6

def max
  @max
end

#minObject (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

.nrpnsObject



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.expand_path(__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

#sampleObject



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