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 CHANGED
@@ -7,19 +7,22 @@ module Hashifiable
7
7
  define_method :to_h do
8
8
  hash_representation = {}
9
9
 
10
- methods = args.select { |a| a.is_a?(Symbol) }
11
- procs = args.select { |a| a.is_a?(Hash) }.inject do |all, hash|
12
- all.merge(hash)
13
- end
14
-
15
- ## Create keys for all lambdas sent.
16
- procs.each do |name, function|
17
- hash_representation[name] = instance_exec(&function)
18
- end
19
-
20
- ## Create keys for all methods specified.
21
- methods.each do |attribute|
22
- hash_representation[attribute] = self.send(attribute)
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
@@ -2,7 +2,7 @@ module Hashifiable
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 1
5
+ PATCH = 2
6
6
 
7
7
  def self.to_s
8
8
  [MAJOR, MINOR, PATCH].join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashifiable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: