excise 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Changes.md CHANGED
@@ -1,3 +1,9 @@
1
+ # v0.2.1
2
+ * Provide cleaner interface
3
+
4
+ # v0.2.0
5
+ * Memoize patterns
6
+
1
7
  # v0.1.1
2
8
  * Add `Excise(string, pattern)` convenience method. (see: [fb50807](https://github.com/ezkl/excise/commit/fb5080776b722cf6c2f0fabb419374b4e1e7674a))
3
9
 
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::Base.new('[{key}]')
32
- pattern.parse_string '[value]' #=> {:key=>"value"}
33
- pattern.parse_string '[other]' #=> {:key=>"other"}
31
+ pattern = Excise.new('[{key}]')
32
+ pattern.parse '[value]' #=> {:key=>"value"}
33
+ pattern.parse '[other]' #=> {:key=>"other"}
34
34
  ```
35
35
 
36
36
  ## Contributing
@@ -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
- def self.parse(string, pattern)
7
- Base.new(string, pattern).parse
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(string, pattern)
12
- Excise.parse(string, pattern)
18
+ def Excise(pattern, string)
19
+ Excise.parse(pattern, string)
13
20
  end
@@ -5,26 +5,20 @@ module Excise
5
5
  TOKEN_EXPRESSION = /{([^{}\t\r\n]+)}/.freeze
6
6
  SPECIAL_CHARACTERS_EXPRESSION = /[\\\^\$\*\+\.\?\(\)\[\]]/.freeze
7
7
 
8
- attr_accessor :string
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(@string)
12
+ def parse(string)
13
+ @matches = pattern_expression.match(string)
14
+
15
+ output = {}
16
+
19
17
  tokens.each_with_index do |key, index|
20
- @output[key.to_sym] = @matches[index+1]
18
+ output[key.to_sym] = @matches[index+1]
21
19
  end
22
- @output
23
- end
24
20
 
25
- def parse_string(string)
26
- @string = string
27
- parse
21
+ output
28
22
  end
29
23
 
30
24
  def tokens
@@ -1,3 +1,3 @@
1
1
  module Excise
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -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 = '[value]'
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, string).parse }
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 '#parse_string' do
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.parse_string('[value]')
29
+ output = pattern.parse('[value]')
30
30
  output.should eq Hash[{first: 'value'}]
31
31
 
32
- output = pattern.parse_string('[other]')
32
+ output = pattern.parse('[other]')
33
33
  output.should eq Hash[{first: 'other'}]
34
34
  end
35
35
  end
@@ -1,6 +1,11 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Excise do
4
+ describe ".new" do
5
+ subject { Excise.new('{key}') }
6
+ it { should be_a Excise::Base }
7
+ end
8
+
4
9
  describe ".parse" do
5
10
  subject { Excise.parse(pattern, string) }
6
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: