extended_interpolation_string 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +10 -0
- data/extended_interpolation_string.gemspec +27 -0
- data/lib/extended_interpolation_string.rb +33 -0
- data/lib/extended_interpolation_string/version.rb +3 -0
- data/test/extended_interpolation_string_test.rb +25 -0
- data/test/test_helper.rb +10 -0
- metadata +107 -0
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Tomas Valent
|
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,46 @@
|
|
1
|
+
# ExtendedInterpolationString
|
2
|
+
|
3
|
+
When interpolating string with hash that includes lambda,
|
4
|
+
you can pass interpolator object upon which the lambda is
|
5
|
+
called before interpolating the hash.
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'ostruct'
|
9
|
+
user = OpenStruct.new(email_count: 20)
|
10
|
+
|
11
|
+
hello_string = 'hi %{user}, you have %{email_count} emails'
|
12
|
+
|
13
|
+
hello_string.class # => String
|
14
|
+
hello_string % { user: 'Joe', email_count: lambda { |user| user.email_count} }
|
15
|
+
# => "hi Joe, you have #<Proc:0x000000022a7928@(irb):9 (lambda)> emails"
|
16
|
+
|
17
|
+
# using this gem
|
18
|
+
extended_hello = hello_string.extended
|
19
|
+
extended_hello.class # => ExtendedInterpolationString
|
20
|
+
extended_hello.interpolator = user
|
21
|
+
extended_hello % { user: 'Joe', email_count: lambda { |user| user.email_count} }
|
22
|
+
# "hi Joe, you have 20 emails"
|
23
|
+
```
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
gem 'extended_interpolation_string'
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle
|
34
|
+
|
35
|
+
Or install it yourself as:
|
36
|
+
|
37
|
+
$ gem install extended_interpolation_string
|
38
|
+
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it ( https://github.com/[my-github-username]/extended_interpolation_string/fork )
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'extended_interpolation_string/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "extended_interpolation_string"
|
8
|
+
spec.version = ExtendedInterpolationString::VERSION
|
9
|
+
spec.authors = ["Tomas Valent"]
|
10
|
+
spec.email = ["equivalent@eq8.eu"]
|
11
|
+
spec.summary = %q{Extended string interpolation}
|
12
|
+
spec.description = 'when interpolating string with hash that includes lamda,' +
|
13
|
+
'you can pass interpolator object upon which the lambda is ' +
|
14
|
+
'called before interpolating'
|
15
|
+
spec.homepage = ""
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
spec.required_ruby_version = '>= 1.9.1'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", ">= 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "minitest"
|
27
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require "extended_interpolation_string/version"
|
3
|
+
|
4
|
+
module ExtendedInterpolationString
|
5
|
+
def extended
|
6
|
+
LambdaInterpolationString.new(self)
|
7
|
+
end
|
8
|
+
|
9
|
+
class LambdaInterpolationString < DelegateClass(String)
|
10
|
+
VERSION = "0.0.1"
|
11
|
+
INTP_PATERN = /%\s*{\s*(\w*)\s*}\.*/
|
12
|
+
NoInterpolatorObjectGiven = Class.new(StandardError)
|
13
|
+
|
14
|
+
attr_accessor :interpolator
|
15
|
+
|
16
|
+
def %(options)
|
17
|
+
if options.is_a? Hash
|
18
|
+
__getobj__.scan(INTP_PATERN).flatten.each do |requested_key|
|
19
|
+
|
20
|
+
begin
|
21
|
+
value = options.fetch(requested_key.to_sym)
|
22
|
+
options[requested_key.to_sym] = value
|
23
|
+
.call(interpolator || raise(NoInterpolatorObjectGiven)) if value.respond_to?(:call)
|
24
|
+
rescue KeyError
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
__getobj__ % options
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
String.send :include, ExtendedInterpolationString
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
describe 'ExtendedInterpolationString' do
|
3
|
+
describe '#%' do
|
4
|
+
subject do
|
5
|
+
extended_string = string.extended
|
6
|
+
extended_string.interpolator = interpolator
|
7
|
+
extended_string % options
|
8
|
+
end
|
9
|
+
|
10
|
+
context "mixed lambda and regular interpolation" do
|
11
|
+
let(:string) { "This %{advanced} interpolation %{is} cool" }
|
12
|
+
let(:interpolator) { OpenStruct.new(advanced: "Advanced") }
|
13
|
+
let(:options) do
|
14
|
+
{
|
15
|
+
is: 'IS',
|
16
|
+
advanced: lambda { |interpolator| "so #{interpolator.advanced}" }
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should call lambda upon interpolator before interpolating and interpolate hash' do
|
21
|
+
subject.must_equal "This so Advanced interpolation IS cool"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: extended_interpolation_string
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tomas Valent
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-09-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: minitest
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: when interpolating string with hash that includes lamda,you can pass
|
63
|
+
interpolator object upon which the lambda is called before interpolating
|
64
|
+
email:
|
65
|
+
- equivalent@eq8.eu
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- extended_interpolation_string.gemspec
|
76
|
+
- lib/extended_interpolation_string.rb
|
77
|
+
- lib/extended_interpolation_string/version.rb
|
78
|
+
- test/extended_interpolation_string_test.rb
|
79
|
+
- test/test_helper.rb
|
80
|
+
homepage: ''
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 1.9.1
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.25
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Extended string interpolation
|
105
|
+
test_files:
|
106
|
+
- test/extended_interpolation_string_test.rb
|
107
|
+
- test/test_helper.rb
|