Class: EventBright::ApiObject

Inherits:
Object
  • Object
show all
Extended by:
ApiObjectClassMethods
Includes:
ApiObjectRelationships
Defined in:
lib/eventbright/api_object.rb

Direct Known Subclasses

Attendee, Discount, Event, Organizer, Ticket, User, Venue

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApiObjectClassMethods

collection, has, ignores, plural_name, readable, readable_date, reformats, remap, renames, requires, singlet_name, updatable, updatable_date

Methods included from ApiObjectRelationships

#collection_clean!, #collection_dirty!, #collection_dirty?, #collection_get, #collection_set, #collections_save, #load_collections_with_hash, #load_relations_with_hash, #relation_clean!, #relation_dirty!, #relation_dirty?, #relation_get, #relation_set, #relations_save, #unnest_child_response

Constructor Details

#initialize(owner = false, hash = {}) ⇒ ApiObject

Returns a new instance of ApiObject


11
12
13
14
15
16
17
18
19
# File 'lib/eventbright/api_object.rb', line 11

def initialize(owner = false, hash = {})
  preinit
  @owner = owner if owner
  unless hash.empty?
    @id = hash.delete(:id)
    load(hash, true)
    init
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes


8
9
10
# File 'lib/eventbright/api_object.rb', line 8

def attributes
  @attributes
end

#collectionsObject

Returns the value of attribute collections


8
9
10
# File 'lib/eventbright/api_object.rb', line 8

def collections
  @collections
end

#dirtyObject

Returns the value of attribute dirty


9
10
11
# File 'lib/eventbright/api_object.rb', line 9

def dirty
  @dirty
end

#dirty_collectionsObject

Returns the value of attribute dirty_collections


9
10
11
# File 'lib/eventbright/api_object.rb', line 9

def dirty_collections
  @dirty_collections
end

#dirty_relationsObject

Returns the value of attribute dirty_relations


9
10
11
# File 'lib/eventbright/api_object.rb', line 9

def dirty_relations
  @dirty_relations
end

#idObject

Returns the value of attribute id


7
8
9
# File 'lib/eventbright/api_object.rb', line 7

def id
  @id
end

#ownerObject

Returns the value of attribute owner


7
8
9
# File 'lib/eventbright/api_object.rb', line 7

def owner
  @owner
end

#relationsObject

Returns the value of attribute relations


8
9
10
# File 'lib/eventbright/api_object.rb', line 8

def relations
  @relations
end

Instance Method Details

#after_attribute_setObject


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

def after_attribute_set
end

#after_load(hash = {}) ⇒ Object

A callback for after loads


79
80
81
# File 'lib/eventbright/api_object.rb', line 79

def after_load(hash = {})
  hash
end

#after_newObject

After save callback, only called on a new call


137
# File 'lib/eventbright/api_object.rb', line 137

def after_new;    end

#after_saveObject

After save callback


141
# File 'lib/eventbright/api_object.rb', line 141

def after_save; end

#after_updateObject

After save callback, only called on an update call


139
# File 'lib/eventbright/api_object.rb', line 139

def after_update; end

#api_hashObject

Callbacks for individual hash changes These are added to the updatable_hash, if appropriate These are called by prep_api_hash


96
# File 'lib/eventbright/api_object.rb', line 96

def api_hash;     {:user => owner};  end

#attribute_get(key) ⇒ Object


33
# File 'lib/eventbright/api_object.rb', line 33

def attribute_get(key);   @attributes[key];   end

#attribute_set(key, val, no_dirty = false) ⇒ Object


35
36
37
38
39
# File 'lib/eventbright/api_object.rb', line 35

def attribute_set(key, val, no_dirty = false)
  @dirty[key] = true if(@attributes[key] != val && !no_dirty)
  @attributes[key] = val
  after_attribute_set
end

#before_save(opts = {}) ⇒ Object

Callback that happens before saving. Allows modification of options


112
# File 'lib/eventbright/api_object.rb', line 112

def before_save(opts = {});   opts;     end

#clean!Object


26
27
28
# File 'lib/eventbright/api_object.rb', line 26

def clean!
  @dirty = @dirty_relations = @dirty_collections = {}
end

#dirty?Boolean

Something is dirty if it's never been loaded or if the @dirty hash contains something.

Returns:

  • (Boolean)

175
176
177
178
# File 'lib/eventbright/api_object.rb', line 175

def dirty?
  @dirty ||= {}
  @dirty.size > 0 || !loaded?
end

#get_hashObject


98
# File 'lib/eventbright/api_object.rb', line 98

def get_hash;     {:id => id};       end

#initObject

Callback after initialization


31
# File 'lib/eventbright/api_object.rb', line 31

def init; end

#init_with_hash(hash, no_dirty = false) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/eventbright/api_object.rb', line 64

def init_with_hash(hash, no_dirty = false)
  @attributes ||= {}
  hash.each do |k, v| 
    self.__send__("#{k}=", v, no_dirty) unless (self.class.ignores.include?(k) ||
                                                self.class.ignores.include?(k.to_sym)) ||
                                                self.class.relations.include?(k) ||
                                                self.class.relations.include?(k.to_sym) ||
                                                self.class.collections.include?(k) ||
                                                self.class.collections.include?(k.to_sym) ||
                                                !self.respond_to?(k)
  end
end

#inspectObject


159
160
161
# File 'lib/eventbright/api_object.rb', line 159

def inspect
  "#<#{self.class.to_s}:#{self.id} @attributes=#{@attributes.inspect}>"
end

#load(hash = {}, no_dirty = false) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/eventbright/api_object.rb', line 49

def load(hash = {}, no_dirty = false)
  if hash.nil? || hash.size == 0
    response = EventBright.call("#{self.class.singlet_name}_get", prep_api_hash('get'))
    hash = response["#{self.class.singlet_name}"]
  end
  unless hash.nil? || hash.size == 0
    init_with_hash(hash, no_dirty)
    load_relations_with_hash(hash, no_dirty)
    load_collections_with_hash(hash, no_dirty)
  end
  clean! if no_dirty
  after_load(hash)
end

#load!Object

Forces a clean load from the remote API. Load can be passed a hash of local values to avoid an API call, but this circumvents it.


106
107
108
# File 'lib/eventbright/api_object.rb', line 106

def load!
  load({}, true)
end

#loaded?Boolean

Defines whether the object has been loaded from a remote source. If not, then we assume it's new when saving.

Returns:

  • (Boolean)

169
170
171
# File 'lib/eventbright/api_object.rb', line 169

def loaded?
  (!@id.nil? || @id == "")
end

#nested_hashObject


101
# File 'lib/eventbright/api_object.rb', line 101

def nested_hash;  {:user => owner, :id => id}; end

#new_hashObject


99
# File 'lib/eventbright/api_object.rb', line 99

def new_hash;     {};                end

#preinitObject


21
22
23
24
# File 'lib/eventbright/api_object.rb', line 21

def preinit
  @attributes = @relations = @collections = {}
  @dirty = @dirty_relations = @dirty_collections = {}
end

#prep_api_hash(method = 'get', hash = {}) ⇒ Object

A callback for methods to clean up the hash a bit allowing subclasses to insert the user if necessary


85
86
87
88
89
90
91
# File 'lib/eventbright/api_object.rb', line 85

def prep_api_hash(method = 'get', hash = {})
  hash = hash.merge api_hash
  hash = hash.merge get_hash if method == 'get'
  hash = hash.merge new_hash if method == 'new'
  hash = hash.merge update_hash if method == 'update'
  hash
end

#save(opts = {}) ⇒ Object

Save function. Can alter functionality by changing callbacks


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/eventbright/api_object.rb', line 115

def save(opts = {})
  return false unless dirty?
  opts.merge!(updatable_hash(self.class.requires))
  opts = relations_save(opts)
  opts = before_save(opts)
  call = if loaded?
    c = EventBright.call("#{self.class.singlet_name}_update", prep_api_hash('update', opts))
    after_update
    c
  else
    c = EventBright.call("#{self.class.singlet_name}_new", prep_api_hash('new', opts))
    after_new
    c
  end
  self.id = call["process"]["id"] unless loaded?
  collections_save
  after_save
  clean!
  call
end

#to_sObject


163
164
165
# File 'lib/eventbright/api_object.rb', line 163

def to_s
  "#<#{self.class.to_s}:#{self.id} @attributes=#{@attributes.inspect}>"
end

#updatable_hash(always_dirty = []) ⇒ Object


144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/eventbright/api_object.rb', line 144

def updatable_hash(always_dirty = [])
  updates = {}
  @attributes.each do |k, v|
    updates[k] = @attributes[k] if @dirty[k] || always_dirty.include?(k)
  end
  updates.merge! :id => @id if @id
  self.class.reformats.each do |k|
    updates[k] = self.__send__(k) if updates[k]
  end
  self.class.renames.each do |k,v|
    updates[v] = updates.delete(k) if updates[k]
  end
  updates
end

#update_hashObject


97
# File 'lib/eventbright/api_object.rb', line 97

def update_hash;  {:id => id};       end