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 +4 -4
- data/.gitignore +1 -0
- data/lib/hash-to-obj.rb +63 -17
- data/lib/hash-to-obj/version.rb +3 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fac97be8d0c513fd32e27704024deea023fa55cb
|
4
|
+
data.tar.gz: 930bb5f01ff267421a7ced58c3b1f89e89a3fcb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ba08e29a2f015b144c345c5b177c4e39c36b86030d9d8a3c609524cce6448923c4bb5567f2256b74f8865ccc0e941981037af97151f79cecf950116c102402f
|
7
|
+
data.tar.gz: a481b39b901627e060070d6d509abbb472294925cd5a27c80688a48550d0fd0fec39a4b2ae88839aa6d4f45dab9951dbf245373ab7f10a359678735f48feec72
|
data/.gitignore
CHANGED
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 = [:[], :[]
|
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.
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# override_warnings
|
22
|
-
#
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
34
|
-
|
35
|
-
|
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
|
-
|
113
|
-
|
156
|
+
##
|
157
|
+
# See {HashToObj::objectify}[rdoc-ref:HashToObj::objectify]
|
158
|
+
def objectify(*args)
|
159
|
+
HashToObj.objectify(*args)
|
114
160
|
end
|
115
161
|
end
|
data/lib/hash-to-obj/version.rb
CHANGED