hashmake 0.1.2 → 0.1.3

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.
@@ -11,4 +11,9 @@ Changed HashedArg to ArgSpec. Added documentation.
11
11
  === 0.1.2 / 2013-02-04
12
12
 
13
13
  Added documentation comment for Hashmake::HashMakeable module.
14
- Removed README.md so YARD could run without the redcarpet gem.
14
+ Removed README.md so YARD could run without the redcarpet gem.
15
+
16
+ === 0.1.3 / 2013-02-06
17
+
18
+ For the ArgSpec class, change :array key to :container, which can be set to any value included in ArgSpec::CONTAINERS.
19
+ In hash_make, add support for Hash containers which contain objects of type :type. Suppor for Array container is still in place.
@@ -6,21 +6,37 @@ module Hashmake
6
6
  # @author James Tunnell
7
7
  #
8
8
  class ArgSpec
9
-
9
+
10
+ # Used to specify that an arg key is mapped to to a regular value of type
11
+ # :type (rather than a container which contains values of type :type).
12
+ CONTAINER_NONE = :containerNone
13
+ # Used to specify that an arg key is mapped to an Array that contains values
14
+ # of type :type.
15
+ CONTAINER_ARRAY = :containerArray
16
+ # Used to specify that an arg key is mapped to a Hash that contains values
17
+ # of type :type.
18
+ CONTAINER_HASH = :containerHash
19
+ # The valid container types. CONTAINER_NONE indicates no container is expected,
20
+ # just a plain object of type given by :type.
21
+ CONTAINERS = [ CONTAINER_NONE, CONTAINER_ARRAY, CONTAINER_HASH ]
22
+
10
23
  # Defines default key/value pairs to use in initializing an instance.
24
+ # The :reqd key is set to true by default.
25
+ # The :validator key is set to a Proc that always returns true.
26
+ # The :container key is set to CONTAINER_NONE.
11
27
  DEFAULT_ARGS = {
12
28
  :reqd => true,
13
29
  :validator => ->(a){true},
14
- :array => false,
30
+ :container => CONTAINER_NONE,
15
31
  }
16
32
 
17
- attr_reader :key, :type, :validator, :reqd, :default, :array
33
+ attr_reader :key, :type, :validator, :reqd, :default, :container
18
34
 
19
35
  # A new instance of ArgSpec.
20
36
  #
21
37
  # @param [Hash] hashed_args Hash to use in initializing an instance. Required
22
38
  # keys are :key and :type. Optional keys are :reqd,
23
- # :validator, :array, and :default.
39
+ # :validator, :container, and :default.
24
40
  # :key => the key used to identify a hashed arg.
25
41
  # :type => the type of object expected to be paired
26
42
  # with the key.
@@ -34,9 +50,10 @@ class ArgSpec
34
50
  # expecting it to produce the default.
35
51
  # :validator => a Proc used to check the validity of
36
52
  # 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.
53
+ # :container => indicates whether the arg key will be paired
54
+ # with a container (array or hash) which contains
55
+ # objects of the type specified by :type. Valid values
56
+ # for this are given by ArgSpec::CONTAINERS.
40
57
  def initialize hashed_args
41
58
  new_args = DEFAULT_ARGS.merge(hashed_args)
42
59
 
@@ -46,7 +63,9 @@ class ArgSpec
46
63
 
47
64
  @validator = new_args[:validator]
48
65
  @reqd = new_args[:reqd]
49
- @array = new_args[:array]
66
+
67
+ @container = new_args[:container]
68
+ raise ArgumentError, "CONTAINERS does not include container #{@container}" unless CONTAINERS.include?(@container)
50
69
 
51
70
  unless @reqd
52
71
  msg = "if hashed arg is not required, a default value or value generator (proc) must be defined via :default key"
@@ -45,13 +45,21 @@ module HashMakeable
45
45
  end
46
46
  end
47
47
 
48
- if arg_spec.array
48
+ case arg_spec.container
49
+ when ArgSpec::CONTAINER_ARRAY
49
50
  raise ArgumentError, "val #{val} is not an array" unless val.is_a?(Array)
50
51
  val.each do |item|
51
52
  raise ArgumentError, "array item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
52
53
  end
53
- else
54
+ when ArgSpec::CONTAINER_HASH
55
+ raise ArgumentError, "val #{val} is not a hash" unless val.is_a?(Hash)
56
+ val.values.each do |item|
57
+ raise ArgumentError, "hash item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
58
+ end
59
+ when ArgSpec::CONTAINER_NONE
54
60
  raise ArgumentError, "val #{val} is not a #{arg_spec.type}" unless val.is_a?(arg_spec.type)
61
+ else
62
+ raise ArgumentError, "arg_spec.container #{arg_spec.container} is not valid"
55
63
  end
56
64
 
57
65
  if assign_args
@@ -4,5 +4,5 @@
4
4
  #
5
5
  module Hashmake
6
6
  # hashmake version
7
- VERSION = "0.1.2"
7
+ VERSION = "0.1.3"
8
8
  end
@@ -16,4 +16,22 @@ describe Hashmake::ArgSpec do
16
16
 
17
17
  lambda { Hashmake::ArgSpec.new hash }.should_not raise_error(ArgumentError)
18
18
  end
19
+
20
+ it 'should raise ArgumentError if valid container is given' do
21
+ Hashmake::ArgSpec::CONTAINERS.each do |container|
22
+ hash = {
23
+ :reqd => true, :key => :stuff, :type => String, :container => container
24
+ }
25
+
26
+ lambda { Hashmake::ArgSpec.new hash }.should_not raise_error(ArgumentError)
27
+ end
28
+ end
29
+
30
+ it 'should raise ArgumentError if invalid container is given' do
31
+ hash = {
32
+ :reqd => true, :key => :stuff, :type => String, :container => :myOwnContainer
33
+ }
34
+
35
+ lambda { Hashmake::ArgSpec.new hash }.should raise_error(ArgumentError)
36
+ end
19
37
  end
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.2
4
+ version: 0.1.3
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-04 00:00:00.000000000 Z
12
+ date: 2013-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -133,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
133
133
  version: '0'
134
134
  segments:
135
135
  - 0
136
- hash: -678443747
136
+ hash: -683104209
137
137
  required_rubygems_version: !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  version: '0'
143
143
  segments:
144
144
  - 0
145
- hash: -678443747
145
+ hash: -683104209
146
146
  requirements: []
147
147
  rubyforge_project:
148
148
  rubygems_version: 1.8.23