Module: Hashifiable
- Defined in:
- lib/hashifiable.rb,
lib/hashifiable/version.rb
Defined Under Namespace
Modules: Version
Constant Summary collapse
- VERSION =
Version.to_s
Instance Method Summary collapse
Instance Method Details
#hashify(*args) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hashifiable.rb', line 4 def hashify(*args) ## Defines to_hash method dinamically with the key/values specified in *args. define_method :to_hash do hash_representation = {} methods = args.select { |a| a.is_a?(Symbol) } procs = args.select { |a| a.is_a?(Hash) }.inject { |all, hash| all.merge(hash) } ## Create keys for all lambdas sent. procs.each do |name, function| hash_representation[name] = instance_exec(&function) end ## Create keys for all methods specified. methods.each do |attribute| hash_representation[attribute] = self.send(attribute) end hash_representation end ## Defines to_json based on the to_hash method. define_method :to_json do self.to_hash.to_json end end |