hashmake 0.1.0 → 0.1.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.
- data/ChangeLog.rdoc +5 -0
- data/README.md +5 -1
- data/README.rdoc +5 -1
- data/hashmake.gemspec +1 -0
- data/lib/hashmake.rb +1 -1
- data/lib/hashmake/arg_spec.rb +59 -0
- data/lib/hashmake/hash_makeable.rb +15 -2
- data/lib/hashmake/version.rb +5 -1
- data/spec/{hashed_arg_spec.rb → arg_spec_spec.rb} +3 -3
- data/spec/hash_makeable_spec.rb +2 -2
- metadata +23 -7
- data/lib/hashmake/hashed_arg.rb +0 -31
data/ChangeLog.rdoc
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
hashmake
|
2
2
|
========
|
3
3
|
|
4
|
-
Like making your ruby objects with hashed args? This lib aims to make the process easier and more powerful
|
4
|
+
Like making your ruby objects with hashed args? This lib aims to make the process easier and more powerful.
|
5
|
+
|
6
|
+
Provides hash_make method that can consider parameter name, type, default value, validation, and requiredd/not, according to the specification provided.
|
7
|
+
|
8
|
+
Also, by default assigns to instance variable of same name as parameter.
|
data/README.rdoc
CHANGED
@@ -6,7 +6,11 @@
|
|
6
6
|
|
7
7
|
== Description
|
8
8
|
|
9
|
-
Make hash-based object initialization easy!
|
9
|
+
Make hash-based object initialization easy!
|
10
|
+
|
11
|
+
Provides hash_make method that can consider parameter name, type, default value, validation, and requiredd/not, according to the specification provided.
|
12
|
+
|
13
|
+
Also, by default assigns to instance variable of same name as parameter.
|
10
14
|
|
11
15
|
== Features
|
12
16
|
|
data/hashmake.gemspec
CHANGED
data/lib/hashmake.rb
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
module Hashmake
|
2
|
+
|
3
|
+
# Provides a specification of how a hashed arg is to be processed by the
|
4
|
+
# hash_make method.
|
5
|
+
#
|
6
|
+
# @author James Tunnell
|
7
|
+
#
|
8
|
+
class ArgSpec
|
9
|
+
|
10
|
+
# Defines default key/value pairs to use in initializing an instance.
|
11
|
+
DEFAULT_ARGS = {
|
12
|
+
:reqd => true,
|
13
|
+
:validator => ->(a){true},
|
14
|
+
:array => false,
|
15
|
+
}
|
16
|
+
|
17
|
+
attr_reader :key, :type, :validator, :reqd, :default, :array
|
18
|
+
|
19
|
+
# A new instance of ArgSpec.
|
20
|
+
#
|
21
|
+
# @param [Hash] hashed_args Hash to use in initializing an instance. Required
|
22
|
+
# keys are :key and :type. Optional keys are :reqd,
|
23
|
+
# :validator, :array, and :default.
|
24
|
+
# :key => the key used to identify a hashed arg.
|
25
|
+
# :type => the type of object expected to be paired
|
26
|
+
# with the key.
|
27
|
+
# :reqd => If true, the arg key must be in the hash
|
28
|
+
# passed to #initialize. If false, then a
|
29
|
+
# default must be specified with :default
|
30
|
+
# key. Set to true by default.
|
31
|
+
# :default => If reqd is false, this must be specified.
|
32
|
+
# This can be any object reference. If it
|
33
|
+
# is a Proc, then the Proc will be called,
|
34
|
+
# expecting it to produce the default.
|
35
|
+
# :validator => a Proc used to check the validity of
|
36
|
+
# whatever value is paired with an arg key.
|
37
|
+
# :array => indicates the arg key will be paired with
|
38
|
+
# an array containing objects of the type
|
39
|
+
# specified by :type.
|
40
|
+
def initialize hashed_args
|
41
|
+
new_args = DEFAULT_ARGS.merge(hashed_args)
|
42
|
+
|
43
|
+
@key = new_args[:key]
|
44
|
+
@type = new_args[:type]
|
45
|
+
raise ArgumentError, "args[:type] #{@type} is not a Class" unless @type.is_a?(Class)
|
46
|
+
|
47
|
+
@validator = new_args[:validator]
|
48
|
+
@reqd = new_args[:reqd]
|
49
|
+
@array = new_args[:array]
|
50
|
+
|
51
|
+
unless @reqd
|
52
|
+
msg = "if hashed arg is not required, a default value or value generator (proc) must be defined via :default key"
|
53
|
+
raise ArgumentError, msg unless new_args.has_key?(:default)
|
54
|
+
@default = new_args[:default]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -7,9 +7,20 @@ module HashMakeable
|
|
7
7
|
base.extend(ClassMethods)
|
8
8
|
end
|
9
9
|
|
10
|
+
# Process a hash that contains 'hashed args'. Each hashed arg is intended to
|
11
|
+
# be used in initializing an object instance.
|
12
|
+
#
|
13
|
+
# @param [Enumerable] arg_specs An enumerable of ArgSpec objects. Each object
|
14
|
+
# details an arg key that might be expected in the
|
15
|
+
# args hash.
|
16
|
+
# @param [Hash] hashed_args A hash that should contain at least all the required
|
17
|
+
# keys and valid values, according to the arg_specs passed in.
|
18
|
+
# Nonrequired keys can be given as well, but if they are
|
19
|
+
# not then a default value is assigned (again, according to
|
20
|
+
# arg_specs passed in).
|
10
21
|
def hash_make arg_specs, hashed_args, assign_args = true
|
11
22
|
arg_specs.each do |arg_spec|
|
12
|
-
raise ArgumentError, "arg_specs item #{arg_spec} is not a
|
23
|
+
raise ArgumentError, "arg_specs item #{arg_spec} is not a ArgSpec" unless arg_spec.is_a?(ArgSpec)
|
13
24
|
end
|
14
25
|
raise ArgumentError, "hashed_args is not a Hash" unless hashed_args.is_a?(Hash)
|
15
26
|
|
@@ -45,8 +56,10 @@ module HashMakeable
|
|
45
56
|
end
|
46
57
|
end
|
47
58
|
|
59
|
+
# Contains class methods to be added to a class that includes the
|
60
|
+
# HashMakeable module.
|
48
61
|
module ClassMethods
|
49
|
-
|
62
|
+
|
50
63
|
end
|
51
64
|
|
52
65
|
end
|
data/lib/hashmake/version.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe Hashmake::
|
3
|
+
describe Hashmake::ArgSpec do
|
4
4
|
it 'should raise ArgumentError if :reqd is false and no :default value is given' do
|
5
5
|
hash = {
|
6
6
|
:reqd => false, :key => :some_variable, :type => String
|
7
7
|
}
|
8
8
|
|
9
|
-
lambda { Hashmake::
|
9
|
+
lambda { Hashmake::ArgSpec.new hash }.should raise_error(ArgumentError)
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'should not raise ArgumentError if :reqd is false and a :default value is given' do
|
@@ -14,6 +14,6 @@ describe Hashmake::HashedArg do
|
|
14
14
|
:reqd => false, :key => :some_variable, :type => String, :default => ""
|
15
15
|
}
|
16
16
|
|
17
|
-
lambda { Hashmake::
|
17
|
+
lambda { Hashmake::ArgSpec.new hash }.should_not raise_error(ArgumentError)
|
18
18
|
end
|
19
19
|
end
|
data/spec/hash_makeable_spec.rb
CHANGED
@@ -5,8 +5,8 @@ describe Hashmake::HashMakeable do
|
|
5
5
|
include HashMakeable
|
6
6
|
|
7
7
|
HASHED_ARG_SPECS = [
|
8
|
-
|
9
|
-
|
8
|
+
ArgSpec.new(:reqd => true, :key => :reqd_string, :type => String, :validator => ->(a){ a.length < 10 }),
|
9
|
+
ArgSpec.new(:reqd => false, :key => :not_reqd_float, :type => Float, :default => 0.0, :validator => ->(a){ a.between?(0.0,1.0) }),
|
10
10
|
]
|
11
11
|
|
12
12
|
attr_reader :reqd_string, :not_reqd_float
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashmake
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -75,6 +75,22 @@ dependencies:
|
|
75
75
|
- - ~>
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0.8'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: pry
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
78
94
|
description: Make hash-based object initialization easy! Provides hash_make method
|
79
95
|
that can consider parameter name, type, default value, validation, and requiredd/not,
|
80
96
|
according to the specification provided. Also, by default assigns by default to
|
@@ -93,11 +109,11 @@ files:
|
|
93
109
|
- Rakefile
|
94
110
|
- hashmake.gemspec
|
95
111
|
- lib/hashmake.rb
|
112
|
+
- lib/hashmake/arg_spec.rb
|
96
113
|
- lib/hashmake/hash_makeable.rb
|
97
|
-
- lib/hashmake/hashed_arg.rb
|
98
114
|
- lib/hashmake/version.rb
|
115
|
+
- spec/arg_spec_spec.rb
|
99
116
|
- spec/hash_makeable_spec.rb
|
100
|
-
- spec/hashed_arg_spec.rb
|
101
117
|
- spec/hashmake_spec.rb
|
102
118
|
- spec/spec_helper.rb
|
103
119
|
homepage: https://rubygems.org/gems/hashmake
|
@@ -115,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
131
|
version: '0'
|
116
132
|
segments:
|
117
133
|
- 0
|
118
|
-
hash: -
|
134
|
+
hash: -66247597
|
119
135
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
136
|
none: false
|
121
137
|
requirements:
|
@@ -124,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
140
|
version: '0'
|
125
141
|
segments:
|
126
142
|
- 0
|
127
|
-
hash: -
|
143
|
+
hash: -66247597
|
128
144
|
requirements: []
|
129
145
|
rubyforge_project:
|
130
146
|
rubygems_version: 1.8.23
|
@@ -132,8 +148,8 @@ signing_key:
|
|
132
148
|
specification_version: 3
|
133
149
|
summary: Make hashed-based object initialization easy!
|
134
150
|
test_files:
|
151
|
+
- spec/arg_spec_spec.rb
|
135
152
|
- spec/hash_makeable_spec.rb
|
136
|
-
- spec/hashed_arg_spec.rb
|
137
153
|
- spec/hashmake_spec.rb
|
138
154
|
- spec/spec_helper.rb
|
139
155
|
has_rdoc:
|
data/lib/hashmake/hashed_arg.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
module Hashmake
|
2
|
-
class HashedArg
|
3
|
-
|
4
|
-
DEFAULT_ARGS = {
|
5
|
-
:reqd => true,
|
6
|
-
:validator => ->(a){true},
|
7
|
-
:array => false,
|
8
|
-
}
|
9
|
-
|
10
|
-
attr_reader :key, :type, :validator, :reqd, :default, :array
|
11
|
-
|
12
|
-
def initialize args
|
13
|
-
new_args = DEFAULT_ARGS.merge(args)
|
14
|
-
|
15
|
-
@key = new_args[:key]
|
16
|
-
@type = new_args[:type]
|
17
|
-
raise ArgumentError, "args[:type] #{@type} is not a Class" unless @type.is_a?(Class)
|
18
|
-
|
19
|
-
@validator = new_args[:validator]
|
20
|
-
@reqd = new_args[:reqd]
|
21
|
-
@array = new_args[:array]
|
22
|
-
|
23
|
-
unless @reqd
|
24
|
-
msg = "if hashed arg is not required, a default value or value generator (proc) must be defined via :default key"
|
25
|
-
raise ArgumentError, msg unless args.has_key?(:default)
|
26
|
-
@default = new_args[:default]
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|