Created
September 18, 2012 07:57
-
-
Save cjse/3741896 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hash_field :body | |
hash_field :type { self.class.to_s } | |
hash_fields :body, | |
:title, | |
:version => "1.2.0", | |
:type => -> { self.class.to_s } | |
def self.hash_field (key, value=nil, &block) | |
if block_given? | |
value = block | |
elsif value.nil? | |
value = key | |
end | |
@hash_fields[key] = value | |
end | |
def self.hash_fields (*keys) | |
keys.each do |key| | |
if key.is_a? Hash | |
@hash_fields.merge!(key) | |
else | |
@hash_fields[key] = key | |
end | |
end | |
end | |
def to_hash | |
hash = {} | |
self.class.hash_fields.each do |key, value| | |
if value.respond_to? :call | |
hash[key] = value.call | |
elsif self.respond_to? value | |
hash[key] = send(value) | |
else | |
hash[key] = value | |
end | |
end | |
hash | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment