Skip to content

Instantly share code, notes, and snippets.

@cjse
Created September 18, 2012 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjse/3741896 to your computer and use it in GitHub Desktop.
Save cjse/3741896 to your computer and use it in GitHub Desktop.
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