Class: FellowshipOne::ApiObject

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

Overview

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

Direct Known Subclasses

Communication, Contribution, Fund, Household, Person, Status

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

.__f1_attributesObject

A list of f1_attr_accessors that have been specified.



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

def self.__f1_attributes
  @__f1_attributes
end

.f1_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.f1_attr_accessor(*vars)
  @__f1_attributes ||= []
  @__f1_attributes.concat(vars)
  attr_accessor(*vars)
end

Instance Method Details

#_default_result_keyObject



109
110
111
# File 'lib/api/api_object.rb', line 109

def _default_result_key
  self.class.to_s.split('::').last.downcase
end

#_field_mapObject

This method should be overwritten in the ApiObject subclass.



105
106
107
# File 'lib/api/api_object.rb', line 105

def _field_map
  {}
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
37
38
39
40
# 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 = "#{_attr_underscore(key.to_s)}="
      if method_to_call.chars.first == "@"
        method_to_call.reverse!.chop!.reverse!
      end

      if respond_to?(method_to_call)
        self.send(method_to_call, value) 
      end
      
    end
  end     
end

#saveObject

Save this object.

Returns:

  • True on success, otherwise false.



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/api/api_object.rb', line 75

def save
  raise "@writer_object not set for #{self.class}" if @writer_object.nil?
  writer = @writer_object.new(self.to_attributes) 
  result = writer.save_object
  if result === false
    @error_messages = writer.error_messages
  else
    rkey = self._default_result_key
    self.initialize_from_json_object(rkey.nil? ? result : result[rkey])
  end
  result === false ? false : true
end

#set_attributes(attribute_data) ⇒ Object

Sets the current object’s attributes from a hash



67
68
69
# File 'lib/api/api_object.rb', line 67

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/api/api_object.rb', line 51

def to_attributes 
  vals = {}
  #vals = {:marked_for_destruction => self.is_deleted?} if self.is_deleted?
  self.class.__f1_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[_f1ize(tca)] = rep
  end
  _map_fields(vals)
  vals
end