Module: SmoothOperator::Operator::ORM::ClassMethods

Defined in:
lib/smooth_operator/operator/orm.rb

Instance Method Summary collapse

Instance Method Details

#create(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/smooth_operator/operator/orm.rb', line 30

def create(options = {})
  new_object = new(options)

  http_handler_orm.make_the_call(:post, { model_name_downcase => new_object.safe_table_hash }, '') do |remote_call|
    new_object.send('after_create_update_or_destroy', remote_call)
  end

  new_object
end

#find(id, options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/smooth_operator/operator/orm.rb', line 14

def find(id, options = {})
  if id == :all
    find_each('', options)
  else
    find_one(id, options)
  end
end

#find_each(relative_path, options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/smooth_operator/operator/orm.rb', line 40

def find_each(relative_path, options)
  http_handler_orm.make_the_call(:get, options, relative_path) do |remote_call|
    objects_list = remote_call.get_attributes(table_name)
    
    if objects_list.kind_of?(Array)
      remote_call.response = objects_list.map { |attributes| new remote_call.get_attributes(model_name_downcase, attributes) }
    else
      remote_call.response = objects_list
    end
  end
end

#find_one(relative_path, options) ⇒ Object



52
53
54
55
56
# File 'lib/smooth_operator/operator/orm.rb', line 52

def find_one(relative_path, options)
  http_handler_orm.make_the_call(:get, options, relative_path) do |remote_call|
    remote_call.response = new remote_call.get_attributes(model_name_downcase)
  end
end

#safe_find(id, options = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/smooth_operator/operator/orm.rb', line 22

def safe_find(id, options = {})
  begin
    find(id, options)
  rescue Exception => exception #exception.response contains the server response
    id == :all ? [] : nil
  end
end