text-interpolator 1.0.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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDA4MDVlMjkyZTA4NWI3ZWZmMjMyYWZkZmJhZGIxMTkyYzI2N2RiNw==
5
+ data.tar.gz: !binary |-
6
+ NTBhOGFiNzgyZDBmMDZiYTlhNjY3YjQ0YTIxYTNjOWExZmVlYmEzMw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ Y2ZjYjEwOTU4NDFlODYyNWMyNWFiMzljYzAyZDU5MTAzYzM2M2YzOWMwYzFi
10
+ YzFlM2Q3Mjg4OTkyMDU5NWZiMzBjODE0YjNkNzlmNzRiODIxYTg0NzllMjVj
11
+ MmI2ZWMwODAwNDA3N2ZkN2JiODVkMjBkYTAxNjIzNmRmZjdkNTY=
12
+ data.tar.gz: !binary |-
13
+ YWUwZmE3MGIwZmQ5YzQwZWZiMDI3ODBkZTUxZjhlN2UyOWViZjk0NmIzODg5
14
+ MjJhMDFlZjgyN2RmNjY2ZjA0OTU2YTY4ZjNlMDkyMjcyYjUzZTk0ZGY0NjBj
15
+ N2U0NzUxODAyZTljYjRlY2Q2NzNkYmY3NDA1MGMyYTVkYjgwZDE=
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
@@ -0,0 +1 @@
1
+ text_interpolator
@@ -0,0 +1 @@
1
+ 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :default do
4
+
5
+ end
6
+
7
+ group :development do
8
+ gem "gemspec_deps_gen", "~>1.1"
9
+ gem "gemcutter", "~>0.7"
10
+ end
11
+
12
+ group :test do
13
+ gem "rspec"
14
+ gem "mocha"
15
+ gem "json_pure"
16
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Alexander Shvets
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,4 @@
1
+ text-interpolator
2
+ =================
3
+
4
+ Simple library for interpolation of variables inside the text.
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env rake
2
+
3
+ include Rake::DSL
4
+
5
+ $LOAD_PATH.unshift File.expand_path("lib", File.dirname(__FILE__))
6
+
7
+ require "rspec/core/rake_task"
8
+ require "text_interpolator/version"
9
+ require "gemspec_deps_gen/gemspec_deps_gen"
10
+
11
+ version = TextInterpolator::VERSION
12
+ project_name = File.basename(Dir.pwd)
13
+
14
+ task :gen do
15
+ generator = GemspecDepsGen.new
16
+
17
+ generator.generate_dependencies "spec", "#{project_name}.gemspec.erb", "#{project_name}.gemspec"
18
+ end
19
+
20
+ task :build => :gen do
21
+ system "gem build #{project_name}.gemspec"
22
+ end
23
+
24
+ task :install do
25
+ system "gem install #{project_name}-#{version}.gem"
26
+ end
27
+
28
+ task :uninstall do
29
+ system "gem uninstall #{project_name}"
30
+ end
31
+
32
+ task :release => :build do
33
+ system "gem push #{project_name}-#{version}.gem"
34
+ end
35
+
36
+ RSpec::Core::RakeTask.new do |task|
37
+ task.pattern = 'spec/**/*_spec.rb'
38
+ task.verbose = false
39
+ end
40
+
41
+
@@ -0,0 +1 @@
1
+ require 'text_interpolator/text_interpolator'
@@ -0,0 +1,78 @@
1
+ class TextInterpolator
2
+
3
+ def interpolate object, env={}
4
+ if object.kind_of? String
5
+ interpolate_string object, env
6
+ elsif object.kind_of? IO
7
+ interpolate_io object, env
8
+ elsif object.kind_of? Hash
9
+ interpolate_hash object
10
+ else
11
+ object
12
+ end
13
+ end
14
+
15
+ def interpolate_string string, env={}
16
+ string = string.gsub(/\#/, '%') if string.index("#")
17
+
18
+ string = interpolate_env_vars(string) if string.index('ENV[')
19
+
20
+ StringIO.new(string).read % env
21
+ end
22
+
23
+ def interpolate_io io, env={}
24
+ result = ''
25
+
26
+ io.each do |line|
27
+ result += interpolate_string(line, env)
28
+ result += '\n' unless io.eof?
29
+ end
30
+
31
+ result
32
+ end
33
+
34
+ def interpolate_hash hash
35
+ pairs = hash.reduce({}) do |result, value|
36
+ result[value[0]] = value[1]
37
+
38
+ result
39
+ end
40
+
41
+ hash.each do |key, value|
42
+ value = value.gsub(/\#/, '%') if value.index("#")
43
+
44
+ hash[key] = StringIO.new(value).read % pairs
45
+ end
46
+
47
+ hash.each do |key, value|
48
+ new_value = interpolate_env_vars value
49
+
50
+ hash[key] = new_value if new_value
51
+ end
52
+
53
+ hash
54
+ end
55
+
56
+ private
57
+
58
+ def interpolate_env_vars value
59
+ while value.index('ENV[')
60
+ value = interpolate_env_var value
61
+ end
62
+
63
+ value
64
+ end
65
+
66
+ def interpolate_env_var value
67
+ index1 = value.index("ENV[")
68
+ index2 = value.index("]")
69
+
70
+ name = value[index1+5..index2-2]
71
+
72
+ left = (index1 == 0) ? '' : value[0..index1-1]
73
+ right = value[index2+1..-1]
74
+
75
+ left + ENV[name] + right
76
+ end
77
+
78
+ end
@@ -0,0 +1,3 @@
1
+ class TextInterpolator
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
2
+
3
+ RSpec.configure do |config|
4
+ config.mock_with :mocha
5
+ end
6
+
@@ -0,0 +1,79 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ require 'json'
4
+ require 'text_interpolator'
5
+
6
+ describe TextInterpolator do
7
+
8
+ describe "#interpolate_string" do
9
+ it "interpolates string with %{} tokens" do
10
+ env = {name1: 'name1', name2: 'name2'}
11
+
12
+ expect(subject.interpolate_string "%{name1}, %{name2}", env).to eq "name1, name2"
13
+ end
14
+
15
+ it "interpolates string with '#{}' token" do
16
+ env = {name1: 'name1', name2: 'name2'}
17
+
18
+ expect(subject.interpolate_string '#{name1}, #{name2}', env).to eq "name1, name2"
19
+ end
20
+
21
+ it "interpolates string with ENV token" do
22
+ ENV['name1'] = 'name1'
23
+ ENV['name2'] = 'name2'
24
+
25
+ expect(subject.interpolate_string "ENV['name1'], ENV['name2']").to eq "name1, name2"
26
+ end
27
+
28
+ it "interpolates string with ENV token and white space" do
29
+ ENV['name1'] = 'name1'
30
+ ENV['name2'] = 'name2'
31
+
32
+ expect(subject.interpolate_string " ENV['name1'], ENV['name2']").to eq " name1, name2"
33
+ end
34
+
35
+ it "interpolates string with mixed tokens" do
36
+ env = {name1: 'name1', name2: 'name2'}
37
+ ENV['name3'] = 'name3'
38
+
39
+ expect(subject.interpolate_string '#{name1}, #{name2}, ENV[\'name3\']', env).to eq "name1, name2, name3"
40
+ end
41
+ end
42
+
43
+ describe "#interpolate_io" do
44
+ it "interpolates io stream" do
45
+ env = {name1: 'name1', name2: 'name2'}
46
+ ENV['name3'] = 'name3'
47
+
48
+ io = StringIO.new '#{name1}, #{name2}, ENV[\'name3\']'
49
+
50
+ expect(subject.interpolate_io io, env).to eq "name1, name2, name3"
51
+ end
52
+ end
53
+
54
+ describe "#interpolate_hash" do
55
+ it "interpolates hash" do
56
+ config =
57
+ '{' +
58
+ '"user": "ENV[\'USER\']",' +
59
+
60
+ '"oracle_base": "/usr/local/oracle",' +
61
+ '"oracle_version": "11.2.0.4.0",'+
62
+
63
+ '"src_dir": "downloads",' +
64
+ '"dest_dir": "#{oracle_base}/instantclient_11_2",' +
65
+
66
+ '"basic_zip": "#{src_dir}/instantclient-basic-macos.x64-#{oracle_version}.zip"' +
67
+ '}'
68
+
69
+ hash = JSON.parse(config, :symbolize_names => true)
70
+
71
+ result = subject.interpolate_hash hash
72
+
73
+ expect(result[:user]).to eq ENV['USER']
74
+ expect(result[:oracle_base]).to eq '/usr/local/oracle'
75
+ expect(result[:dest_dir]).to eq '/usr/local/oracle/instantclient_11_2'
76
+ expect(result[:basic_zip]).to eq 'downloads/instantclient-basic-macos.x64-11.2.0.4.0.zip'
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/lib/text_interpolator/version')
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "text-interpolator"
7
+ spec.summary = %q{Simple library for interpolation of variables inside the text}
8
+ spec.description = %q{Simple library for interpolation of variables inside the text.}
9
+ spec.email = "alexander.shvets@gmail.com"
10
+ spec.authors = ["Alexander Shvets"]
11
+ spec.homepage = "http://github.com/shvets/text-interpolator"
12
+
13
+ spec.files = `git ls-files`.split($\)
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+ spec.version = TextInterpolator::VERSION
17
+ spec.license = "MIT"
18
+
19
+
20
+ spec.add_development_dependency "gemspec_deps_gen", ["~> 1.1"]
21
+ spec.add_development_dependency "gemcutter", ["~> 0.7"]
22
+
23
+ end
24
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/lib/text_interpolator/version')
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "text-interpolator"
7
+ spec.summary = %q{Simple library for interpolation of variables inside the text}
8
+ spec.description = %q{Simple library for interpolation of variables inside the text.}
9
+ spec.email = "alexander.shvets@gmail.com"
10
+ spec.authors = ["Alexander Shvets"]
11
+ spec.homepage = "http://github.com/shvets/text-interpolator"
12
+
13
+ spec.files = `git ls-files`.split($\)
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+ spec.version = TextInterpolator::VERSION
17
+ spec.license = "MIT"
18
+
19
+ <%= project_dependencies %>
20
+ end
21
+
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: text-interpolator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Shvets
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gemspec_deps_gen
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gemcutter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ description: Simple library for interpolation of variables inside the text.
42
+ email: alexander.shvets@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - .gitignore
48
+ - .ruby-gemset
49
+ - .ruby-version
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - lib/text_interpolator.rb
55
+ - lib/text_interpolator/text_interpolator.rb
56
+ - lib/text_interpolator/version.rb
57
+ - spec/spec_helper.rb
58
+ - spec/text_interpolator_spec.rb
59
+ - text-interpolator.gemspec
60
+ - text-interpolator.gemspec.erb
61
+ homepage: http://github.com/shvets/text-interpolator
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Simple library for interpolation of variables inside the text
85
+ test_files:
86
+ - spec/spec_helper.rb
87
+ - spec/text_interpolator_spec.rb