Class: ActiveZuora::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/active_zuora/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zobject_class, selected_field_names = [:id]) ⇒ Relation

Returns a new instance of Relation.



8
9
10
11
# File 'lib/active_zuora/relation.rb', line 8

def initialize(zobject_class, selected_field_names=[:id])
  @zobject_class, @selected_field_names, @filters = zobject_class, selected_field_names, []
  @order_attribute, @order_direction = :created_date, :asc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/active_zuora/relation.rb', line 157

def method_missing(method, *args, &block)
  # This is how the chaing can happen on class methods or named scopes on the 
  # ZObject class.
  if Array.method_defined?(method)
    to_a.send(method, *args, &block)
  elsif zobject_class.respond_to?(method)
    scoped { zobject_class.send(method, *args, &block) }
  else
    super
  end
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



4
5
6
# File 'lib/active_zuora/relation.rb', line 4

def filters
  @filters
end

#order_attributeObject

Returns the value of attribute order_attribute.



4
5
6
# File 'lib/active_zuora/relation.rb', line 4

def order_attribute
  @order_attribute
end

#order_directionObject

Returns the value of attribute order_direction.



4
5
6
# File 'lib/active_zuora/relation.rb', line 4

def order_direction
  @order_direction
end

#selected_field_namesObject

Returns the value of attribute selected_field_names.



4
5
6
# File 'lib/active_zuora/relation.rb', line 4

def selected_field_names
  @selected_field_names
end

#zobject_classObject (readonly)

Returns the value of attribute zobject_class.



6
7
8
# File 'lib/active_zuora/relation.rb', line 6

def zobject_class
  @zobject_class
end

Instance Method Details

#delete_allObject



151
152
153
# File 'lib/active_zuora/relation.rb', line 151

def delete_all
  zobject_class.delete(to_a.map(&:id))
end

#dupObject



13
14
15
16
17
18
19
# File 'lib/active_zuora/relation.rb', line 13

def dup
  dup = super
  dup.selected_field_names = dup.selected_field_names.dup
  dup.filters = dup.filters.dup
  dup.unload
  dup
end

#find(id) ⇒ Object



77
78
79
80
# File 'lib/active_zuora/relation.rb', line 77

def find(id)
  return nil if id.blank?
  where(:id => id).first
end

#find_each(&block) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/active_zuora/relation.rb', line 82

def find_each(&block)
  # Iterate through each item, but don't save the results in memory.
  if loaded?
    # If we're already loaded, iterate through the cached records.
    to_a.each(&block)
  else
    query.each(&block)
  end
end

#loaded?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/active_zuora/relation.rb', line 98

def loaded?
  !@records.nil?
end

#merge(relation) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_zuora/relation.rb', line 56

def merge(relation)
  if relation.is_a?(Hash)
    where(relation)
  else
    dup.tap do |dup| 
      dup.filters.concat relation.filters
      dup.filters.uniq!
      dup.order_attribute = relation.order_attribute
      dup.order_direction = relation.order_direction
    end
  end
end

#or(conditions) ⇒ Object



35
36
37
# File 'lib/active_zuora/relation.rb', line 35

def or(conditions)
  dup.tap { |dup| dup.filters << ['or', conditions] }\
end

#order(attribute, direction = :asc) ⇒ Object



39
40
41
42
43
44
# File 'lib/active_zuora/relation.rb', line 39

def order(attribute, direction = :asc)
  dup.tap do |dup|
    dup.order_attribute = attribute
    dup.order_direction = direction
  end
end

#query(&block) ⇒ Object



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

def query(&block)
  # Keep querying until all pages are retrieved.
  # Throws an exception for an invalid query.
  response = zobject_class.connection.request(:query){ |soap| soap.body = { :query_string => to_zql } }
  query_response = response[:query_response]
  records = objectify_query_results(query_response[:result][:records])
  records.each(&:block) if block_given?
  # If there are more pages of records, keep fetching
  # them until done.
  until query_response[:result][:done]
    query_response = zobject_class.connection.request(:query_more) do |soap|
      soap.body = { :query_locator => response[:query_response][:result][:query_locator] }
    end[:query_more_response]
    more_records = objectify_query_results(query_response[:result][:records])
    more_records.each(&:block) if block_given?
    records.concat more_records
  end
  sort_records!(records)
rescue Savon::SOAP::Fault => exception
  # Add the zql to the exception message and re-raise.
  exception.message << ": #{to_zql}"
  raise
end

#reloadObject



107
108
109
110
# File 'lib/active_zuora/relation.rb', line 107

def reload
  unload.to_a
  self
end

#scopedObject



46
47
48
49
50
51
52
53
54
# File 'lib/active_zuora/relation.rb', line 46

def scoped
  # Account.select(:id).where(:status => "Draft") do
  #   Account.all # => select id from Account where status = "Draft"
  # end
  previous_scope, zobject_class.current_scope = zobject_class.current_scope, self
  yield
ensure
  zobject_class.current_scope = previous_scope
end

#select(*field_names) ⇒ Object

Conditions / Selecting



25
26
27
# File 'lib/active_zuora/relation.rb', line 25

def select(*field_names)
  dup.tap { |dup| dup.selected_field_names = field_names.flatten }
end

#to_aObject Also known as: all



92
93
94
# File 'lib/active_zuora/relation.rb', line 92

def to_a
  @records ||= query
end

#to_zqlObject

Finding / Loading



73
74
75
# File 'lib/active_zuora/relation.rb', line 73

def to_zql
  select_statement + " from " + zobject_class.zuora_object_name + " " + where_statement
end

#unloadObject



102
103
104
105
# File 'lib/active_zuora/relation.rb', line 102

def unload
  @records = nil
  self
end

#update_all(attributes = {}) ⇒ Object

Updating / Deleting



140
141
142
143
144
145
146
147
148
149
# File 'lib/active_zuora/relation.rb', line 140

def update_all(attributes={})
  # Update using an attribute hash, or you can pass a block
  # and update the attributes directly on the objects.
  if block_given?
    to_a.each { |record| yield record }
  else
    to_a.each { |record| record.attributes = attributes }
  end
  zobject_class.update(to_a)
end

#where(conditions) ⇒ Object Also known as: and



29
30
31
# File 'lib/active_zuora/relation.rb', line 29

def where(conditions)
  dup.tap { |dup| dup.filters << ['and', conditions] }
end