Class: Quickbooks::Model
Overview
These were all created from the info in qbxmlops70.xml, found in the docs in the QBSDK package.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(args = {}) ⇒ Model
The default for all subclasses is simply to apply the attributes given, and mark the object as a new_record?
90
91
92
|
# File 'lib/quickbooks/model.rb', line 90
def initialize(args={})
self.attributes = args
end
|
Class Method Details
.camelized_valid_filters ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/quickbooks/model.rb', line 33
def camelized_valid_filters
cvf = []
valid_filters.each do |v|
cvf << (Quickbooks::CAMELIZE_EXCEPTIONS.has_key?(v) ? Quickbooks::CAMELIZE_EXCEPTIONS[v] : (v.is_a?(Symbol) ? v.to_s.camelize.to_sym : v.split('/').map {|e| e.camelize}.join('/')))
end
cvf
end
|
.filter_aliases ⇒ Object
30
31
32
|
# File 'lib/quickbooks/model.rb', line 30
def filter_aliases
@filter_aliases ||= {}
end
|
.filter_aliases=(v) ⇒ Object
26
27
28
29
|
# File 'lib/quickbooks/model.rb', line 26
def filter_aliases=(v)
al = [v].flatten_slashes.expand_slashes.flatten_slashes
@filter_aliases = al.empty? ? {} : al
end
|
.inherited(klass) ⇒ Object
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/quickbooks/model.rb', line 9
def inherited(klass)
def klass.valid_filters
(superclass.valid_filters + (@valid_filters ||= [])).flatten_slashes
end
def klass.filter_aliases
seed = [superclass.filter_aliases].flatten_slashes.expand_slashes.flatten_slashes
(seed.empty? ? {} : seed).merge(@filter_aliases ||= {})
end
klass.instance_variable_set('@object_properties', {})
end
|
.properties ⇒ Object
82
83
84
|
# File 'lib/quickbooks/model.rb', line 82
def properties
read_only + read_write
end
|
.read_only(*args) ⇒ Object
Read-only attributes: These are attributes, but not modifiable in Quickbooks
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/quickbooks/model.rb', line 64
def read_only(*args)
if args.empty?
@read_only || (@read_only = [])
else
args.each do |prop|
if prop.is_a?(Symbol)
read_only << prop
attr_accessor prop
else
@object_properties[prop.class_leaf_name.underscore.to_sym] = prop
read_only << prop.class_leaf_name.underscore.to_sym
class_eval "def #{prop.class_leaf_name.underscore}=(v); @#{prop.class_leaf_name.underscore} = #{prop.name}.new(v); end
def #{prop.class_leaf_name.underscore}; @#{prop.class_leaf_name.underscore}; end"
end
end
end
end
|
.read_write(*args) ⇒ Object
Register multiple read/writable properties at once. For example:
read_write :first_name, :last_name, :phone, :alt_phone
For reference attributes (like parent_ref), use ParentRef - a class constant for that object. read_write will set the property setter and accessor accordingly.
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/quickbooks/model.rb', line 45
def read_write(*args)
if args.empty?
@read_write || (@read_write = [])
else
args.each do |prop|
if prop.is_a?(Symbol)
read_write << prop
attr_accessor prop
else
@object_properties[prop.class_leaf_name.underscore.to_sym] = prop
read_write << prop.class_leaf_name.underscore.to_sym
class_eval "def #{prop.class_leaf_name.underscore}=(v); @#{prop.class_leaf_name.underscore} = #{prop.name}.new(v); end
def #{prop.class_leaf_name.underscore}; @#{prop.class_leaf_name.underscore}; end"
end
end
end
end
|
.valid_filters ⇒ Object
23
24
25
|
# File 'lib/quickbooks/model.rb', line 23
def valid_filters
(@valid_filters ||= []).flatten_slashes
end
|
.valid_filters=(v) ⇒ Object
20
21
22
|
# File 'lib/quickbooks/model.rb', line 20
def valid_filters=(v)
@valid_filters = v.stringify_values!.flatten_slashes
end
|
Instance Method Details
#==(other) ⇒ Object
167
168
169
170
171
172
|
# File 'lib/quickbooks/model.rb', line 167
def ==(other)
return false unless other.is_a?(self.class)
!self.class.read_write.any? do |column|
self.instance_variable_get('@' + column.to_s) != other.instance_variable_get('@' + column.to_s)
end
end
|
#===(other) ⇒ Object
174
175
176
177
178
179
180
181
|
# File 'lib/quickbooks/model.rb', line 174
def ===(other)
if other.is_a?(Hash)
self == self.class.new(other)
else
self == other
end
end
|
#attributes(include_read_only = false) ⇒ Object
Returns a hash that represents all this object’s attributes.
95
96
97
98
99
100
101
|
# File 'lib/quickbooks/model.rb', line 95
def attributes(include_read_only=false)
attrs = {}
(include_read_only ? self.class.read_only + self.class.read_write : self.class.read_write).each do |column|
attrs[column.to_s] = instance_variable_get('@' + column.to_s)
end
attrs
end
|
#attributes=(attrs) ⇒ Object
Updates all attributes included in attrs to the values given. The object will now be dirty?.
103
104
105
106
107
108
109
110
|
# File 'lib/quickbooks/model.rb', line 103
def attributes=(attrs)
raise ArgumentError, "attributes can only be set to a hash of attributes" unless attrs.is_a?(Hash)
attrs.each do |key,value|
if self.respond_to?(key.to_s.underscore+'=')
self.send(key.to_s.underscore+'=', value)
end
end
end
|
#dirty? ⇒ Boolean
Returns true if any attributes have changed since the object was last loaded or updated from Quickbooks.
118
119
120
121
122
123
|
# File 'lib/quickbooks/model.rb', line 118
def dirty?
self.class.read_write.any? do |column|
self.instance_variable_get('@' + column.to_s) != original_values[column.to_s]
end
end
|
#dirty_attributes(compare = {}) ⇒ Object
Returns a hash of the attributes and their (new) values that have been changed since the object was last loaded or updated from Quickbooks. If you send in some attributes, it will compare to those given instead of original_attributes.
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/quickbooks/model.rb', line 127
def dirty_attributes(compare={})
compare = original_values if compare.empty?
pairs = {}
self.class.read_write.each do |column|
value = instance_variable_get('@' + column.to_s)
if value != compare[column.to_s]
pairs[column.to_s] = value
end
end
pairs
end
|
#original_values ⇒ Object
Keeps track of the original values the object had when it was instantiated from a quickbooks response. dirty? and dirty_attributes compare the current values with these ones.
113
114
115
|
# File 'lib/quickbooks/model.rb', line 113
def original_values
@original_values || (@original_values = {})
end
|
#to_dirty_hash ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/quickbooks/model.rb', line 139
def to_dirty_hash
hsh = {}
self.dirty_attributes.each do |key,value|
if value.is_a?(Quickbooks::Model)
h = value.to_dirty_hash
hsh[key] = h unless h.empty?
else
hsh[key] = value
end
end
hsh.ordered!(self.class.read_write.stringify_values)
hsh
end
|
#to_hash(include_read_only = false) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/quickbooks/model.rb', line 153
def to_hash(include_read_only=false)
hsh = {}
self.attributes(include_read_only).each do |key,value|
if value.is_a?(Quickbooks::Model)
h = value.to_hash(include_read_only)
hsh[key] = h unless h.empty?
else
hsh[key] = value
end
end
hsh.ordered!((include_read_only ? self.class.read_only + self.class.read_write : self.class.read_write).stringify_values)
hsh
end
|