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 +4 -4
- data/.travis.yml +5 -0
- data/README.md +27 -4
- data/Rakefile +4 -6
- data/lib/parsable/context.rb +44 -0
- data/lib/parsable/parser.rb +6 -14
- data/lib/parsable/version.rb +1 -1
- data/lib/parsable.rb +11 -1
- data/spec/context_spec.rb +34 -0
- data/spec/parsable_spec.rb +23 -9
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab6073c965b0d9bf5241f4a1e9083388e57ecd7b
|
4
|
+
data.tar.gz: dc4214f8d8f41bbf11187efaa2cf06825d6fac48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fecf0d3de669811f5a290dc7fbb08c3b4aa3c034575cd8b1ba58f3b1175103c193e409cf9b8eadbf8ddab88874e9669a7251bbdb2646bd417c4190eed87494ae
|
7
|
+
data.tar.gz: a526d289ec739e6f95401b388352d4cc8d0b20a028037339c21c189e751f60eef309dc748e52b5842222ba831bf8ce661fb7299dbc27585ed29296152da6b0b0
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -19,17 +19,40 @@ Or install it yourself as:
|
|
19
19
|
## Usage
|
20
20
|
|
21
21
|
```ruby
|
22
|
-
|
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
|
-
|
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
@@ -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
|
data/lib/parsable/parser.rb
CHANGED
@@ -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
|
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
|
-
|
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
|
data/lib/parsable/version.rb
CHANGED
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
|
-
|
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
|
data/spec/parsable_spec.rb
CHANGED
@@ -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 =>
|
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 =>
|
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 =>
|
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 =>
|
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
|
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-
|
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
|