Class: Riseup::Spec

Inherits:
Object
  • Object
show all
Defined in:
lib/riseup/spec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ Spec

Returns a new instance of Spec.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/riseup/spec.rb', line 5

def initialize(spec)
  unless spec.is_a?(Array)
    raise ArgumentError, 'Specification must be an array'
  end
  spec.each_with_index do |i, j|
    unless i.is_a?(Array)
      raise ArgumentError, "Specification item #{j} must be an array"
    end
    if i.size < 2
      raise ArgumentError, "Specification item #{j} is too small, must contain at least 2 items"
    end
    if i.size > 3
      raise ArgumentError, "Specification item #{j} is too largw, must contain no more than 3 items"
    end
  end
  @spec = spec
  @spec_map = Hash[@spec.collect { |t| [t[0], TokenData.new(t[1], t[2])] }]
  @tokens = @spec.map { |t| t[0] }
  # (?x)
  @regex = Regexp.new('(' + (@tokens.map { |t| Regexp.escape(t) }).join('|') + '|[.])')
end

Instance Attribute Details

#regexObject (readonly)

Returns the value of attribute regex.



35
36
37
# File 'lib/riseup/spec.rb', line 35

def regex
  @regex
end

#tokensObject (readonly)

Returns the value of attribute tokens.



37
38
39
# File 'lib/riseup/spec.rb', line 37

def tokens
  @tokens
end

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/riseup/spec.rb', line 27

def [](key)
  @spec_map[key]
end

#include?(x) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/riseup/spec.rb', line 31

def include?(x)
  @spec_map.include?(x)
end