parsable 0.0.3 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c740f58f539581f7589ed578a9ed87ea584a4c97
4
- data.tar.gz: a3be8dcade555f1e0feeb3ccf4a47a989bd70485
3
+ metadata.gz: ab6073c965b0d9bf5241f4a1e9083388e57ecd7b
4
+ data.tar.gz: dc4214f8d8f41bbf11187efaa2cf06825d6fac48
5
5
  SHA512:
6
- metadata.gz: dc9c47f798a3e30eb128084b973a9d2723581f761112c5085506ea1ea1374a5cd142f7e0d15ffe9f02bf9facf2fd3ae3d3610e365c0e11a4f117cc6fb5bfb0e6
7
- data.tar.gz: a5af0ba0090e4f737897dfce4b2f4da65bd7ea1d17898c3fa61e3a48c2856e14790fd0dc5cec56469fea97219aafc5261f2d288022944f394ff3c0852da69337
6
+ metadata.gz: fecf0d3de669811f5a290dc7fbb08c3b4aa3c034575cd8b1ba58f3b1175103c193e409cf9b8eadbf8ddab88874e9669a7251bbdb2646bd417c4190eed87494ae
7
+ data.tar.gz: a526d289ec739e6f95401b388352d4cc8d0b20a028037339c21c189e751f60eef309dc748e52b5842222ba831bf8ce661fb7299dbc27585ed29296152da6b0b0
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
data/README.md CHANGED
@@ -19,17 +19,40 @@ Or install it yourself as:
19
19
  ## Usage
20
20
 
21
21
  ```ruby
22
- email = OpenStruct.new(:name => "Bert")
22
+ context = Parsable::Context.new
23
+
24
+ context.system_store('email', 'name', 'Bert') # top level for pre-defined variables
23
25
 
24
26
  Parsable.crunch(\
25
- :string => %(my+{{email.name}}@email.com),
26
- :context => {:email => email}
27
+ :string => %(my+{{email.name}}@email.com), :context => context
27
28
  )
28
29
 
29
30
  #=> my+Bert@email.com
30
31
  ```
31
32
 
32
- You must pass in a context that has a key that matches used `{{variables}}`. The value of that key must respond to the attribute (`{{variable.attribute}}`)
33
+ ```ruby
34
+ context = Parsable::Context.new
35
+ context.custom_store('email', 'bert@company.com') # scoped to "custom" for user-entered variables
36
+
37
+ context.read('custom', 'email')
38
+ #=> 'bert@company.com'
39
+
40
+ Parsable.crunch(\
41
+ :string => %({{custom.email}} is my email!), :context => context
42
+ )
43
+
44
+ #=> bert@company.com is my email!
45
+ ```
46
+
47
+ ### Pre-defined
48
+ ```ruby
49
+ Parsable.crunch(:string => "{{random.hex}} {{random.integer}}")
50
+ # "6e53a6dbab3a8e9b0eb9d467463c8a46 1392849191"
51
+ # note: {{random.integer}} is implemented by Time.now.to_i, so not really random at all.
52
+
53
+ Parsable.crunch(:string => "{{date.today}} {{date.year}} {{time.now}}")
54
+ # "2014-02-19 2014 2014-02-19 17:35:35 -0500"
55
+ ```
33
56
 
34
57
  ## Contributing
35
58
 
data/Rakefile CHANGED
@@ -1,8 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
- require "rake/testtask"
2
+ require 'rspec/core/rake_task'
3
3
 
4
- Rake::TestTask.new do |t|
5
- t.pattern = "spec/**/*_spec.rb"
6
- t.libs.push 'spec'
7
- t.verbose = true
8
- end
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
@@ -0,0 +1,44 @@
1
+ require 'securerandom'
2
+ require 'date'
3
+ require 'ostruct'
4
+
5
+ module Parsable
6
+ class Context
7
+
8
+ attr_accessor :variables
9
+
10
+ def initialize args={}
11
+
12
+ @variables = args.fetch(:variables, {
13
+ :random => OpenStruct.new(:hex => SecureRandom.hex, :integer => Time.now.to_i),
14
+ :date => OpenStruct.new(:today => Date.today.to_s, :year => Date.today.year.to_s),
15
+ :time => OpenStruct.new(:now => Time.now.to_s),
16
+ :custom => OpenStruct.new
17
+ }) # object_key => OpenStruct.new(:attribute => value)
18
+ # {{object.attribute}} returns value
19
+ end
20
+
21
+ def custom_store attribute, value
22
+ _store :custom, attribute, value
23
+ end
24
+
25
+ def system_store object_key, attribute, value
26
+ _store object_key, attribute, value
27
+ end
28
+
29
+ def read object_key, attribute
30
+ _object(object_key).send(attribute.to_sym)
31
+ end
32
+
33
+ private
34
+
35
+ def _object object_key
36
+ variables[object_key.to_sym] ||= OpenStruct.new
37
+ end
38
+
39
+ def _store object_key, attribute, value
40
+ _object(object_key).send("#{attribute}=".to_sym, value)
41
+ end
42
+
43
+ end
44
+ end
@@ -6,35 +6,27 @@ module Parsable
6
6
 
7
7
  def initialize args={}
8
8
  @original_string = args.fetch(:string).to_s
9
- @context = args[:context]
9
+ @context = args.fetch(:context, Parsable::Context.new)
10
10
  @strings = all_captures(@original_string)
11
11
  end
12
12
 
13
13
  def parse
14
- strings.collect do |string|
14
+ strings.uniq.collect do |string|
15
15
  function, object, attribute = capture(string)
16
16
 
17
17
  {
18
18
  :original => string, :function => function,
19
19
  :object => object, :attribute => attribute,
20
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
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)
24
25
  }
25
26
  }
26
27
  end
27
28
  end
28
29
 
29
- def crunch
30
- crunched = original_string.dup
31
- parse.each do |item|
32
- crunched.gsub!("{{#{item[:original]}}}", "#{item[:lambda].call}")
33
- end
34
-
35
- crunched
36
- end
37
-
38
30
  private
39
31
 
40
32
  def capture string
@@ -1,3 +1,3 @@
1
1
  module Parsable
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/parsable.rb CHANGED
@@ -1,10 +1,20 @@
1
1
  require "parsable/version"
2
2
  require 'parsable/parser'
3
+ require 'parsable/context'
3
4
 
4
5
  module Parsable
5
6
 
6
7
  def self.crunch args={}
7
- Parsable::Parser.new(args).crunch
8
+ original = args.fetch(:string).to_s
9
+ parsed_parts = Parsable::Parser.new(args).parse
10
+
11
+ crunched = original.dup
12
+
13
+ parsed_parts.each do |item|
14
+ crunched.gsub!("{{#{item[:original]}}}", "#{item[:lambda].call}")
15
+ end
16
+
17
+ crunched
8
18
  end
9
19
 
10
20
  end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Parsable::Context do
4
+
5
+ let(:context) { Parsable::Context.new }
6
+
7
+ describe '#new' do
8
+ it "sets default variables" do
9
+ expect(context.instance_variable_get('@variables').keys.size).to eql(4)
10
+ end
11
+ end
12
+
13
+ describe '#custom_store' do
14
+ it "stores custom variables in the custom object_key" do
15
+ context.custom_store :email, "test@test.com"
16
+ expect(context.instance_variable_get('@variables')[:custom][:email]).to eql("test@test.com")
17
+ end
18
+ end
19
+
20
+ describe '#system_store' do
21
+ it "stores variables as a top level object_key" do
22
+ context.system_store :test_object, 'test_attribute', 'test_value'
23
+ expect(context.instance_variable_get('@variables')[:test_object][:test_attribute]).to eql("test_value")
24
+ end
25
+ end
26
+
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")
31
+ end
32
+ end
33
+
34
+ end
@@ -2,13 +2,18 @@ require 'spec_helper'
2
2
 
3
3
  describe Parsable do
4
4
 
5
+ let(:context) { Parsable::Context.new }
6
+
7
+ before :each do
8
+ context.system_store 'location', 'name', 'Here'
9
+ end
10
+
5
11
  describe '.crunch' do
6
12
  it "outputs string with crunched values" do
7
- location = OpenStruct.new(:name => "Here")
8
13
 
9
14
  output = Parsable.crunch(\
10
15
  :string => %(my+{{location.name}}@email.com),
11
- :context => {:location => location}
16
+ :context => context
12
17
  )
13
18
 
14
19
  expect(output).to eql(%(my+Here@email.com))
@@ -16,11 +21,10 @@ describe Parsable do
16
21
 
17
22
  context "nil string" do
18
23
  it "does nothing" do
19
- location = OpenStruct.new(:name => "Here")
20
24
 
21
25
  output = Parsable.crunch(\
22
26
  :string => nil,
23
- :context => {:location => location}
27
+ :context => context
24
28
  )
25
29
 
26
30
  expect(output).to eql("")
@@ -32,20 +36,31 @@ describe Parsable do
32
36
  string = %(my@email.com)
33
37
  output = Parsable.crunch(\
34
38
  :string => string,
35
- :context => {}
39
+ :context => context
36
40
  )
37
41
 
38
42
  expect(output).to eql(string)
39
43
  end
40
44
  end
41
45
 
46
+ context "same replacement" do
47
+ it "replaces both strings" do
48
+ string = %(my{{location.name}}@email.com{{location.name}})
49
+ output = Parsable.crunch(\
50
+ :string => string,
51
+ :context => context
52
+ )
53
+
54
+ expect(output).to eql(%(myHere@email.comHere))
55
+ end
56
+ end
57
+
42
58
  context "method not defined" do
43
59
  it "replaces with empty" do
44
- location = OpenStruct.new(:name => "Here")
45
60
 
46
61
  output = Parsable.crunch(\
47
62
  :string => %(my+{{location.not_name}}@email.com),
48
- :context => {:location => location}
63
+ :context => context
49
64
  )
50
65
 
51
66
  expect(output).to eql(%(my+@email.com))
@@ -54,11 +69,10 @@ describe Parsable do
54
69
 
55
70
  context "no context key" do
56
71
  it "replaces with empty" do
57
- location = OpenStruct.new(:name => "Here")
58
72
 
59
73
  output = Parsable.crunch(\
60
74
  :string => %(my+{{wrong_key.name}}@email.com),
61
- :context => {:location => location}
75
+ :context => context
62
76
  )
63
77
 
64
78
  expect(output).to eql(%(my+@email.com))
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.0.3
4
+ version: 0.1.0
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-18 00:00:00.000000000 Z
11
+ date: 2014-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,14 +74,17 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - .gitignore
77
+ - .travis.yml
77
78
  - Gemfile
78
79
  - LICENSE.txt
79
80
  - README.md
80
81
  - Rakefile
81
82
  - lib/parsable.rb
83
+ - lib/parsable/context.rb
82
84
  - lib/parsable/parser.rb
83
85
  - lib/parsable/version.rb
84
86
  - parsable.gemspec
87
+ - spec/context_spec.rb
85
88
  - spec/parsable_spec.rb
86
89
  - spec/parser_spec.rb
87
90
  - spec/spec_helper.rb
@@ -110,6 +113,7 @@ signing_key:
110
113
  specification_version: 4
111
114
  summary: LiquidText style parsing/crunching
112
115
  test_files:
116
+ - spec/context_spec.rb
113
117
  - spec/parsable_spec.rb
114
118
  - spec/parser_spec.rb
115
119
  - spec/spec_helper.rb