parsable 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f593cfaed243bd455a5b54d6fbc209eedd367de8
4
+ data.tar.gz: 35cb090332db65e58ec19d83f16b95ff68a848c7
5
+ SHA512:
6
+ metadata.gz: be718003528ade2a45f7ad33678197608ce8cb7d768f031cdbbc35c5cdcc1e4d7ac27fdb336d033e82a581aacf200f58e7a8bb39d2ad39ef3e9dbc629474f15f
7
+ data.tar.gz: a106062667fde8970b5897b14fedcb7bcbcdba1cb1ad617df680d20f7c512927f95023f97aa1f971bf8f1d555de4d0031e7050a7545ae21adba5c4aa36bd8af9
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rbenv*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in parsable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Hubert Liu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Parsable
2
+
3
+ A basic implementation of replacing inline {{variables}} with values. Inspired by [Shopify's Liquid](https://github.com/Shopify/liquid)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'parsable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install parsable
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ email = OpenStruct.new(:name => "Bert")
23
+
24
+ Parsable.crunch(\
25
+ :string => %(my+{{email.name}}@email.com),
26
+ :context => {:email => email}
27
+ )
28
+
29
+ #=> my+Bert@email.com
30
+ ```
31
+
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
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( http://github.com/<my-github-username>/parsable/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "spec/**/*_spec.rb"
6
+ t.libs.push 'spec'
7
+ t.verbose = true
8
+ end
@@ -0,0 +1,7 @@
1
+ module Parsable
2
+ class Combiner
3
+
4
+
5
+
6
+ end
7
+ end
@@ -0,0 +1,67 @@
1
+ module Parsable
2
+ class Parser
3
+
4
+ attr_accessor :object, :attribute, :function_method,
5
+ :original_string, :strings, :context
6
+
7
+ def initialize args={}
8
+ @original_string = args.fetch(:string)
9
+ @context = args[:context]
10
+ @strings = all_captures(@original_string)
11
+ end
12
+
13
+ def parse
14
+ strings.collect do |string|
15
+ function, object, attribute = capture(string)
16
+
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
+ }
25
+ }
26
+ end
27
+ end
28
+
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
+ private
39
+
40
+ def capture string
41
+ [capture_function_method(string), capture_object(string), capture_attribute(string)]
42
+ end
43
+
44
+ def all_captures string
45
+ string.scan(/\{\{(\w*\(?\w*\.?\w*\)?)\}\}/).flatten
46
+ end
47
+
48
+ def capture_function_method string
49
+ match = string.match(/(.*)\(.*\)/)
50
+ match[1] if match
51
+ end
52
+
53
+ def capture_attribute string
54
+ object_attribute(string).last
55
+ end
56
+
57
+ def capture_object string
58
+ object_attribute(string).first
59
+ end
60
+
61
+ def object_attribute string
62
+ string.match(/(\w*\.\w*)/)[1].split('.')
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,3 @@
1
+ module Parsable
2
+ VERSION = "0.0.1"
3
+ end
data/lib/parsable.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "parsable/version"
2
+ require 'parsable/parser'
3
+
4
+ module Parsable
5
+
6
+ def self.crunch args={}
7
+ Parsable::Parser.new(args).crunch
8
+ end
9
+
10
+ end
data/parsable.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'parsable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "parsable"
8
+ spec.version = Parsable::VERSION
9
+ spec.authors = ["Hubert Liu"]
10
+ spec.email = ["hubert.liu@rigor.com"]
11
+ spec.summary = %q{LiquidText style parsing/crunching}
12
+ spec.description = %q{Replace inline variables in strings with their values }
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Parsable do
4
+
5
+ describe '.crunch' do
6
+ it "outputs string with crunched values" do
7
+ location = OpenStruct.new(:name => "Here")
8
+
9
+ output = Parsable.crunch(\
10
+ :string => %(my+{{location.name}}@email.com),
11
+ :context => {:location => location}
12
+ )
13
+
14
+ expect(output).to eql(%(my+Here@email.com))
15
+ end
16
+
17
+ context "no replacements" do
18
+ it "returns the original" do
19
+ string = %(my@email.com)
20
+ output = Parsable.crunch(\
21
+ :string => string,
22
+ :context => {}
23
+ )
24
+
25
+ expect(output).to eql(string)
26
+ end
27
+ end
28
+
29
+ context "method not defined" do
30
+ it "replaces with empty" do
31
+ location = OpenStruct.new(:name => "Here")
32
+
33
+ output = Parsable.crunch(\
34
+ :string => %(my+{{location.not_name}}@email.com),
35
+ :context => {:location => location}
36
+ )
37
+
38
+ expect(output).to eql(%(my+@email.com))
39
+ end
40
+ end
41
+
42
+ context "no context key" do
43
+ it "replaces with empty" do
44
+ location = OpenStruct.new(:name => "Here")
45
+
46
+ output = Parsable.crunch(\
47
+ :string => %(my+{{wrong_key.name}}@email.com),
48
+ :context => {:location => location}
49
+ )
50
+
51
+ expect(output).to eql(%(my+@email.com))
52
+ end
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Parsable::Parser do
4
+
5
+ describe '#parse' do
6
+
7
+ context "when no variables" do
8
+ it "returns empty" do
9
+ parsed = Parsable::Parser.new(:string => "novariables").parse
10
+ expect(parsed).to be_empty
11
+ end
12
+ end
13
+
14
+ context 'when one variable' do
15
+
16
+ context 'no function method' do
17
+ before :each do
18
+ @parsed = Parsable::Parser.new(:string => %(my+{{location.name}}@email.com)).parse.first
19
+ end
20
+ it "parses object name" do
21
+ expect(@parsed[:object]).to eql('location')
22
+ end
23
+
24
+ it "parses attribute" do
25
+ expect(@parsed[:attribute]).to eql('name')
26
+ end
27
+ end
28
+
29
+ context "function method" do
30
+ it "parses function method" do
31
+ string = %(my+{{url_safe(location.name)}}@email.com)
32
+ parsed = Parsable::Parser.new(:string => string).parse.first
33
+ expect(parsed[:function]).to eql('url_safe')
34
+ end
35
+ end
36
+ end
37
+
38
+ context 'when multiple variables' do
39
+ before :each do
40
+ @parsed = Parsable::Parser.new(:string => %(my+{{location.name}}@{{email.domain}}.com)).parse
41
+ end
42
+
43
+ it "returns multiple strings" do
44
+ expect(@parsed.size).to eql(2)
45
+ end
46
+
47
+ context 'no function method' do
48
+ it "parses object names" do
49
+ expect(@parsed.first[:object]).to eql('location')
50
+ expect(@parsed.last[:object]).to eql('email')
51
+ end
52
+
53
+ it "parses attributes" do
54
+ expect(@parsed.first[:attribute]).to eql('name')
55
+ expect(@parsed.last[:attribute]).to eql('domain')
56
+ end
57
+ end
58
+
59
+ context "function method" do
60
+ it "parses function methods" do
61
+ string = %(my+{{url_safe(location.name)}}@{{email.domain}}.com)
62
+ parsed = Parsable::Parser.new(:string => string).parse
63
+
64
+ expect(parsed.first[:function]).to eql('url_safe')
65
+ expect(parsed.last[:function]).to be_nil
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,7 @@
1
+ require "parsable"
2
+ require 'pry'
3
+
4
+ RSpec.configure do |config|
5
+ # Use color in STDOUT
6
+ config.color_enabled = true
7
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parsable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hubert Liu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: 'Replace inline variables in strings with their values '
70
+ email:
71
+ - hubert.liu@rigor.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/parsable.rb
82
+ - lib/parsable/combiner.rb
83
+ - lib/parsable/parser.rb
84
+ - lib/parsable/version.rb
85
+ - parsable.gemspec
86
+ - spec/parsable_spec.rb
87
+ - spec/parser_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.14
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: LiquidText style parsing/crunching
113
+ test_files:
114
+ - spec/parsable_spec.rb
115
+ - spec/parser_spec.rb
116
+ - spec/spec_helper.rb