hash-class 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +36 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +48 -0
- data/hash-class.gemspec +22 -0
- data/lib/hash-class.rb +6 -0
- data/lib/hash-class/assign_if.rb +18 -0
- data/lib/hash-class/version.rb +3 -0
- data/test/test_helper.rb +3 -0
- data/test/unit/test_hash.rb +36 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 051a960b7c5585819d0507b651eb273a3c9a6871
|
4
|
+
data.tar.gz: 480b18535d9a3f226da684c2248c083767ef0f7e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 00b48dc651d6c2b74d15ef82827afb8628bd7152eb28b6be6be7bc05a59fbf46c6a78a7352c1ebc261378d8500df662e699353cdd0327d3087e354b7f3de6f9b
|
7
|
+
data.tar.gz: 52704904df640b55e5696eb8b95f8336a375e9aebaea754a5cf9f8af34078f987a6849170a5b56109f1f4d692357f07c869eda1db45cb8eb33ff6412e7bf0c3d
|
data/.gitignore
ADDED
@@ -0,0 +1,36 @@
|
|
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
|
35
|
+
|
36
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Dennis Blommesteijn
|
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.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
Hash-class will extend the Hash object with `assign_if`. This function reduces the verbosity when conditionaly assigning the value by key (within the basic Hash object).
|
2
|
+
|
3
|
+
**Example:**
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# old
|
7
|
+
a = hash[:some_element] if hash.include?(:some_element)
|
8
|
+
# new
|
9
|
+
hash.assign_if(:some_element) {|e| a = e}
|
10
|
+
```
|
11
|
+
|
12
|
+
**Logic**
|
13
|
+
|
14
|
+
Block `{|e| a = e}` is yielded when key `:some_element` in `hash` is found. `e` contains the value of key `:some_element`.
|
15
|
+
*NOTE: When the key is not found, the block is not yielded!*
|
16
|
+
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
**Add gem to your Gemfile**
|
21
|
+
|
22
|
+
```bash
|
23
|
+
gem 'hash-class', github: 'dblommesteijn/hash-class'
|
24
|
+
```
|
25
|
+
|
26
|
+
**Use as standalone gem**
|
27
|
+
|
28
|
+
```bash
|
29
|
+
gem install specific_install
|
30
|
+
gem specific_install https://github.com/dblommesteijn/hash-class
|
31
|
+
```
|
32
|
+
|
33
|
+
### Code snippet
|
34
|
+
|
35
|
+
```bash
|
36
|
+
require 'hash-class'
|
37
|
+
a = 123
|
38
|
+
hash = {}
|
39
|
+
hash.assign_if(:some_element) {|e| a = e}
|
40
|
+
```
|
41
|
+
|
42
|
+
## Run Tests
|
43
|
+
|
44
|
+
```bash
|
45
|
+
ruby -I test test/unit/test_hash.rb
|
46
|
+
```
|
47
|
+
|
48
|
+
|
data/hash-class.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash-class'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "hash-class"
|
8
|
+
gem.version = HashClass::VERSION
|
9
|
+
gem.authors = ["Dennis Blommesteijn"]
|
10
|
+
gem.email = ["dennis@blommesteijn.com"]
|
11
|
+
gem.description = %q{Extends Ruby's Hash object with assign_if}
|
12
|
+
gem.summary = %q{Extends Ruby's Hash object with assign_if}
|
13
|
+
gem.homepage = "https://github.com/dblommesteijn/hash-class"
|
14
|
+
|
15
|
+
# dependencies
|
16
|
+
gem.add_development_dependency "rake", "> 10.0.0"
|
17
|
+
gem.add_development_dependency "test-unit", "> 3.0.0"
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
20
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
21
|
+
gem.require_paths = ["lib"]
|
22
|
+
end
|
data/lib/hash-class.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module HashClass
|
2
|
+
module AssignIf
|
3
|
+
|
4
|
+
# lookup element and return value if exists
|
5
|
+
def assign_if(*opts, &block)
|
6
|
+
if opts.is_a?(Array)
|
7
|
+
el = opts.first
|
8
|
+
if block.is_a?(Proc)
|
9
|
+
return block.call(self[el]) if self.include?(el)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# attach to Hash
|
18
|
+
Hash.send :include, HashClass::AssignIf
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestHash < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
end
|
7
|
+
|
8
|
+
def teardown
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_assign_if_empty
|
12
|
+
# assert false, "not implemented!"
|
13
|
+
hs = {a: 1, b: 2, c: 3}
|
14
|
+
# block notation
|
15
|
+
a,b,c = nil
|
16
|
+
hs.assign_if(:a) { |v| a = v }
|
17
|
+
assert a == 1
|
18
|
+
hs.assign_if(:b) { |v| b = v }
|
19
|
+
assert b == 2
|
20
|
+
hs.assign_if(:c) { |v| c = v }
|
21
|
+
assert c == 3
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_assign_if_overwrite
|
25
|
+
hs = {a: 1, b: 2, c: 3}
|
26
|
+
# block notation
|
27
|
+
d,e,f = 4,5,6
|
28
|
+
hs.assign_if(:d) { |v| d = v }
|
29
|
+
assert d == 4
|
30
|
+
hs.assign_if(:e) { |v| e = v }
|
31
|
+
assert e == 5
|
32
|
+
hs.assign_if(:f) { |v| f = v }
|
33
|
+
assert f == 6
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash-class
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dennis Blommesteijn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 10.0.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 10.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: test-unit
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
description: Extends Ruby's Hash object with assign_if
|
42
|
+
email:
|
43
|
+
- dennis@blommesteijn.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- hash-class.gemspec
|
53
|
+
- lib/hash-class.rb
|
54
|
+
- lib/hash-class/assign_if.rb
|
55
|
+
- lib/hash-class/version.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
- test/unit/test_hash.rb
|
58
|
+
homepage: https://github.com/dblommesteijn/hash-class
|
59
|
+
licenses: []
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.4.5.1
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Extends Ruby's Hash object with assign_if
|
81
|
+
test_files:
|
82
|
+
- test/test_helper.rb
|
83
|
+
- test/unit/test_hash.rb
|