hashifiable 0.1.1 → 0.1.2
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/lib/hashifiable.rb +16 -13
- data/lib/hashifiable/version.rb +1 -1
- metadata +1 -1
data/lib/hashifiable.rb
CHANGED
@@ -7,19 +7,22 @@ module Hashifiable
|
|
7
7
|
define_method :to_h do
|
8
8
|
hash_representation = {}
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
10
|
+
args.each do |argument|
|
11
|
+
case argument
|
12
|
+
when Symbol
|
13
|
+
## Calls the specified method on the object and stores it
|
14
|
+
## under the method name.
|
15
|
+
hash_representation[argument] = self.send(argument)
|
16
|
+
when Hash
|
17
|
+
## Takes the key of the hash passed as the key in the object's
|
18
|
+
## hash, the Proc/lambda is called in the context of the object
|
19
|
+
## to provide the value in the hash.
|
20
|
+
argument.each do |name, function|
|
21
|
+
hash_representation[name] = instance_exec(&function)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
raise ArgumentError
|
25
|
+
end
|
23
26
|
end
|
24
27
|
|
25
28
|
hash_representation
|
data/lib/hashifiable/version.rb
CHANGED