Module: EventBright::ApiObjectRelationships

Included in:
ApiObject
Defined in:
lib/eventbright/api_object_relationships.rb

Instance Method Summary collapse

Instance Method Details

#collection_clean!(key) ⇒ Object


23
# File 'lib/eventbright/api_object_relationships.rb', line 23

def collection_clean!(key); @dirty_collections[key] = false;          end

#collection_dirty!(key) ⇒ Object


24
# File 'lib/eventbright/api_object_relationships.rb', line 24

def collection_dirty!(key); @dirty_collections[key] = true;           end

#collection_dirty?(key) ⇒ Boolean


25
# File 'lib/eventbright/api_object_relationships.rb', line 25

def collection_dirty?(key); @dirty_collections[key] ? true : false;   end

#collection_get(key) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/eventbright/api_object_relationships.rb', line 4

def collection_get(key);  
  return @collections[key] unless @collections[key].nil? || collection_dirty?(key)
  klass = self.class.collections[key]
  begin
    response = EventBright.call(klass.getter, nested_hash)
    response = unnest_child_response(response)
    c = klass.new(owner, response[klass.plural_name], self)
  rescue EventBright::Error => e
    if e.type == "Not Found" || e.type == "Discount error" || e.type == "Order error"
      c = klass.new(owner, nil, self) 
    else
      raise e
    end
  end
  collection_clean!(key)
  collection_set(key, c, self)
end

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


37
38
39
# File 'lib/eventbright/api_object_relationships.rb', line 37

def collection_set(key, val, no_dirty = false)
  @collections[key] = val
end

#collections_saveObject


66
67
68
69
70
# File 'lib/eventbright/api_object_relationships.rb', line 66

def collections_save
  self.class.collections.each do |col, klass|
    collection_get(col).save if collection_get(col)
  end
end

#load_collections_with_hash(hash, no_dirty = false) ⇒ Object


51
52
53
54
55
# File 'lib/eventbright/api_object_relationships.rb', line 51

def load_collections_with_hash(hash, no_dirty = false)
  self.class.relations.each do |rel, klass|
    collection_set(rel, klass.new(owner, hash.delete(klass.plural_name)), false, self) if hash[klass.singlet_name]
  end
end

#load_relations_with_hash(hash, no_dirty = false) ⇒ Object


45
46
47
48
49
# File 'lib/eventbright/api_object_relationships.rb', line 45

def load_relations_with_hash(hash, no_dirty = false)
  self.class.relations.each do |rel, klass|
    relation_set(rel, klass.new(owner, hash.delete(klass.singlet_name)), no_dirty) if hash[klass.singlet_name]
  end
end

#relation_clean!(key) ⇒ Object


27
# File 'lib/eventbright/api_object_relationships.rb', line 27

def relation_clean!(key);   @dirty_relations[key]   = false;          end

#relation_dirty!(key) ⇒ Object


28
# File 'lib/eventbright/api_object_relationships.rb', line 28

def relation_dirty!(key);   @dirty_relations[key]   = true;           end

#relation_dirty?(key) ⇒ Boolean


29
# File 'lib/eventbright/api_object_relationships.rb', line 29

def relation_dirty?(key);   @dirty_relations[key]   ? true : false;   end

#relation_get(key) ⇒ Object


3
# File 'lib/eventbright/api_object_relationships.rb', line 3

def relation_get(key);    @relations[key];    end

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


32
33
34
35
# File 'lib/eventbright/api_object_relationships.rb', line 32

def relation_set(key, val, no_dirty = false)
  @dirty_relations[key] = true
  @relations[key] = val
end

#relations_save(opts) ⇒ Object


57
58
59
60
61
62
63
64
# File 'lib/eventbright/api_object_relationships.rb', line 57

def relations_save(opts)
  self.class.relations.each do |rel, klass|
    relation_get(rel).save if relation_get(rel) && relation_get(rel).dirty?
    opts["#{rel.to_s}_id"] = relation_get(rel).id if relation_dirty?(rel)
    opts.delete(rel)
  end
  opts
end

#unnest_child_response(response) ⇒ Object


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

def unnest_child_response(response)
  response["#{self.class.singlet_name}"]
end