excise 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Changes.md +6 -0
- data/README.md +3 -3
- data/lib/excise.rb +11 -4
- data/lib/excise/base.rb +8 -14
- data/lib/excise/version.rb +1 -1
- data/spec/excise/base_spec.rb +6 -6
- data/spec/excise_spec.rb +5 -0
- metadata +1 -1
data/Changes.md
CHANGED
data/README.md
CHANGED
@@ -28,9 +28,9 @@ Excise('About {result_count} results ({load_time} seconds)',
|
|
28
28
|
#=> {:result_count=>"49,000,000", :load_time=>"0.15"}
|
29
29
|
|
30
30
|
# Memoize pattern for performance improvement
|
31
|
-
pattern = Excise
|
32
|
-
pattern.
|
33
|
-
pattern.
|
31
|
+
pattern = Excise.new('[{key}]')
|
32
|
+
pattern.parse '[value]' #=> {:key=>"value"}
|
33
|
+
pattern.parse '[other]' #=> {:key=>"other"}
|
34
34
|
```
|
35
35
|
|
36
36
|
## Contributing
|
data/lib/excise.rb
CHANGED
@@ -1,13 +1,20 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require "excise/version"
|
3
3
|
require "excise/base"
|
4
|
+
require "forwardable"
|
4
5
|
|
5
6
|
module Excise
|
6
|
-
|
7
|
-
|
7
|
+
class << self
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def_delegator 'Excise::Base', :new
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.parse(pattern, string)
|
14
|
+
new(pattern).parse(string)
|
8
15
|
end
|
9
16
|
end
|
10
17
|
|
11
|
-
def Excise(
|
12
|
-
Excise.parse(
|
18
|
+
def Excise(pattern, string)
|
19
|
+
Excise.parse(pattern, string)
|
13
20
|
end
|
data/lib/excise/base.rb
CHANGED
@@ -5,26 +5,20 @@ module Excise
|
|
5
5
|
TOKEN_EXPRESSION = /{([^{}\t\r\n]+)}/.freeze
|
6
6
|
SPECIAL_CHARACTERS_EXPRESSION = /[\\\^\$\*\+\.\?\(\)\[\]]/.freeze
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(pattern, string = "")
|
8
|
+
def initialize(pattern)
|
11
9
|
@pattern = pattern
|
12
|
-
@string = string
|
13
|
-
|
14
|
-
@output = {}
|
15
10
|
end
|
16
11
|
|
17
|
-
def parse
|
18
|
-
@matches = pattern_expression.match(
|
12
|
+
def parse(string)
|
13
|
+
@matches = pattern_expression.match(string)
|
14
|
+
|
15
|
+
output = {}
|
16
|
+
|
19
17
|
tokens.each_with_index do |key, index|
|
20
|
-
|
18
|
+
output[key.to_sym] = @matches[index+1]
|
21
19
|
end
|
22
|
-
@output
|
23
|
-
end
|
24
20
|
|
25
|
-
|
26
|
-
@string = string
|
27
|
-
parse
|
21
|
+
output
|
28
22
|
end
|
29
23
|
|
30
24
|
def tokens
|
data/lib/excise/version.rb
CHANGED
data/spec/excise/base_spec.rb
CHANGED
@@ -5,15 +5,15 @@ module Excise
|
|
5
5
|
context 'only a pattern' do
|
6
6
|
let(:base) { Base.new(pattern) }
|
7
7
|
let(:pattern) { '[{first}]' }
|
8
|
+
let(:string) { '[value]' }
|
8
9
|
|
9
10
|
it "should parse strings" do
|
10
|
-
base.string
|
11
|
-
base.parse.should be_a Hash
|
11
|
+
base.parse(string).should be_a Hash
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
context 'pattern and string' do
|
16
|
-
subject { Base.new(pattern
|
16
|
+
subject { Base.new(pattern).parse(string) }
|
17
17
|
|
18
18
|
let(:string) { '[this] (patterned) <STRING>' }
|
19
19
|
let(:pattern) { '[{first}] ({second}) <{third}>' }
|
@@ -23,13 +23,13 @@ module Excise
|
|
23
23
|
its(:values) { should eq ['this', 'patterned', 'STRING'] }
|
24
24
|
end
|
25
25
|
|
26
|
-
describe '#
|
26
|
+
describe '#parse' do
|
27
27
|
let(:pattern) { Base.new('[{first}]') }
|
28
28
|
it "should take a string and parse it" do
|
29
|
-
output = pattern.
|
29
|
+
output = pattern.parse('[value]')
|
30
30
|
output.should eq Hash[{first: 'value'}]
|
31
31
|
|
32
|
-
output = pattern.
|
32
|
+
output = pattern.parse('[other]')
|
33
33
|
output.should eq Hash[{first: 'other'}]
|
34
34
|
end
|
35
35
|
end
|
data/spec/excise_spec.rb
CHANGED