parsable 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/lib/parsable/parsed_item.rb +12 -0
- data/lib/parsable/parser.rb +7 -13
- data/lib/parsable/version.rb +1 -1
- data/lib/parsable.rb +3 -1
- data/spec/context_spec.rb +11 -3
- data/spec/parser_spec.rb +9 -9
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48f0ddf2c95fb1197cd638d4e8bfaed246c89be1
|
4
|
+
data.tar.gz: 874eee2df9c4fe3ea3889d11fdbb2d1e6c0225b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f833679a76bf2828ee3a970be01529ac56a18dbfafc294a7ffbda35faf6956fbc86543dbac7ebb73085595a4459391771321ff405e8979f3ed5a5ba323a74bd
|
7
|
+
data.tar.gz: bac16f61d81fa2a50df895529e17674c7ebad8dd8171bbc6cc771cdf1cbb77b12a6ade4b0ddadc39c698f45758479b9e386ee4e0d69d96a7030a210b216849d2
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Parsable
|
2
|
+
class ParsedItem
|
3
|
+
attr_accessor :original, :function, :object, :attribute
|
4
|
+
|
5
|
+
def initialize args={}
|
6
|
+
@original = args[:original]
|
7
|
+
@function = args[:function]
|
8
|
+
@object = args[:object]
|
9
|
+
@attribute = args[:attribute]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/parsable/parser.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
module Parsable
|
2
2
|
class Parser
|
3
3
|
|
4
|
-
attr_accessor :
|
5
|
-
:original_string, :strings, :context
|
4
|
+
attr_accessor :original_string, :strings
|
6
5
|
|
7
6
|
def initialize args={}
|
8
7
|
@original_string = args.fetch(:string).to_s
|
9
|
-
@context = args.fetch(:context, Parsable::Context.new)
|
10
8
|
@strings = all_captures(@original_string)
|
11
9
|
end
|
12
10
|
|
@@ -14,16 +12,12 @@ module Parsable
|
|
14
12
|
strings.uniq.collect do |string|
|
15
13
|
function, object, attribute = capture(string)
|
16
14
|
|
17
|
-
|
18
|
-
:original => string,
|
19
|
-
:
|
20
|
-
:
|
21
|
-
|
22
|
-
|
23
|
-
# end
|
24
|
-
context.read(object, attribute)
|
25
|
-
}
|
26
|
-
}
|
15
|
+
Parsable::ParsedItem.new(\
|
16
|
+
:original => string,
|
17
|
+
:function => function,
|
18
|
+
:object => object,
|
19
|
+
:attribute => attribute
|
20
|
+
)
|
27
21
|
end
|
28
22
|
end
|
29
23
|
|
data/lib/parsable/version.rb
CHANGED
data/lib/parsable.rb
CHANGED
@@ -1,17 +1,19 @@
|
|
1
1
|
require "parsable/version"
|
2
2
|
require 'parsable/parser'
|
3
3
|
require 'parsable/context'
|
4
|
+
require 'parsable/parsed_item'
|
4
5
|
|
5
6
|
module Parsable
|
6
7
|
|
7
8
|
def self.crunch args={}
|
8
9
|
original = args.fetch(:string).to_s
|
9
10
|
parsed_parts = Parsable::Parser.new(args).parse
|
11
|
+
context = args[:context] || Parsable::Context.new
|
10
12
|
|
11
13
|
crunched = original.dup
|
12
14
|
|
13
15
|
parsed_parts.each do |item|
|
14
|
-
crunched.gsub!("{{#{item
|
16
|
+
crunched.gsub!("{{#{item.original}}}", context.read(item.object, item.attribute).to_s)
|
15
17
|
end
|
16
18
|
|
17
19
|
crunched
|
data/spec/context_spec.rb
CHANGED
@@ -25,9 +25,17 @@ describe Parsable::Context do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
describe '#read' do
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
context 'object_key exists' do
|
29
|
+
it "gets value for object.attribute" do
|
30
|
+
context.instance_variable_get('@variables')[:test_object] = OpenStruct.new(:fruit => 'bananas')
|
31
|
+
expect(context.read('test_object', 'fruit')).to eql("bananas")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'no object_key' do
|
36
|
+
it "is nil" do
|
37
|
+
expect(context.read('not_exists', 'fruit')).to be_nil
|
38
|
+
end
|
31
39
|
end
|
32
40
|
end
|
33
41
|
|
data/spec/parser_spec.rb
CHANGED
@@ -25,11 +25,11 @@ describe Parsable::Parser do
|
|
25
25
|
@parsed = Parsable::Parser.new(:string => %(my+{{location.name}}@email.com)).parse.first
|
26
26
|
end
|
27
27
|
it "parses object name" do
|
28
|
-
expect(@parsed
|
28
|
+
expect(@parsed.object).to eql('location')
|
29
29
|
end
|
30
30
|
|
31
31
|
it "parses attribute" do
|
32
|
-
expect(@parsed
|
32
|
+
expect(@parsed.attribute).to eql('name')
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -37,7 +37,7 @@ describe Parsable::Parser do
|
|
37
37
|
it "parses function method" do
|
38
38
|
string = %(my+{{url_safe(location.name)}}@email.com)
|
39
39
|
parsed = Parsable::Parser.new(:string => string).parse.first
|
40
|
-
expect(parsed
|
40
|
+
expect(parsed.function).to eql('url_safe')
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -53,13 +53,13 @@ describe Parsable::Parser do
|
|
53
53
|
|
54
54
|
context 'no function method' do
|
55
55
|
it "parses object names" do
|
56
|
-
expect(@parsed.first
|
57
|
-
expect(@parsed.last
|
56
|
+
expect(@parsed.first.object).to eql('location')
|
57
|
+
expect(@parsed.last.object).to eql('email')
|
58
58
|
end
|
59
59
|
|
60
60
|
it "parses attributes" do
|
61
|
-
expect(@parsed.first
|
62
|
-
expect(@parsed.last
|
61
|
+
expect(@parsed.first.attribute).to eql('name')
|
62
|
+
expect(@parsed.last.attribute).to eql('domain')
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
@@ -68,8 +68,8 @@ describe Parsable::Parser do
|
|
68
68
|
string = %(my+{{url_safe(location.name)}}@{{email.domain}}.com)
|
69
69
|
parsed = Parsable::Parser.new(:string => string).parse
|
70
70
|
|
71
|
-
expect(parsed.first
|
72
|
-
expect(parsed.last
|
71
|
+
expect(parsed.first.function).to eql('url_safe')
|
72
|
+
expect(parsed.last.function).to be_nil
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parsable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hubert Liu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- Rakefile
|
82
82
|
- lib/parsable.rb
|
83
83
|
- lib/parsable/context.rb
|
84
|
+
- lib/parsable/parsed_item.rb
|
84
85
|
- lib/parsable/parser.rb
|
85
86
|
- lib/parsable/version.rb
|
86
87
|
- parsable.gemspec
|