hashifiable 0.1.0 → 0.1.1
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 +8 -9
- data/lib/hashifiable/version.rb +1 -1
- metadata +1 -1
data/lib/hashifiable.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
-
require 'json'
|
2
|
-
|
3
1
|
module Hashifiable
|
2
|
+
|
4
3
|
def hashify(*args)
|
5
4
|
|
6
|
-
## Defines to_hash method dinamically with the key/values specified
|
7
|
-
|
5
|
+
## Defines to_hash method dinamically with the key/values specified
|
6
|
+
## in *args.
|
7
|
+
define_method :to_h do
|
8
8
|
hash_representation = {}
|
9
9
|
|
10
10
|
methods = args.select { |a| a.is_a?(Symbol) }
|
11
|
-
procs = args.select { |a| a.is_a?(Hash) }.inject
|
11
|
+
procs = args.select { |a| a.is_a?(Hash) }.inject do |all, hash|
|
12
|
+
all.merge(hash)
|
13
|
+
end
|
12
14
|
|
13
15
|
## Create keys for all lambdas sent.
|
14
16
|
procs.each do |name, function|
|
@@ -23,9 +25,6 @@ module Hashifiable
|
|
23
25
|
hash_representation
|
24
26
|
end
|
25
27
|
|
26
|
-
|
27
|
-
define_method :to_json do
|
28
|
-
self.to_hash.to_json
|
29
|
-
end
|
28
|
+
alias_method :to_hash, :to_h
|
30
29
|
end
|
31
30
|
end
|
data/lib/hashifiable/version.rb
CHANGED