Module: HttpApiTools::Model::Attributes::ClassMethods

Defined in:
lib/http_api_tools/model/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, options = {}) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/http_api_tools/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 = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/http_api_tools/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 = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/http_api_tools/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