tender_hash 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: 332ad5b937b547b141b7815e24b5f81238b70e31
4
+ data.tar.gz: e77dd0545547f84ec6015f8e4223763346065fef
5
+ SHA512:
6
+ metadata.gz: 6221fe934b6907466657b7caecead851c3b15b7261e4d6d5694170001357052e589df10589083f77e2fca7682403041121ba9cc83ba72a2931192ddb771f0529
7
+ data.tar.gz: 8cd09654f4e1007d1f038473fc991e18f856c38bac3d9d90158c1de01eecaf362b99955056708310b2606f525889bb10746c0a1f57e415f68d1b8dd08278d67a
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ hash_mapper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.1
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tender_hash.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Rodrigo Pavano
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,124 @@
1
+ # TenderHash
2
+
3
+ Map hashes with style.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tender_hash'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tender_hash
18
+
19
+ ## Usage
20
+
21
+ An empty set of rules will filter all keys returning an empty hash.
22
+
23
+ ```ruby
24
+ TenderHash.map( { name: 'Rodrigo', age: 27 } )
25
+ ```
26
+
27
+ The `key` method will cause the mapper to keep the specified key without
28
+ making any changes.
29
+
30
+ ```ruby
31
+ TenderHash.map( { name: 'Rodrigo', age: 27 } ) do
32
+ key :name
33
+ end
34
+
35
+ # => { name: 'Rodrigo' }
36
+ ```
37
+
38
+ The `map_key` methods allows to define mappings for the hash keys.
39
+
40
+ ```ruby
41
+ TenderHash.map( { name: 'Rodrigo', age: 27 } ) do
42
+ map_key :name, :first_name
43
+ end
44
+
45
+ # => { first_name: 'Rodrigo' }
46
+ ```
47
+
48
+ The `key` and `map_key` methods accept a `default` option. If the input hash
49
+ doesn't have the key defined or if it's value is `nil`, then the default value
50
+ will be set for that key on the returned hash.
51
+
52
+ ```ruby
53
+ TenderHash.map( { name: 'Rodrigo', age: 27 } ) do
54
+ map_key :name, :first_name, default: 'John Doe'
55
+ key :height, default: 1.90
56
+ end
57
+
58
+ # => { first_name: 'Rodrigo', height: 1.90 }
59
+ ```
60
+
61
+ The `key` and `map_key` methods also accept a `cast_to` option. The
62
+ possible alternatives are: `:integer`, `:string` and `:boolean`.
63
+
64
+ ```ruby
65
+ TenderHash.map( { name: 'Rodrigo', age: 27, logged_in: 'false' } ) do
66
+ key :age, cast_to: :string
67
+ key :logged_in, cast_to: :boolean
68
+ end
69
+
70
+ # => { age: '27', looged_in: false }
71
+ ```
72
+
73
+ The `scope` method allows to nest a hash within a key.
74
+
75
+ ```ruby
76
+ TenderHash.map( { name: 'Rodrigo', age: 27, logged_in: 'false' } ) do
77
+ scope :personal_info do
78
+ map_key :name, :first_name
79
+ key :age
80
+ end
81
+
82
+ key :logged_in
83
+ end
84
+
85
+ # => {
86
+ # personal_info: {
87
+ # first_name: 'Rodrigo',
88
+ # age: 27
89
+ # },
90
+ # logged_in: 'false'
91
+ # }
92
+ ```
93
+
94
+ A good use case for this gem is to map the `ENV` hash into a more
95
+ readable and usable hash.
96
+
97
+ ```ruby
98
+ TenderHash.map(ENV) do
99
+ map_key 'EMAIL_NOTIFICATIONS', :send_email_notifications, cast_to: :boolean, default: true
100
+ map_key 'MAPS_API_AUTH_TOKEN', :api_auth_token
101
+
102
+ scope :database do
103
+ map_key 'POSTGRES_URL', :url, default: 'localhost'
104
+ map_key 'PORT', :port, cast_to: :integer, default: 5432
105
+ end
106
+ end
107
+
108
+ # => {
109
+ # send_email_notifications: true,
110
+ # api_auth_token: 'super-secret',
111
+ # database: {
112
+ # url: 'localhost',
113
+ # port: 5432
114
+ # }
115
+ # }
116
+ ```
117
+
118
+ ## Contributing
119
+
120
+ 1. Fork it ( https://github.com/rodrei/tender_hash/fork )
121
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
122
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
123
+ 4. Push to the branch (`git push origin my-new-feature`)
124
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,37 @@
1
+ module TenderHash
2
+ class Map
3
+
4
+ def initialize(hash=nil, &block)
5
+ @old_hash = hash || {}
6
+ @rules = []
7
+ instance_exec(&block) if block_given?
8
+ end
9
+
10
+ def source=(hash)
11
+ @old_hash = hash
12
+ end
13
+
14
+ def map_key(old, new, options={})
15
+ @rules << Rule.new(old, new, options)
16
+ end
17
+
18
+ def key(key, options={})
19
+ map_key(key, key, options)
20
+ end
21
+
22
+ def scope(key, &block)
23
+ scope = ScopeRule.new(key, &block)
24
+ @rules << scope
25
+ scope.map
26
+ end
27
+
28
+ def to_h
29
+ {}.tap do |new_hash|
30
+ @rules.each do |rule|
31
+ rule.apply(@old_hash, new_hash)
32
+ end
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,44 @@
1
+ module TenderHash
2
+ class Rule
3
+
4
+ def initialize(old_key, new_key=nil, options={})
5
+ @old_key = old_key
6
+ @new_key = new_key || old_key
7
+ @cast_to = options[:cast_to] && options[:cast_to].to_sym
8
+ @default = options[:default]
9
+ end
10
+
11
+
12
+ def apply(old_hash, new_hash)
13
+ new_value = cast_value(old_hash[@old_key]) unless old_hash[@old_key].nil?
14
+ new_hash[@new_key] = new_value.nil? ? @default : new_value
15
+ end
16
+
17
+ private
18
+
19
+ def cast_value(val)
20
+ case @cast_to
21
+ when :integer
22
+ val.to_i
23
+ when :string
24
+ val.to_s
25
+ when :boolean
26
+ cast_to_boolean(val)
27
+ else
28
+ val
29
+ end
30
+ end
31
+
32
+ def cast_to_boolean(val)
33
+ case val
34
+ when 'true', 1
35
+ true
36
+ when 'false', 0
37
+ false
38
+ else
39
+ val
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,22 @@
1
+ module TenderHash
2
+ class ScopeRule
3
+
4
+ def initialize(key, &block)
5
+ raise ArgumentError unless block_given?
6
+ @key = key
7
+ @block = block
8
+ end
9
+
10
+ def map
11
+ @map ||= Map.new
12
+ end
13
+
14
+ def apply(old, new, options={})
15
+ map.source = old
16
+ map.instance_exec(&@block)
17
+ new[@key] = map.to_h
18
+ end
19
+
20
+ end
21
+ end
22
+
@@ -0,0 +1,3 @@
1
+ module TenderHash
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ require "tender_hash/version"
2
+ require "tender_hash/map"
3
+ require "tender_hash/rule"
4
+ require "tender_hash/scope_rule"
5
+
6
+ module HashMapper
7
+
8
+ def self.map(hash, &block)
9
+ raise ArgumentError unless block_given?
10
+
11
+ Map.new(hash, &block).to_h
12
+ end
13
+
14
+ end
15
+
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'tender_hash'
@@ -0,0 +1,133 @@
1
+ require 'spec_helper'
2
+
3
+ describe TenderHash::Map do
4
+ let(:hash) do { bob: 32, alice: 'cooper', johnny: '13' } end
5
+ let(:mapper) { TenderHash::Map.new(hash) }
6
+
7
+ describe "#to_h" do
8
+ context "given an empty source hash" do
9
+ it "returns an new empty hash" do
10
+ hash = {}
11
+ TenderHash::Map.new(hash).to_h.tap do |new_hash|
12
+ expect( new_hash ).to be_empty
13
+ expect( new_hash ).to_not equal hash
14
+ end
15
+ end
16
+ end
17
+
18
+ context "given no rules are specified" do
19
+ it "returns an empty hash" do
20
+ hash = { hello: 'there' }
21
+ expect( TenderHash::Map.new(hash).to_h ).to be_empty
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "#key" do
27
+ it "keeps the specified key" do
28
+ mapper.key :bob
29
+ expect( mapper.to_h ).to eql( bob: 32 )
30
+ end
31
+
32
+ it "adds a key with nil value if the key didn't exist in the source hash" do
33
+ mapper.key :not_existing
34
+ expect( mapper.to_h ).to eql( not_existing: nil )
35
+ end
36
+
37
+ describe "cast_to option" do
38
+ it "calls to_i on the mapped value if cast_to is set to integer" do
39
+ mapper.key :johnny, cast_to: :integer
40
+ expect( mapper.to_h ).to eql( johnny: 13 )
41
+ end
42
+
43
+ it "calls to_s on the mapped value if cast_to is set to string" do
44
+ mapper.key :bob, cast_to: :string
45
+ expect( mapper.to_h ).to eql( bob: '32' )
46
+ end
47
+
48
+ describe "boolean" do
49
+ it "sets it to true when `true`" do
50
+ mapper.source = { true: 'true' }
51
+ mapper.key :true, cast_to: :boolean
52
+ expect( mapper.to_h ).to eql( true: true )
53
+ end
54
+
55
+ it "sets it to true when equal to 1" do
56
+ mapper.source = { true: 1 }
57
+ mapper.key :true, cast_to: :boolean
58
+ expect( mapper.to_h ).to eql( true: true )
59
+ end
60
+
61
+ it "sets it to false when `false`" do
62
+ mapper.source = { false: 'false' }
63
+ mapper.key :false, cast_to: :boolean
64
+ expect( mapper.to_h ).to eql( false: false )
65
+ end
66
+
67
+ it "sets it to false when equal to 0" do
68
+ mapper.source = { false: 'false' }
69
+ mapper.key :false, cast_to: :boolean
70
+ expect( mapper.to_h ).to eql( false: false )
71
+ end
72
+
73
+ it "doesn't change its value in any other case" do
74
+ value = double
75
+ mapper.source = { double: value }
76
+ mapper.key :double, cast_to: :boolean
77
+ expect( mapper.to_h ).to eql( double: value )
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+ describe "default options" do
84
+ it "disregards the default option when there's a value for the specified key" do
85
+ mapper.key :bob, default: 'hello'
86
+ expect( mapper.to_h ).to eql( bob: 32 )
87
+ end
88
+
89
+ it "sets the default when there's no value for the specified key" do
90
+ mapper.key :new, cast_to: :string, default: "new"
91
+ expect( mapper.to_h ).to eql( new: 'new' )
92
+ end
93
+ end
94
+ end
95
+
96
+ describe "#map_key" do
97
+ it "updates the key on the return hash" do
98
+ mapper.map_key :bob, :BOB
99
+ mapper.map_key :alice, :Alice
100
+ expect( mapper.to_h ).to eql( BOB: 32, Alice: 'cooper' )
101
+ end
102
+ end
103
+
104
+ describe "#scope" do
105
+ it "allows to nest hashes" do
106
+ mapper.scope :subkey do
107
+ map_key :bob, :BOB
108
+ end
109
+
110
+ expect( mapper.to_h ).to eql( subkey: { BOB: 32 })
111
+ end
112
+
113
+ it "allows multiple nesting" do
114
+ mapper.scope :subkey do
115
+ map_key :alice, :ALICE
116
+ scope :nested_subkey do
117
+ key :bob
118
+ end
119
+ end
120
+
121
+ expect( mapper.to_h ).to eql(
122
+ {
123
+ subkey: {
124
+ ALICE: 'cooper',
125
+ nested_subkey: { bob: 32 }
126
+ }
127
+ }
128
+ )
129
+ end
130
+
131
+ end
132
+ end
133
+
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe TenderHash do
4
+ it 'has a version number' do
5
+ expect(TenderHash::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tender_hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tender_hash"
8
+ spec.version = TenderHash::VERSION
9
+ spec.authors = ["Rodrigo Pavano"]
10
+ spec.email = ["rodrigopavano@gmail.com"]
11
+ spec.summary = %q{Utility to map a hash following a set of rules and conditions.}
12
+ spec.description = %q{TenderHash allows you to map hashes with ease. It offers a nice DSL to define the set of rules used do the mapping.}
13
+ spec.homepage = "https://github.com/rodrei/tender_hash"
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.6"
22
+ spec.add_development_dependency "rake", "~> 10.3"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tender_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rodrigo Pavano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-08 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: TenderHash allows you to map hashes with ease. It offers a nice DSL to
56
+ define the set of rules used do the mapping.
57
+ email:
58
+ - rodrigopavano@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".ruby-gemset"
66
+ - ".ruby-version"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - lib/tender_hash.rb
73
+ - lib/tender_hash/map.rb
74
+ - lib/tender_hash/rule.rb
75
+ - lib/tender_hash/scope_rule.rb
76
+ - lib/tender_hash/version.rb
77
+ - spec/spec_helper.rb
78
+ - spec/tender_hash/map_spec.rb
79
+ - spec/tender_hash_spec.rb
80
+ - tender_hash.gemspec
81
+ homepage: https://github.com/rodrei/tender_hash
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.2.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Utility to map a hash following a set of rules and conditions.
105
+ test_files:
106
+ - spec/spec_helper.rb
107
+ - spec/tender_hash/map_spec.rb
108
+ - spec/tender_hash_spec.rb