Skip to content

Instantly share code, notes, and snippets.

@jdlich
Created April 9, 2010 20:02
Show Gist options
  • Save jdlich/361529 to your computer and use it in GitHub Desktop.
Save jdlich/361529 to your computer and use it in GitHub Desktop.
def initialize item
# set the item instance variable
@item = item
# grab the item's attributes from its yaml file
attributes = File.expand_path('content' + item.identifier.gsub(/\/$/,'') + '.yaml')
# load the yaml data into a hash and grab just the attribute
# names, not the values
attributes = File.open(attributes) { |a| YAML::load(a) }.keys
# create an instance variable and an accessor method for each
# attribute on the fly
attributes.each do |a|
instance_variable_set("@#{a}", item[a.to_sym])
instance_eval %{
def #{a}
instance_variable_get("@#{a}")
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment