rego-ruby-ext 0.0.6 → 0.0.7
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 +15 -0
- data/.document +0 -0
- data/.rspec +0 -0
- data/Gemfile +14 -14
- data/LICENSE.txt +20 -20
- data/README.md +0 -0
- data/Rakefile +49 -49
- data/VERSION +1 -1
- data/init.rb +2 -2
- data/lib/boolean-ext.rb +11 -11
- data/lib/date-ext.rb +4 -4
- data/lib/enumerable-ext.rb +31 -31
- data/lib/nil-ext.rb +15 -15
- data/lib/numeric-ext.rb +5 -5
- data/lib/rego-ruby-ext.rb +4 -4
- data/lib/string-ext.rb +37 -37
- data/lib/string-interpolation.rb +50 -50
- data/lib/symbol-ext.rb +3 -3
- data/lib/time-ext.rb +7 -7
- data/rego-ruby-ext.gemspec +82 -83
- data/spec/boolean_spec.rb +18 -18
- data/spec/date_spec.rb +7 -7
- data/spec/enumerable_spec.rb +51 -51
- data/spec/nil_spec.rb +17 -17
- data/spec/numeric_spec.rb +7 -7
- data/spec/rego-ruby-ext_spec.rb +5 -5
- data/spec/spec_helper.rb +13 -13
- data/spec/string_interpolation_spec.rb +31 -31
- data/spec/string_spec.rb +39 -39
- data/spec/symbol_spec.rb +8 -8
- data/spec/time_spec.rb +7 -7
- metadata +84 -111
- data/lib/hash-ext.rb +0 -39
- data/spec/hash_spec.rb +0 -30
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MTJlODQwNmY3MDBkYjA2M2YwN2VlMjZjMjJlOTQ5NzliOTQ4NTgxYw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ODcyMGE1MWY0ZWI1YmVjMGYyMWQ0YzBmZGExODcxNGRmNzgwYjY5OA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NTJiMjg5NDAwYTRlNzZmOTA5NTAxODI4OWIwNDQxMzZjOGQwM2MzZDcwMDEw
|
10
|
+
NWI0NzMzZjkyNzU5NTgxYmRhNWI3OGJmYTZlZWQ0YjNiZWNkNWU3YzNiZTRh
|
11
|
+
NTA5Y2JkMzM2YWY2OWU4Y2Q2N2UxZDNmY2MwMGVmYTk1YTQ3Nzk=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YmFmYmY1ZjEzMjgzNDhjNTE0ZGUxZDgxMjAzOTUyZTgyM2MyOTJkMjcxZTM3
|
14
|
+
MmEwNDA1ZDMwNTEyYTE5NjBjMGIzMTg0ODNjMGNkNjQyY2FiMWNlNmQzOTFl
|
15
|
+
Y2YwYzQzY2I5ODU2NTYyZTIwM2RkNmZjZGRkMmRhZDM2OTRlZTY=
|
data/.document
CHANGED
File without changes
|
data/.rspec
CHANGED
File without changes
|
data/Gemfile
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
source "http://rubygems.org"
|
2
|
-
# Add dependencies required to use your gem here.
|
3
|
-
# Example:
|
4
|
-
# gem "activesupport", ">= 2.3.5"
|
5
|
-
|
6
|
-
# Add dependencies to develop your gem here.
|
7
|
-
# Include everything needed to run rake, tests, features, etc.
|
8
|
-
group :development do
|
9
|
-
gem "rdoc"
|
10
|
-
gem "rspec"
|
11
|
-
gem "bundler"
|
12
|
-
gem "jeweler", "~> 1.
|
13
|
-
gem "rcov", ">= 0"
|
14
|
-
end
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rdoc"
|
10
|
+
gem "rspec"
|
11
|
+
gem "bundler"
|
12
|
+
gem "jeweler", "~> 1.8.8"
|
13
|
+
gem "rcov", ">= 0"
|
14
|
+
end
|
data/LICENSE.txt
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
Copyright (c) 2011 Alex Tkachev
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1
|
+
Copyright (c) 2011 Alex Tkachev
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
File without changes
|
data/Rakefile
CHANGED
@@ -1,49 +1,49 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'bundler'
|
5
|
-
begin
|
6
|
-
Bundler.setup(:default, :development)
|
7
|
-
rescue Bundler::BundlerError => e
|
8
|
-
$stderr.puts e.message
|
9
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
-
exit e.status_code
|
11
|
-
end
|
12
|
-
require 'rake'
|
13
|
-
|
14
|
-
require 'jeweler'
|
15
|
-
Jeweler::Tasks.new do |gem|
|
16
|
-
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
-
gem.name = "rego-ruby-ext"
|
18
|
-
gem.homepage = "http://github.com/alextk/rego-ruby-ext"
|
19
|
-
gem.license = "MIT"
|
20
|
-
gem.summary = %Q{Some basic ruby core classes extensions with tests}
|
21
|
-
gem.description = %Q{Ruby core extensions that are common for all ReGO projects}
|
22
|
-
gem.email = "tkachev.alex@gmail.com"
|
23
|
-
gem.authors = ["Alex Tkachev"]
|
24
|
-
# dependencies defined in Gemfile
|
25
|
-
end
|
26
|
-
Jeweler::RubygemsDotOrgTasks.new
|
27
|
-
|
28
|
-
require 'rspec/core'
|
29
|
-
require 'rspec/core/rake_task'
|
30
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
-
end
|
33
|
-
|
34
|
-
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
-
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
-
spec.rcov = true
|
37
|
-
end
|
38
|
-
|
39
|
-
task :default => :spec
|
40
|
-
|
41
|
-
require 'rdoc/task'
|
42
|
-
RDoc::Task.new do |rdoc|
|
43
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
-
|
45
|
-
rdoc.rdoc_dir = 'rdoc'
|
46
|
-
rdoc.title = "rego-ruby-ext #{version}"
|
47
|
-
rdoc.rdoc_files.include('README*')
|
48
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "rego-ruby-ext"
|
18
|
+
gem.homepage = "http://github.com/alextk/rego-ruby-ext"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Some basic ruby core classes extensions with tests}
|
21
|
+
gem.description = %Q{Ruby core extensions that are common for all ReGO projects}
|
22
|
+
gem.email = "tkachev.alex@gmail.com"
|
23
|
+
gem.authors = ["Alex Tkachev"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
RDoc::Task.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "rego-ruby-ext #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/init.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "lib", "rego-ruby-ext")
|
2
|
-
|
1
|
+
require File.join(File.dirname(__FILE__), "lib", "rego-ruby-ext")
|
2
|
+
|
data/lib/boolean-ext.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
#hash that true.is_a?(Boolean) and false.is_a?(Boolean) would work
|
2
|
-
module Boolean
|
3
|
-
end
|
4
|
-
|
5
|
-
class TrueClass
|
6
|
-
include Boolean
|
7
|
-
end
|
8
|
-
|
9
|
-
class FalseClass
|
10
|
-
include Boolean
|
11
|
-
end
|
1
|
+
#hash that true.is_a?(Boolean) and false.is_a?(Boolean) would work
|
2
|
+
module Boolean
|
3
|
+
end
|
4
|
+
|
5
|
+
class TrueClass
|
6
|
+
include Boolean
|
7
|
+
end
|
8
|
+
|
9
|
+
class FalseClass
|
10
|
+
include Boolean
|
11
|
+
end
|
data/lib/date-ext.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
class Date
|
2
|
-
# Adds empty? as instance methods to Date.
|
3
|
-
def empty?; false; end
|
4
|
-
end
|
1
|
+
class Date
|
2
|
+
# Adds empty? as instance methods to Date.
|
3
|
+
def empty?; false; end
|
4
|
+
end
|
data/lib/enumerable-ext.rb
CHANGED
@@ -1,31 +1,31 @@
|
|
1
|
-
module Enumerable
|
2
|
-
|
3
|
-
# Add dynamic attribute-based selectors.
|
4
|
-
# Ex:
|
5
|
-
# [{:name=>'Guy',:last_name=>'Shmuely'},
|
6
|
-
# {:name=>'Jack',:last_name=>'Sperow'}].find_by_name_and_last_name('Guy','Shmuely')
|
7
|
-
# => [{:name=>'Guy',:last_name=>'Shmuely'}]
|
8
|
-
# [{:name=>'Guy',:last_name=>'Shmuely',:age=>25},
|
9
|
-
# {:name=>'Jack',:last_name=>'Sperow'},
|
10
|
-
# {:name=>'Roobin'}].collect_name_and_last_name
|
11
|
-
# => [['Guy','Shmuely'],['Jack','Sperow'],['Roobin',nil]]
|
12
|
-
def method_missing(method_id, *arguments)
|
13
|
-
if match = /collect_([_a-zA-Z]\w*)/.match(method_id.to_s)
|
14
|
-
attributes = match.captures.last.split('_and_')
|
15
|
-
collection = self.collect{|array| attributes.collect{|attr| (array.is_a?(Array) || array.is_a?(Hash)) ? array[attr.to_sym] : array.send(attr.to_sym)}}
|
16
|
-
collection.flatten! if attributes.size == 1
|
17
|
-
collection
|
18
|
-
elsif match = /select_by_([_a-zA-Z]\w*)/.match(method_id.to_s)
|
19
|
-
attribute_names = match.captures.last.split('_and_')
|
20
|
-
select do |element|
|
21
|
-
flag=true
|
22
|
-
attribute_names.each_with_index { |name, idx| value=(element.is_a?(Array) || element.is_a?(Hash)) ? element[name.to_sym] : element.send(name)
|
23
|
-
flag=false unless value==arguments[idx] }
|
24
|
-
flag
|
25
|
-
end
|
26
|
-
else
|
27
|
-
super
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
1
|
+
module Enumerable
|
2
|
+
|
3
|
+
# Add dynamic attribute-based selectors.
|
4
|
+
# Ex:
|
5
|
+
# [{:name=>'Guy',:last_name=>'Shmuely'},
|
6
|
+
# {:name=>'Jack',:last_name=>'Sperow'}].find_by_name_and_last_name('Guy','Shmuely')
|
7
|
+
# => [{:name=>'Guy',:last_name=>'Shmuely'}]
|
8
|
+
# [{:name=>'Guy',:last_name=>'Shmuely',:age=>25},
|
9
|
+
# {:name=>'Jack',:last_name=>'Sperow'},
|
10
|
+
# {:name=>'Roobin'}].collect_name_and_last_name
|
11
|
+
# => [['Guy','Shmuely'],['Jack','Sperow'],['Roobin',nil]]
|
12
|
+
def method_missing(method_id, *arguments)
|
13
|
+
if match = /collect_([_a-zA-Z]\w*)/.match(method_id.to_s)
|
14
|
+
attributes = match.captures.last.split('_and_')
|
15
|
+
collection = self.collect{|array| attributes.collect{|attr| (array.is_a?(Array) || array.is_a?(Hash)) ? array[attr.to_sym] : array.send(attr.to_sym)}}
|
16
|
+
collection.flatten! if attributes.size == 1
|
17
|
+
collection
|
18
|
+
elsif match = /select_by_([_a-zA-Z]\w*)/.match(method_id.to_s)
|
19
|
+
attribute_names = match.captures.last.split('_and_')
|
20
|
+
select do |element|
|
21
|
+
flag=true
|
22
|
+
attribute_names.each_with_index { |name, idx| value=(element.is_a?(Array) || element.is_a?(Hash)) ? element[name.to_sym] : element.send(name)
|
23
|
+
flag=false unless value==arguments[idx] }
|
24
|
+
flag
|
25
|
+
end
|
26
|
+
else
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/lib/nil-ext.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
# Adds several instance methods to NilClass.
|
2
|
-
class NilClass
|
3
|
-
# Ex: nil.empty? => true
|
4
|
-
def empty?; true; end
|
5
|
-
def blank?; true; end
|
6
|
-
|
7
|
-
def any?; false; end
|
8
|
-
def include?(*args); false; end
|
9
|
-
|
10
|
-
def strip; nil; end
|
11
|
-
def truncate; nil; end
|
12
|
-
|
13
|
-
#DO NOT override to_sym this since it breaks globalize
|
14
|
-
#def to_sym; ""; end
|
15
|
-
end
|
1
|
+
# Adds several instance methods to NilClass.
|
2
|
+
class NilClass
|
3
|
+
# Ex: nil.empty? => true
|
4
|
+
def empty?; true; end
|
5
|
+
def blank?; true; end
|
6
|
+
|
7
|
+
def any?; false; end
|
8
|
+
def include?(*args); false; end
|
9
|
+
|
10
|
+
def strip; nil; end
|
11
|
+
def truncate; nil; end
|
12
|
+
|
13
|
+
#DO NOT override to_sym this since it breaks globalize
|
14
|
+
#def to_sym; ""; end
|
15
|
+
end
|
data/lib/numeric-ext.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
class Numeric
|
2
|
-
# Adds empty? as instance methods to Numric.
|
3
|
-
# Ex: 5.empty? => false
|
4
|
-
def empty?; false; end
|
5
|
-
end
|
1
|
+
class Numeric
|
2
|
+
# Adds empty? as instance methods to Numric.
|
3
|
+
# Ex: 5.empty? => false
|
4
|
+
def empty?; false; end
|
5
|
+
end
|
data/lib/rego-ruby-ext.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
['nil-ext', 'boolean-ext', 'date-ext', 'time-ext', 'enumerable-ext', '
|
2
|
-
require File.join(File.dirname(__FILE__), file_name)
|
3
|
-
end
|
4
|
-
|
1
|
+
['nil-ext', 'boolean-ext', 'date-ext', 'time-ext', 'enumerable-ext', 'numeric-ext', 'symbol-ext', 'string-interpolation', 'string-ext'].each do |file_name|
|
2
|
+
require File.join(File.dirname(__FILE__), file_name)
|
3
|
+
end
|
4
|
+
|
data/lib/string-ext.rb
CHANGED
@@ -1,37 +1,37 @@
|
|
1
|
-
class String
|
2
|
-
|
3
|
-
# returns a hash like params containing all the "get" params from a given url
|
4
|
-
# Ex:
|
5
|
-
# 'http://wiki.rego.co.il/doku.php?id=development:horizon3:plugins:core_extensions:start&do=edit&rev='.to_params
|
6
|
-
# => {:id=>'development:horizon3:plugins:core_extensions:start', :do=>'edit', :rev=>nil}
|
7
|
-
def to_params
|
8
|
-
hash = {}
|
9
|
-
params=self.split("?")
|
10
|
-
if params.size > 1
|
11
|
-
params=params[1].split("&")
|
12
|
-
params=params.collect{|param| param.split("=")}
|
13
|
-
params.each do |param|
|
14
|
-
hash[param[0].to_sym]=param[1]
|
15
|
-
end
|
16
|
-
end
|
17
|
-
hash
|
18
|
-
end
|
19
|
-
|
20
|
-
# converts string to either TrueClass or FalseClass. If converion can't be made, returns nil
|
21
|
-
def to_boolean
|
22
|
-
s = self.downcase.strip
|
23
|
-
if s == 'true'
|
24
|
-
true
|
25
|
-
elsif s == 'false'
|
26
|
-
false
|
27
|
-
else
|
28
|
-
nil
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# matches and replaces placeholders in form of %{foo} or %<foo>
|
33
|
-
def interpolate(values_hash = {}, options = {})
|
34
|
-
StringInterpolation.interpolate(self, values_hash, options)
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
1
|
+
class String
|
2
|
+
|
3
|
+
# returns a hash like params containing all the "get" params from a given url
|
4
|
+
# Ex:
|
5
|
+
# 'http://wiki.rego.co.il/doku.php?id=development:horizon3:plugins:core_extensions:start&do=edit&rev='.to_params
|
6
|
+
# => {:id=>'development:horizon3:plugins:core_extensions:start', :do=>'edit', :rev=>nil}
|
7
|
+
def to_params
|
8
|
+
hash = {}
|
9
|
+
params=self.split("?")
|
10
|
+
if params.size > 1
|
11
|
+
params=params[1].split("&")
|
12
|
+
params=params.collect{|param| param.split("=")}
|
13
|
+
params.each do |param|
|
14
|
+
hash[param[0].to_sym]=param[1]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
hash
|
18
|
+
end
|
19
|
+
|
20
|
+
# converts string to either TrueClass or FalseClass. If converion can't be made, returns nil
|
21
|
+
def to_boolean
|
22
|
+
s = self.downcase.strip
|
23
|
+
if s == 'true'
|
24
|
+
true
|
25
|
+
elsif s == 'false'
|
26
|
+
false
|
27
|
+
else
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# matches and replaces placeholders in form of %{foo} or %<foo>
|
33
|
+
def interpolate(values_hash = {}, options = {})
|
34
|
+
StringInterpolation.interpolate(self, values_hash, options)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/lib/string-interpolation.rb
CHANGED
@@ -1,50 +1,50 @@
|
|
1
|
-
# heavily based on Masao Mutoh's gettext String interpolation extension
|
2
|
-
# http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb
|
3
|
-
|
4
|
-
module StringInterpolation
|
5
|
-
INTERPOLATION_PATTERN = Regexp.union(
|
6
|
-
/%%/,
|
7
|
-
/%\{(\w+)\}/, # matches placeholders like "%{foo}"
|
8
|
-
/%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/ # matches placeholders like "%<foo>.d"
|
9
|
-
)
|
10
|
-
|
11
|
-
# define module methods (static methods, equivalent of writing def self.interpolate ... end)
|
12
|
-
class << self
|
13
|
-
|
14
|
-
# Replace variables (defined in sprintf syntax) in given <i>string</i> with values from <i>variables</i> hash.
|
15
|
-
#
|
16
|
-
# If variable value is not found there are 3 possible strategies (configurable via <i>:value_not_found</i> in third <i>options</i> argument):
|
17
|
-
# * :raise - raise argument error
|
18
|
-
# * :ignore - ignore the variable in string (leave as is, do not replace) (DEFAULT)
|
19
|
-
# * :hide - replace the variable in string with empty string
|
20
|
-
def interpolate(string, values, options = {})
|
21
|
-
options[:not_found_strategy] ||= :ignore
|
22
|
-
raise ArgumentError.new('Interpolation values must be a Hash.') unless values.kind_of?(Hash)
|
23
|
-
interpolate_hash(string, values, options)
|
24
|
-
end
|
25
|
-
|
26
|
-
def interpolate_hash(string, values, options)
|
27
|
-
string.gsub(INTERPOLATION_PATTERN) do |match|
|
28
|
-
if match == '%%'
|
29
|
-
'%'
|
30
|
-
else
|
31
|
-
key = ($1 || $2).to_sym
|
32
|
-
if values.key?(key)
|
33
|
-
value = values[key]
|
34
|
-
value = value.call(values) if value.respond_to?(:call)
|
35
|
-
$3 ? sprintf("%#{$3}", value) : value
|
36
|
-
else
|
37
|
-
if options[:not_found_strategy] == :raise
|
38
|
-
raise ArgumentError.new("missing interpolation argument #{key} in #{string.inspect}. Given values are: (#{values.inspect})")
|
39
|
-
elsif options[:not_found_strategy] == :hide
|
40
|
-
value = ''
|
41
|
-
else
|
42
|
-
value = $&
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
end
|
1
|
+
# heavily based on Masao Mutoh's gettext String interpolation extension
|
2
|
+
# http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb
|
3
|
+
|
4
|
+
module StringInterpolation
|
5
|
+
INTERPOLATION_PATTERN = Regexp.union(
|
6
|
+
/%%/,
|
7
|
+
/%\{(\w+)\}/, # matches placeholders like "%{foo}"
|
8
|
+
/%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/ # matches placeholders like "%<foo>.d"
|
9
|
+
)
|
10
|
+
|
11
|
+
# define module methods (static methods, equivalent of writing def self.interpolate ... end)
|
12
|
+
class << self
|
13
|
+
|
14
|
+
# Replace variables (defined in sprintf syntax) in given <i>string</i> with values from <i>variables</i> hash.
|
15
|
+
#
|
16
|
+
# If variable value is not found there are 3 possible strategies (configurable via <i>:value_not_found</i> in third <i>options</i> argument):
|
17
|
+
# * :raise - raise argument error
|
18
|
+
# * :ignore - ignore the variable in string (leave as is, do not replace) (DEFAULT)
|
19
|
+
# * :hide - replace the variable in string with empty string
|
20
|
+
def interpolate(string, values, options = {})
|
21
|
+
options[:not_found_strategy] ||= :ignore
|
22
|
+
raise ArgumentError.new('Interpolation values must be a Hash.') unless values.kind_of?(Hash)
|
23
|
+
interpolate_hash(string, values, options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def interpolate_hash(string, values, options)
|
27
|
+
string.gsub(INTERPOLATION_PATTERN) do |match|
|
28
|
+
if match == '%%'
|
29
|
+
'%'
|
30
|
+
else
|
31
|
+
key = ($1 || $2).to_sym
|
32
|
+
if values.key?(key)
|
33
|
+
value = values[key]
|
34
|
+
value = value.call(values) if value.respond_to?(:call)
|
35
|
+
$3 ? sprintf("%#{$3}", value) : value
|
36
|
+
else
|
37
|
+
if options[:not_found_strategy] == :raise
|
38
|
+
raise ArgumentError.new("missing interpolation argument #{key} in #{string.inspect}. Given values are: (#{values.inspect})")
|
39
|
+
elsif options[:not_found_strategy] == :hide
|
40
|
+
value = ''
|
41
|
+
else
|
42
|
+
value = $&
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|