Skip to content

Instantly share code, notes, and snippets.

@adamcarlile
Created October 31, 2011 21:22
Show Gist options
  • Save adamcarlile/1328997 to your computer and use it in GitHub Desktop.
Save adamcarlile/1328997 to your computer and use it in GitHub Desktop.
Serialized hash, accessors as methods
module ExtraFields
def self.included(base)
base.serialized_fields.each do |method_name|
base.class_eval <<-EOS
def #{method_name.to_s}
extra_fields[:#{method_name}] if extra_fields && extra_fields[:#{method_name}]
end
def #{method_name.to_s}=(value)
self.extra_fields[:#{method_name}] = value
end
EOS
end
end
end
class Page
include ExtraFields
private
def serialized_fields
[:field_one, :field_two, :another_field]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment