Class: TheCity::ApiObject

Inherits:
Object
  • Object
show all
Defined in:
lib/api/api_object.rb

Overview

This class is the base class for all TheCity objects and is meant to be inherited.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#error_messagesObject (readonly)

Returns the value of attribute error_messages.



6
7
8
# File 'lib/api/api_object.rb', line 6

def error_messages
  @error_messages
end

#marked_for_destructionObject (readonly)

Returns the value of attribute marked_for_destruction.



6
7
8
# File 'lib/api/api_object.rb', line 6

def marked_for_destruction
  @marked_for_destruction
end

Class Method Details

.__tc_attributesObject

A list of tc_attr_accessors that have been specified.



18
19
20
# File 'lib/api/api_object.rb', line 18

def self.__tc_attributes
  @__tc_attributes
end

.tc_attr_accessor(*vars) ⇒ Object

Used to specify a list of getters and setters.



10
11
12
13
14
# File 'lib/api/api_object.rb', line 10

def self.tc_attr_accessor(*vars)
  @__tc_attributes ||= []
  @__tc_attributes.concat(vars)
  attr_accessor(*vars)
end

Instance Method Details

#deleteObject

Delete this object.

Returns:

  • True on success, otherwise false.



85
86
87
88
89
90
91
92
93
94
# File 'lib/api/api_object.rb', line 85

def delete
  writer = @writer_object.new(self.to_attributes) 
  result = writer.delete_object
  if result === false
    @error_messages = writer.error_messages
  else
    @_deleted = true
  end
  result === false ? false : true
end

#initialize_from_json_object(object_attributes) ⇒ Object

Initializes the current object from the JSON data that was loaded into the Hash

Parameters:

  • object_attributes

    A Hash of values to load into the current object.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/api/api_object.rb', line 25

def initialize_from_json_object(object_attributes)
  if object_attributes.is_a?( Hash )
    object_attributes.each do |key, value| 
      method_to_call = "#{key.to_s.downcase.gsub(' ', '_')}="
      if respond_to?(method_to_call)
        self.send(method_to_call, value) 
      else
        # puts method_to_call  # Show the missing methods
      end
    end
  end     
end

#is_deleted?Boolean

Returns the status of the current object.

Returns:

  • (Boolean)


40
41
42
# File 'lib/api/api_object.rb', line 40

def is_deleted?
  @_deleted ||= false
end

#saveObject

Save this object.

Returns:

  • True on success, otherwise false.



70
71
72
73
74
75
76
77
78
79
# File 'lib/api/api_object.rb', line 70

def save
  writer = @writer_object.new(self.to_attributes) 
  result = writer.save_object
  if result === false
    @error_messages = writer.error_messages
  else
    self.set_attributes(result)
  end
  result === false ? false : true
end

#set_attributes(attribute_data) ⇒ Object

Sets the current object’s attributes from a hash



62
63
64
# File 'lib/api/api_object.rb', line 62

def set_attributes(attribute_data)
  attribute_data.each { |key, value| self.send("#{key}=", value) if self.respond_to?("#{key}=") }
end

#to_attributesObject

Gets the current object’s attributes in a Hash.

Returns:

  • A hash of all the attributes.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/api/api_object.rb', line 47

def to_attributes 
  vals = {}
  vals = {:marked_for_destruction => self.is_deleted?} if self.is_deleted?
  self.class.__tc_attributes.each do |tca| 
    rep = self.send(tca)               
    if rep.class == Array   
      rep.collect! { |r| r.respond_to?(:to_attributes) ? r.to_attributes : r }
    end
    vals[tca] = rep
  end
  vals
end