hashmake 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,18 +7,9 @@ module Hashmake
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,
10
+ # The valid container types. If nil, indicates no container is expected,
20
11
  # just a plain object of type given by :type.
21
- CONTAINERS = [ CONTAINER_NONE, CONTAINER_ARRAY, CONTAINER_HASH ]
12
+ CONTAINERS = [ nil, Hash, Array ]
22
13
 
23
14
  # Defines default key/value pairs to use in initializing an instance.
24
15
  # The :reqd key is set to true by default.
@@ -27,7 +18,8 @@ class ArgSpec
27
18
  DEFAULT_ARGS = {
28
19
  :reqd => true,
29
20
  :validator => ->(a){true},
30
- :container => CONTAINER_NONE,
21
+ :container => nil,
22
+ :type => Object,
31
23
  }
32
24
 
33
25
  attr_reader :key, :type, :validator, :reqd, :default, :container
@@ -24,13 +24,12 @@ module HashMakeable
24
24
  # not then a default value is assigned (again, according to
25
25
  # arg_specs passed in).
26
26
  def hash_make arg_specs, hashed_args, assign_args = true
27
- arg_specs.each do |arg_spec|
27
+ arg_specs.each do |key, arg_spec|
28
28
  raise ArgumentError, "arg_specs item #{arg_spec} is not a ArgSpec" unless arg_spec.is_a?(ArgSpec)
29
29
  end
30
30
  raise ArgumentError, "hashed_args is not a Hash" unless hashed_args.is_a?(Hash)
31
31
 
32
- arg_specs.each do |arg_spec|
33
- key = arg_spec.key
32
+ arg_specs.each do |key, arg_spec|
34
33
  if hashed_args.has_key?(key)
35
34
  val = hashed_args[key]
36
35
  else
@@ -45,36 +44,52 @@ module HashMakeable
45
44
  end
46
45
  end
47
46
 
48
- case arg_spec.container
49
- when ArgSpec::CONTAINER_ARRAY
50
- raise ArgumentError, "val #{val} is not an array" unless val.is_a?(Array)
51
- val.each do |item|
52
- raise ArgumentError, "array item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
53
- raise ArgumentError, "array item #{item} is not valid" unless arg_spec.validator.call(item)
54
- end
55
- when ArgSpec::CONTAINER_HASH
56
- raise ArgumentError, "val #{val} is not a hash" unless val.is_a?(Hash)
57
- val.values.each do |item|
58
- raise ArgumentError, "hash item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
59
- raise ArgumentError, "hash item #{item} is not valid" unless arg_spec.validator.call(item)
60
- end
61
- when ArgSpec::CONTAINER_NONE
62
- raise ArgumentError, "val #{val} is not a #{arg_spec.type}" unless val.is_a?(arg_spec.type)
63
- raise ArgumentError, "val #{val} is not valid" unless arg_spec.validator.call(val)
64
- else
65
- raise ArgumentError, "arg_spec.container #{arg_spec.container} is not valid"
66
- end
67
-
47
+ validate_arg arg_spec, val
68
48
  if assign_args
69
49
  self.instance_variable_set("@#{key.to_s}".to_sym, val)
50
+ end
51
+ end
52
+ end
53
+
54
+ def validate_arg arg_spec, val
55
+ if arg_spec.container == Array
56
+ raise ArgumentError, "val #{val} is not an array" unless val.is_a?(Array)
57
+ val.each do |item|
58
+ raise ArgumentError, "array item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
59
+ raise ArgumentError, "array item #{item} is not valid" unless arg_spec.validator.call(item)
60
+ end
61
+ elsif arg_spec.container == Hash
62
+ raise ArgumentError, "val #{val} is not a hash" unless val.is_a?(Hash)
63
+ val.values.each do |item|
64
+ raise ArgumentError, "hash item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
65
+ raise ArgumentError, "hash item #{item} is not valid" unless arg_spec.validator.call(item)
70
66
  end
67
+ elsif arg_spec.container.nil?
68
+ raise ArgumentError, "val #{val} is not a #{arg_spec.type}" unless val.is_a?(arg_spec.type)
69
+ raise ArgumentError, "val #{val} is not valid" unless arg_spec.validator.call(val)
70
+ else
71
+ raise ArgumentError, "arg_spec.container #{arg_spec.container} is not valid"
71
72
  end
73
+
74
+ return true
72
75
  end
73
76
 
74
77
  # Contains class methods to be added to a class that includes the
75
78
  # HashMakeable module.
76
79
  module ClassMethods
80
+ def arg_spec args
81
+ ArgSpec.new args
82
+ end
77
83
 
84
+ def arg_spec_array args
85
+ args = { :container => Array, :default => ->(){Array.new} }.merge(args)
86
+ ArgSpec.new args
87
+ end
88
+
89
+ def arg_spec_hash args
90
+ args = { :container => Hash, :default => ->(){Hash.new} }.merge(args)
91
+ ArgSpec.new args
92
+ end
78
93
  end
79
94
 
80
95
  end
@@ -4,5 +4,5 @@
4
4
  #
5
5
  module Hashmake
6
6
  # hashmake version
7
- VERSION = "0.1.4"
7
+ VERSION = "0.1.5"
8
8
  end
@@ -17,7 +17,7 @@ describe Hashmake::ArgSpec do
17
17
  lambda { Hashmake::ArgSpec.new hash }.should_not raise_error(ArgumentError)
18
18
  end
19
19
 
20
- it 'should raise ArgumentError if valid container is given' do
20
+ it 'should not raise ArgumentError if valid container is given' do
21
21
  Hashmake::ArgSpec::CONTAINERS.each do |container|
22
22
  hash = {
23
23
  :reqd => true, :key => :stuff, :type => String, :container => container
@@ -29,7 +29,7 @@ describe Hashmake::ArgSpec do
29
29
 
30
30
  it 'should raise ArgumentError if invalid container is given' do
31
31
  hash = {
32
- :reqd => true, :key => :stuff, :type => String, :container => :myOwnContainer
32
+ :reqd => true, :key => :stuff, :type => String, :container => Fixnum
33
33
  }
34
34
 
35
35
  lambda { Hashmake::ArgSpec.new hash }.should raise_error(ArgumentError)
@@ -1,20 +1,21 @@
1
+ require 'pry'
1
2
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
3
 
3
4
  describe Hashmake::HashMakeable do
4
5
  class MyTestClass
5
6
  include HashMakeable
6
7
 
7
- HASHED_ARG_SPECS = [
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
- ArgSpec.new(:reqd => false, :key => :not_reqd_array_of_float, :type => Float, :container => ArgSpec::CONTAINER_ARRAY, :default => ->(){Array.new}, :validator => ->(a){ a.between?(0.0,1.0) }),
11
- ArgSpec.new(:reqd => false, :key => :not_reqd_hash_of_float, :type => Float, :container => ArgSpec::CONTAINER_HASH, :default => ->(){Hash.new}, :validator => ->(a){ a.between?(0.0,1.0) }),
12
- ]
8
+ ARG_SPECS = {
9
+ :reqd_string => arg_spec(:reqd => true, :type => String, :validator => ->(a){ a.length < 10 }),
10
+ :not_reqd_float => arg_spec(:reqd => false, :type => Float, :default => 0.0, :validator => ->(a){ a.between?(0.0,1.0) }),
11
+ :not_reqd_array_of_float => arg_spec_array(:reqd => false, :type => Float, :validator => ->(a){ a.between?(0.0,1.0) }),
12
+ :not_reqd_hash_of_float => arg_spec_hash(:reqd => false, :type => Float, :validator => ->(a){ a.between?(0.0,1.0) }),
13
+ }
13
14
 
14
15
  attr_reader :reqd_string, :not_reqd_float, :not_reqd_array_of_float, :not_reqd_hash_of_float
15
16
 
16
17
  def initialize hashed_args = {}
17
- hash_make MyTestClass::HASHED_ARG_SPECS, hashed_args
18
+ hash_make ARG_SPECS, hashed_args
18
19
  end
19
20
  end
20
21
 
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
4
+ version: 0.1.5
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-06 00:00:00.000000000 Z
12
+ date: 2013-03-04 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: 581566081
136
+ hash: 144920435
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: 581566081
145
+ hash: 144920435
146
146
  requirements: []
147
147
  rubyforge_project:
148
148
  rubygems_version: 1.8.23