Class: Excise::Base

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

Constant Summary collapse

TOKEN_EXPRESSION =
/{([^{}\t\r\n]+)}/.freeze
SPECIAL_CHARACTERS_EXPRESSION =
/[\\\^\$\*\+\.\?\(\)\[\]]/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Base

Returns a new instance of Base.



8
9
10
# File 'lib/excise/base.rb', line 8

def initialize(pattern)
  @pattern = pattern
end

Instance Method Details

#parse(string) ⇒ Object



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

def parse(string)
  @matches = pattern_expression.match(string)

  output = {}

  tokens.each_with_index do |key, index|
    output[key.to_sym] = @matches[index+1]
  end

  output
end

#pattern_expressionObject



28
29
30
# File 'lib/excise/base.rb', line 28

def pattern_expression
  @pattern_expression ||= Regexp.new(@pattern.gsub(special_characters_expression, '\\\\\0').gsub(token_expression, "(\.+)"))
end

#special_characters_expressionObject



36
37
38
# File 'lib/excise/base.rb', line 36

def special_characters_expression
  SPECIAL_CHARACTERS_EXPRESSION
end

#token_expressionObject



32
33
34
# File 'lib/excise/base.rb', line 32

def token_expression
  TOKEN_EXPRESSION
end

#tokensObject



24
25
26
# File 'lib/excise/base.rb', line 24

def tokens
  @tokens ||= @pattern.scan(token_expression).flatten
end