Class: NumberMuncher::Token::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/number_muncher/token/base.rb

Direct Known Subclasses

Float, Fraction, Int

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, captures = nil) ⇒ Base

Returns a new instance of Base.



23
24
25
26
27
# File 'lib/number_muncher/token/base.rb', line 23

def initialize(text, captures = nil)
  @text = text
  @captures = captures
  @value = Numeric.new(parse)
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



4
5
6
# File 'lib/number_muncher/token/base.rb', line 4

def text
  @text
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/number_muncher/token/base.rb', line 4

def value
  @value
end

Class Method Details

.captures(scanner, text) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/number_muncher/token/base.rb', line 11

def self.captures(scanner, text)
  if scanner.respond_to?(:captures)
    scanner.captures.map(&:presence)
  else
    match = regex.match(text)

    match.regexp.named_captures.each_with_object([]) do |(capture, _), arr|
      arr << match[capture]
    end
  end
end

.scan(scanner) ⇒ Object



7
8
9
# File 'lib/number_muncher/token/base.rb', line 7

def self.scan(scanner)
  new(scanner.matched, captures(scanner, scanner.matched)) if scanner.scan(regex)
end

Instance Method Details

#==(other) ⇒ Object



48
49
50
51
52
# File 'lib/number_muncher/token/base.rb', line 48

def ==(other)
  return to_a == other if other.is_a?(Array)

  super
end

#float?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/number_muncher/token/base.rb', line 33

def float?
  false
end

#fraction?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/number_muncher/token/base.rb', line 37

def fraction?
  false
end

#inspectObject



54
55
56
# File 'lib/number_muncher/token/base.rb', line 54

def inspect
  to_a.to_s
end

#int?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/number_muncher/token/base.rb', line 29

def int?
  false
end

#to_aObject



41
42
43
44
45
46
# File 'lib/number_muncher/token/base.rb', line 41

def to_a
  [
    self.class.name.demodulize.underscore.to_sym,
    value
  ]
end