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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, string = "") ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
# File 'lib/excise/base.rb', line 10

def initialize(pattern, string = "")
  @pattern = pattern
  @string = string

  @output = {}
end

Instance Attribute Details

#stringObject

Returns the value of attribute string.



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

def string
  @string
end

Instance Method Details

#parseObject



17
18
19
20
21
22
23
# File 'lib/excise/base.rb', line 17

def parse
  @matches = pattern_expression.match(@string)
  tokens.each_with_index do |key, index|
    @output[key.to_sym] = @matches[index+1]
  end
  @output
end

#parse_string(string) ⇒ Object



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

def parse_string(string)
  @string = string
  parse
end

#pattern_expressionObject



34
35
36
# File 'lib/excise/base.rb', line 34

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

#special_characters_expressionObject



42
43
44
# File 'lib/excise/base.rb', line 42

def special_characters_expression
  SPECIAL_CHARACTERS_EXPRESSION
end

#token_expressionObject



38
39
40
# File 'lib/excise/base.rb', line 38

def token_expression
  TOKEN_EXPRESSION
end

#tokensObject



30
31
32
# File 'lib/excise/base.rb', line 30

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