excise 0.1.1 → 0.2.0

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Excise [![Build Status](https://secure.travis-ci.org/ezkl/excise.png)](http://travis-ci.org/ezkl/excise)
1
+ # Excise [![Build Status](https://secure.travis-ci.org/ezkl/excise.png)](http://travis-ci.org/ezkl/excise) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/ezkl/excise)
2
2
  > This is a simple helper to extract values from a string based on a pattern.
3
3
 
4
4
  A Ruby port of [laktek's](https://github.com/laktek/) [extract-values](https://github.com/laktek/extract-values).
@@ -22,9 +22,15 @@ Or install it yourself as:
22
22
  ```ruby
23
23
  require 'excise'
24
24
 
25
- Excise('About 49,000,000 results (0.15 seconds)',
26
- 'About {result_count} results ({load_time} seconds)')
25
+ # Basic use
26
+ Excise('About {result_count} results ({load_time} seconds)',
27
+ 'About 49,000,000 results (0.15 seconds)')
27
28
  #=> {:result_count=>"49,000,000", :load_time=>"0.15"}
29
+
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"}
28
34
  ```
29
35
 
30
36
  ## Contributing
data/lib/excise/base.rb CHANGED
@@ -2,34 +2,45 @@
2
2
 
3
3
  module Excise
4
4
  class Base
5
- def initialize(string, pattern)
6
- @string = string
5
+ TOKEN_EXPRESSION = /{([^{}\t\r\n]+)}/.freeze
6
+ SPECIAL_CHARACTERS_EXPRESSION = /[\\\^\$\*\+\.\?\(\)\[\]]/.freeze
7
+
8
+ attr_accessor :string
9
+
10
+ def initialize(pattern, string = "")
7
11
  @pattern = pattern
8
- @matches = pattern_expression.match(@string)
12
+ @string = string
13
+
9
14
  @output = {}
10
15
  end
11
16
 
12
17
  def parse
18
+ @matches = pattern_expression.match(@string)
13
19
  tokens.each_with_index do |key, index|
14
20
  @output[key.to_sym] = @matches[index+1]
15
21
  end
16
22
  @output
17
23
  end
18
24
 
25
+ def parse_string(string)
26
+ @string = string
27
+ parse
28
+ end
29
+
19
30
  def tokens
20
- @pattern.scan(token_expression).flatten
31
+ @tokens ||= @pattern.scan(token_expression).flatten
21
32
  end
22
33
 
23
34
  def pattern_expression
24
- Regexp.new(@pattern.gsub(special_characters_expression, '\\\\\0').gsub(token_expression, "(\.+)"))
35
+ @pattern_expression ||= Regexp.new(@pattern.gsub(special_characters_expression, '\\\\\0').gsub(token_expression, "(\.+)"))
25
36
  end
26
37
 
27
38
  def token_expression
28
- /{([^{}\t\r\n]+)}/
39
+ TOKEN_EXPRESSION
29
40
  end
30
41
 
31
42
  def special_characters_expression
32
- /[\\\^\$\*\+\.\?\(\)\[\]]/
43
+ SPECIAL_CHARACTERS_EXPRESSION
33
44
  end
34
45
  end
35
46
  end
@@ -1,3 +1,3 @@
1
1
  module Excise
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -2,8 +2,18 @@ require "spec_helper"
2
2
 
3
3
  module Excise
4
4
  describe "Base" do
5
- context 'patterned string' do
6
- subject { Base.new(string, pattern).parse }
5
+ context 'only a pattern' do
6
+ let(:base) { Base.new(pattern) }
7
+ let(:pattern) { '[{first}]' }
8
+
9
+ it "should parse strings" do
10
+ base.string = '[value]'
11
+ base.parse.should be_a Hash
12
+ end
13
+ end
14
+
15
+ context 'pattern and string' do
16
+ subject { Base.new(pattern, string).parse }
7
17
 
8
18
  let(:string) { '[this] (patterned) <STRING>' }
9
19
  let(:pattern) { '[{first}] ({second}) <{third}>' }
@@ -12,5 +22,16 @@ module Excise
12
22
  its(:keys) { should eq [:first, :second, :third] }
13
23
  its(:values) { should eq ['this', 'patterned', 'STRING'] }
14
24
  end
25
+
26
+ describe '#parse_string' do
27
+ let(:pattern) { Base.new('[{first}]') }
28
+ it "should take a string and parse it" do
29
+ output = pattern.parse_string('[value]')
30
+ output.should eq Hash[{first: 'value'}]
31
+
32
+ output = pattern.parse_string('[other]')
33
+ output.should eq Hash[{first: 'other'}]
34
+ end
35
+ end
15
36
  end
16
37
  end
data/spec/excise_spec.rb CHANGED
@@ -2,18 +2,18 @@ require "spec_helper"
2
2
 
3
3
  describe Excise do
4
4
  describe ".parse" do
5
- subject { Excise.parse(string, pattern) }
5
+ subject { Excise.parse(pattern, string) }
6
6
 
7
- let(:string) { '[this] (patterned) <STRING>' }
8
7
  let(:pattern) { '[{first}] ({second}) <{third}>' }
8
+ let(:string) { '[this] (patterned) <STRING>' }
9
9
 
10
10
  it { should be_a Hash }
11
11
  end
12
12
 
13
13
  describe "Excise convenience method" do
14
- subject { Excise(string, pattern) }
15
- let(:string) { '[first]' }
14
+ subject { Excise(pattern, string) }
16
15
  let(:pattern) { '[{first}]' }
16
+ let(:string) { '[first]' }
17
17
 
18
18
  it { should be_a Hash }
19
19
  end
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.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -62,18 +62,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
62
  - - ! '>='
63
63
  - !ruby/object:Gem::Version
64
64
  version: '0'
65
- segments:
66
- - 0
67
- hash: -3359420660313118086
68
65
  required_rubygems_version: !ruby/object:Gem::Requirement
69
66
  none: false
70
67
  requirements:
71
68
  - - ! '>='
72
69
  - !ruby/object:Gem::Version
73
70
  version: '0'
74
- segments:
75
- - 0
76
- hash: -3359420660313118086
77
71
  requirements: []
78
72
  rubyforge_project:
79
73
  rubygems_version: 1.8.23
@@ -82,3 +76,4 @@ specification_version: 3
82
76
  summary: ! 'From extract-values description: "... a simple helper to extract values
83
77
  from a string based on a pattern."'
84
78
  test_files: []
79
+ has_rdoc: