Skip to content

Instantly share code, notes, and snippets.

@frangucc
Created January 26, 2011 20:15
Show Gist options
  • Save frangucc/797353 to your computer and use it in GitHub Desktop.
Save frangucc/797353 to your computer and use it in GitHub Desktop.
Unpack Content Package
class Content_Package::Unpack
include ActiveModel::Serializers::JSON
include ActiveModel::Validations
ATTRIBUTES = [:test_id, :question_id, :answer]
attr_accessor *ATTRIBUTES
validates_presence_of :test_id, :question_id
validates_inclusion_of :answer, :in => %w[A B C D]
def initialize(attributes = {})
self.attributes = attribute
end
def attributes
ATTRIBUTES.inject(
ActiveSupport::HashWithIndifferentAccess.new
) do |result, key|
result[key] = read_attribute_for_validation(key)
result
end
end
def attributes=(attrs)
attrs.each_pair {|k, v| send("#{k}=", v)}
end
def read_attribute_for_validation(key)
send(key)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment