Module: Her::Model::ORM::ClassMethods

Defined in:
lib/her/model/orm.rb

Instance Method Summary collapse

Instance Method Details

#destroy_existing(id, params = {}) ⇒ Object

Destroy an existing resource

Examples:

User.destroy_existing(1)
# Called via DELETE "/users/1"


163
164
165
166
167
# File 'lib/her/model/orm.rb', line 163

def destroy_existing(id, params={})
  request(params.merge(_method: :delete, _path: build_request_path(params.merge(id: id)))) do |parsed_data|
    new(parse(parsed_data[:data]))
  end
end

#include_root_in_json(value = nil) ⇒ Object

Return or change the value of ‘include_root_in_json`



178
179
180
181
# File 'lib/her/model/orm.rb', line 178

def include_root_in_json(value=nil)
  return @include_root_in_json if value.nil?
  @include_root_in_json = value
end

#new_collection(parsed_data) ⇒ Object

Initialize a collection of resources with raw data from an HTTP request

Parameters:

  • parsed_data (Array)


132
133
134
# File 'lib/her/model/orm.rb', line 132

def new_collection(parsed_data)
  Her::Model::ORM.initialize_collection(self, parsed_data)
end

#parse(data) ⇒ Object

Parse data before assigning it to a resource

Parameters:

  • data (Hash)


139
140
141
142
143
144
145
# File 'lib/her/model/orm.rb', line 139

def parse(data)
  if parse_root_in_json
    parse_root_in_json == true ? data[root_element.to_sym] : data[parse_root_in_json]
  else
    data
  end
end

#parse_root_in_json(value = nil) ⇒ Object

Return or change the value of ‘parse_root_in`



184
185
186
187
# File 'lib/her/model/orm.rb', line 184

def parse_root_in_json(value=nil)
  return @parse_root_in_json if value.nil?
  @parse_root_in_json = value
end

#save_existing(id, params) ⇒ Object

Save an existing resource and return it

Examples:

@user = User.save_existing(1, { fullname: "Tobias Fünke" })
# Called via PUT "/users/1"


152
153
154
155
156
# File 'lib/her/model/orm.rb', line 152

def save_existing(id, params)
  resource = new(params.merge(id: id))
  resource.save
  resource
end

#setter_method_namesObject



170
171
172
173
174
175
# File 'lib/her/model/orm.rb', line 170

def setter_method_names
  @setter_method_names ||= instance_methods.inject(Set.new) do |memo, method_name|
    memo << method_name.to_s if method_name.to_s.end_with?('=')
    memo
  end
end