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
12
13
14
# 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, []

  if field?(:created_date)
    @order_attribute, @order_direction = :created_date, :asc
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/active_zuora/relation.rb', line 160

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



154
155
156
# File 'lib/active_zuora/relation.rb', line 154

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

#dupObject



16
17
18
19
20
21
22
# File 'lib/active_zuora/relation.rb', line 16

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



80
81
82
83
# File 'lib/active_zuora/relation.rb', line 80

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

#find_each(&block) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/active_zuora/relation.rb', line 85

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)


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

def loaded?
  !@records.nil?
end

#merge(relation) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_zuora/relation.rb', line 59

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



38
39
40
# File 'lib/active_zuora/relation.rb', line 38

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

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



42
43
44
45
46
47
# File 'lib/active_zuora/relation.rb', line 42

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

#query(&block) ⇒ Object



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

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 => 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



110
111
112
113
# File 'lib/active_zuora/relation.rb', line 110

def reload
  unload.to_a
  self
end

#scopedObject



49
50
51
52
53
54
55
56
57
# File 'lib/active_zuora/relation.rb', line 49

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



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

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

#to_aObject Also known as: all



95
96
97
# File 'lib/active_zuora/relation.rb', line 95

def to_a
  @records ||= query
end

#to_zqlObject

Finding / Loading



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

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

#unloadObject



105
106
107
108
# File 'lib/active_zuora/relation.rb', line 105

def unload
  @records = nil
  self
end

#update_all(attributes = {}) ⇒ Object

Updating / Deleting



143
144
145
146
147
148
149
150
151
152
# File 'lib/active_zuora/relation.rb', line 143

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



32
33
34
# File 'lib/active_zuora/relation.rb', line 32

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