hash_plus 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/README.md +23 -0
- data/hash_plus.gemspec +19 -0
- data/lib/hash_plus.rb +18 -0
- data/spec/hash_plus_spec.rb +27 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b9267cb1a1e240b821d6ae0dd0bebd992cd0b33a
|
4
|
+
data.tar.gz: c7dcdd02b364543613e93f48dad90f2463f3a1cc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ba42169256a8a06cc34d695f6c40768223b68f26f96f82d990912839bb41a9d63f62cedacbebf213e747f1466fe787063f8f65c6f0e00c172f05b9cf6c1cceff
|
7
|
+
data.tar.gz: 0a6bb6ac0776161ee41eefbce80e932964bf66aca74a9c6e547510b9a930820a291d67af150003e0c7220055917c2b544f58eb794455b7895fe71bfd627994b3
|
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
hash_plus
|
2
|
+
=========
|
3
|
+
|
4
|
+
Extensions to the Ruby Hash class
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
|
8
|
+
my_vehicle = {
|
9
|
+
:make => 'Subaru',
|
10
|
+
:model => 'Forester',
|
11
|
+
:year => 2525,
|
12
|
+
}
|
13
|
+
|
14
|
+
my_vehicle.requires_fields(:make, :model) # No errors
|
15
|
+
|
16
|
+
my_vehicle.requires_fields(:mileage) # Raises Argument error
|
17
|
+
|
18
|
+
my_vehicle[:mileage] = nil
|
19
|
+
my_vehicle.requires_fields(:mileage) # Raises Argument error
|
20
|
+
|
21
|
+
my_vehicle[:mileage] = 40_000
|
22
|
+
my_vehicle.requires_fields(:mileage) # No errors
|
23
|
+
```
|
data/hash_plus.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "hash_plus"
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "hash_plus"
|
7
|
+
gem.version = '1.0'
|
8
|
+
gem.authors = ["Glenn Nagel"]
|
9
|
+
gem.email = ["glenn@mercury-wireless.com"]
|
10
|
+
gem.homepage = "https://github.com/gnagel/hash_plus"
|
11
|
+
gem.summary = %q{Handy extensions to the Ruby Hash class}
|
12
|
+
gem.description = %q{Extensions to the Hash class for checking form input}
|
13
|
+
gem.license = 'MIT'
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib", "tasks"]
|
19
|
+
end
|
data/lib/hash_plus.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
class Hash
|
2
|
+
# Short hand methods
|
3
|
+
def requires(*cols); requires_fields(cols); end
|
4
|
+
def missing(*cols); missing_fields(cols); end
|
5
|
+
|
6
|
+
# Raise an ArgumentError if there are missing columns
|
7
|
+
def requires_fields(*cols)
|
8
|
+
missing = missing_fields(cols)
|
9
|
+
unless missing.empty?
|
10
|
+
raise ArgumentError, "Missing value for keys=#{missing.join(',')} in opts=#{self.inspect}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Return the missing columns
|
15
|
+
def missing_fields(*cols)
|
16
|
+
cols.select { |required_key| self[required_key].nil? }
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rspec'
|
3
|
+
require 'rspec-expectations'
|
4
|
+
|
5
|
+
|
6
|
+
$:.push File.expand_path("../lib", File.dirname(__FILE__))
|
7
|
+
require 'hash_plus'
|
8
|
+
|
9
|
+
describe Hash do
|
10
|
+
context "empty hash" do
|
11
|
+
subject(:hash) { Hash.new }
|
12
|
+
it { hash.missing_fields(:a).first.should === :a }
|
13
|
+
it { hash.missing_fields(:a, :b, :c).should =~ [:a, :b, :c] }
|
14
|
+
|
15
|
+
it { expect { hash.requires_fields(:a) }.to raise_error ArgumentError, "Missing value for keys=a in opts={}" }
|
16
|
+
end
|
17
|
+
|
18
|
+
context "hash containing values" do
|
19
|
+
subject(:hash) { {:a => 'b', :c => 'd'} }
|
20
|
+
it { hash.missing_fields(:a).should be_empty }
|
21
|
+
it { hash.missing_fields(:a, :c).should be_empty }
|
22
|
+
it { hash.missing_fields(:a, :b, :c).should =~ [:b] }
|
23
|
+
|
24
|
+
it { expect { hash.requires_fields(:b) }.to raise_error ArgumentError, "Missing value for keys=b in opts=#{hash.inspect}" }
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_plus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Glenn Nagel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Extensions to the Hash class for checking form input
|
14
|
+
email:
|
15
|
+
- glenn@mercury-wireless.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- README.md
|
22
|
+
- hash_plus.gemspec
|
23
|
+
- lib/hash_plus.rb
|
24
|
+
- spec/hash_plus_spec.rb
|
25
|
+
homepage: https://github.com/gnagel/hash_plus
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
- tasks
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.0.3
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Handy extensions to the Ruby Hash class
|
50
|
+
test_files:
|
51
|
+
- spec/hash_plus_spec.rb
|