module Hat::Model::Attributes::ClassMethods

Public Instance Methods

attribute(name, options = {}) click to toggle source
# File lib/hat/model/attributes.rb, line 111
def attribute(name, options = {})
  self._attributes[name] = options
  if options[:read_only]
    self.send(:attr_reader, name.to_sym)
  else
    self.send(:attr_accessor, name.to_sym)
  end
end
belongs_to(attr_name, options = {}) click to toggle source
# File lib/hat/model/attributes.rb, line 120
def belongs_to(attr_name, options = {})

  id_attr_name = "#{attr_name}_id"
  id_setter_method_name = "#{id_attr_name}="

  send(:attr_reader, attr_name)
  send(:attr_reader, id_attr_name)

  define_method("#{attr_name}=") do |value|
    set_belongs_to_value(attr_name, value)
  end

  define_method(id_setter_method_name) do |value|
    instance_variable_set("@#{id_attr_name}", value)
  end
end
has_many(attr_name, options = {}) click to toggle source
# File lib/hat/model/attributes.rb, line 138
def has_many(attr_name, options = {})

  ids_attr_name = "#{attr_name.to_s.singularize}_ids"
  id_setter_method_name = "#{ids_attr_name}="

  send(:attr_reader, attr_name)
  send(:attr_reader, ids_attr_name)

  define_method("#{attr_name}=") do |value|
    set_has_many_value(attr_name, value)
  end

  define_method(id_setter_method_name) do |value|
    instance_variable_set("@#{ids_attr_name}", value)
  end

end