hash-to-obj 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee934fea5eddeefa8540e7691d2b39b28e1e948c
4
- data.tar.gz: b5b7c45e8ffd2d74b4cd84f4051a2184a85c4d2f
3
+ metadata.gz: fac97be8d0c513fd32e27704024deea023fa55cb
4
+ data.tar.gz: 930bb5f01ff267421a7ced58c3b1f89e89a3fcb9
5
5
  SHA512:
6
- metadata.gz: b79470f4236a73cab06c10bb5494fdcd20e62ee98c9802854b4325497203e4193dbca745ff1c66857b8da66cf0eed2fecd62b25975d15780e5de3c1fdd6e7321
7
- data.tar.gz: e19f7befcd4897b53aa8cbb170928222317570532bdbe6c5180e651dc718443709cb82b95cb88c71367898ca8eae5bfc110d8e4932641d78c9132735b2835e02
6
+ metadata.gz: 9ba08e29a2f015b144c345c5b177c4e39c36b86030d9d8a3c609524cce6448923c4bb5567f2256b74f8865ccc0e941981037af97151f79cecf950116c102402f
7
+ data.tar.gz: a481b39b901627e060070d6d509abbb472294925cd5a27c80688a48550d0fd0fec39a4b2ae88839aa6d4f45dab9951dbf245373ab7f10a359678735f48feec72
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /*.gem
data/lib/hash-to-obj.rb CHANGED
@@ -11,32 +11,76 @@ module HashToObj
11
11
  # Anything that you want to objectify needs to respond to these methods like a
12
12
  # Hash would. If it doesn't at least respond_to? these methods, an error will
13
13
  # be thrown when objectifying.
14
- DUCK_TYPE_API = [:[], :[]=].freeze
14
+ DUCK_TYPE_API = [:[], :[]=, :each_key].freeze
15
15
 
16
16
  ##
17
17
  # Throws an error if hash doesn't have all messages defined in DUCK_TYPE_API.
18
- # Generates the accessors for all keys on the passed hash. Unless
19
- # override_warnings is true-y, this will throw errors if we're gonna screw
20
- # anything up. If you pass something that responds to :puts in
21
- # override_warnings then warnings will just be puts'd to that, and things will
22
- # continue.
23
- def self.objectify(hash, override_warnings = false)
24
- # Make sure it looks SOMEWHAT familiar.
25
- DUCK_TYPE_API.each do |method_sym|
26
- unless hash.respond_to?(method_sym) then
27
- raise(ArgumentError,
28
- "Cannot objectify something that doesn't look like a hash.")
18
+ # Generates the accessors for all keys on the passed hash.
19
+ #
20
+ # Accepted options:
21
+ # [override_warnings] Unless override_warnings is truthy, this will throw
22
+ # errors if we're gonna screw anything up. If you pass
23
+ # something that responds to :puts in override_warnings
24
+ # then warnings will just be puts'd to that, and things
25
+ # will continue.
26
+ # [default_module] If a default_module is specified, that will be included
27
+ # in the hash before adding our accessors, essentially
28
+ # allowing you to define a set of methods that should be
29
+ # present after objectifying regardless of the keys in the
30
+ # hash.
31
+ def self.objectify(hash, options = {})
32
+ if options.is_a?(Hash) then
33
+ internal_objectify(hash,
34
+ options[:override_warnings],
35
+ options[:default_module])
36
+ else
37
+ unless HashToObj.const_defined?(:SQUELCH_GLOBAL_WARNINGS) then
38
+ puts "objectify(hash, override_warnings) is deprecated.\n"\
39
+ ' please use objectify(hash, override_warnings: true) instead.'
29
40
  end
41
+ # Looks like they're using 0.1.0 API.
42
+ internal_objectify(hash, options)
43
+ end
44
+ end
45
+
46
+ #-------------------------------------------------------------------------
47
+ # :section: Internal Methods
48
+ # Usually don't want to mess with these if you're not part of hash-to-obj.
49
+ #-------------------------------------------------------------------------
50
+
51
+ ##
52
+ # Internal version of objectify. Uses seperated arguments instead of options
53
+ # hash.
54
+ def self.internal_objectify(hash,
55
+ override_warnings = false,
56
+ default_module = nil)
57
+ # Make sure it looks SOMEWHAT familiar.
58
+ unless quacks?(hash) then
59
+ raise(ArgumentError,
60
+ "Cannot objectify something that doesn't look like a hash.")
30
61
  end
31
62
 
32
63
  # Now lets actually add those methods.
33
- hash.each_key do |key|
34
- generate_accessors(hash, key, override_warnings)
35
- end
64
+ hash.each_key { |key| generate_accessors(hash, key, override_warnings) }
65
+
66
+ hash.extend(default_module) if default_module
36
67
 
37
68
  hash
38
69
  end
39
70
 
71
+ ##
72
+ # Returns true if the passed object responds to all methods defined in
73
+ # DUCK_TYPE_API.
74
+ def self.quacks?(obj)
75
+ DUCK_TYPE_API.each do |method_sym|
76
+ unless obj.respond_to?(method_sym) then
77
+ return false
78
+ end
79
+ end
80
+
81
+ true
82
+ end
83
+
40
84
  ##
41
85
  # Generates the accessors for the passed key on the passed hash. Unless
42
86
  # override_warnings is true-y, this will throw errors if we're gonna screw
@@ -109,7 +153,9 @@ if begin
109
153
  'Call HashToObj.objectify instead.'
110
154
  end
111
155
  else
112
- def objectify(hash, override_warnings = false)
113
- HashToObj.objectify(hash, override_warnings)
156
+ ##
157
+ # See {HashToObj::objectify}[rdoc-ref:HashToObj::objectify]
158
+ def objectify(*args)
159
+ HashToObj.objectify(*args)
114
160
  end
115
161
  end
@@ -1,3 +1,5 @@
1
1
  module HashToObj
2
- VERSION = '0.1.0'.freeze
2
+ # :category:
3
+ # Current version of hash-to-obj
4
+ VERSION = '0.2.0'.freeze
3
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash-to-obj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Maxwell