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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6dc2aca5f2edb3f0bb1d0522ac63de4a73125969
4
- data.tar.gz: 207194e469beaf3c8ebc89e49bdbda02251550c5
3
+ metadata.gz: 48f0ddf2c95fb1197cd638d4e8bfaed246c89be1
4
+ data.tar.gz: 874eee2df9c4fe3ea3889d11fdbb2d1e6c0225b8
5
5
  SHA512:
6
- metadata.gz: 952219dcc4492b0df30c0bb15b1415c3cdf0ebdddd344d46fb963b9c1cba9b612164d51885996a34b1e60dd33c109928e9ae872a4044ca75867488fa401baae3
7
- data.tar.gz: c12bdd6d23ee1ba81c7a555ddba9d18e6ce06394a5abe1276e8d25f107e2df6e6a6eb9607a585512a300f7ce995ef940321ab2b393e326fe73a223d1af7c54d1
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
@@ -1,12 +1,10 @@
1
1
  module Parsable
2
2
  class Parser
3
3
 
4
- attr_accessor :object, :attribute, :function_method,
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, :function => function,
19
- :object => object, :attribute => attribute,
20
- :lambda => lambda {
21
- # if context[object.to_sym].respond_to?(attribute.to_sym)
22
- # context[object.to_sym].send(attribute.to_sym)
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
 
@@ -1,3 +1,3 @@
1
1
  module Parsable
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
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[:original]}}}", "#{item[:lambda].call}")
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
- it "gets value for object.attribute" do
29
- context.instance_variable_get('@variables')[:test_object] = OpenStruct.new(:fruit => 'bananas')
30
- expect(context.read('test_object', 'fruit')).to eql("bananas")
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[:object]).to eql('location')
28
+ expect(@parsed.object).to eql('location')
29
29
  end
30
30
 
31
31
  it "parses attribute" do
32
- expect(@parsed[:attribute]).to eql('name')
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[:function]).to eql('url_safe')
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[:object]).to eql('location')
57
- expect(@parsed.last[:object]).to eql('email')
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[:attribute]).to eql('name')
62
- expect(@parsed.last[:attribute]).to eql('domain')
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[:function]).to eql('url_safe')
72
- expect(parsed.last[:function]).to be_nil
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.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-19 00:00:00.000000000 Z
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