Class: Xeroizer::Record::BaseModel
- Inherits:
-
Object
- Object
- Xeroizer::Record::BaseModel
- Includes:
- ClassLevelInheritableAttributes, BaseModelHttpProxy
- Defined in:
- lib/xeroizer/record/base_model.rb
Direct Known Subclasses
AccountModel, AddressModel, BankAccountModel, BankTransactionModel, BrandingThemeModel, ContactGroupModel, ContactModel, CreditNoteModel, CurrencyModel, EmployeeModel, ExternalLinkModel, InvoiceModel, ItemModel, ItemPurchaseDetailsModel, ItemSalesDetailsModel, JournalLineModel, JournalLineTrackingCategoryModel, JournalModel, LineItemModel, ManualJournalLineModel, ManualJournalModel, OptionModel, OrganisationModel, PaymentModel, PhoneModel, TaxRateModel, TrackingCategoryChildModel, TrackingCategoryModel
Defined Under Namespace
Modules: InvaidPermissionError Classes: InvalidPermissionError
Constant Summary
- ALLOWED_PERMISSIONS =
[:read, :write, :update]
- DEFAULT_RECORDS_PER_BATCH_SAVE =
2000
Instance Attribute Summary (collapse)
-
- (Object) application
readonly
Returns the value of attribute application.
-
- (Object) model_class
readonly
Returns the value of attribute model_class.
-
- (Object) model_name
readonly
Returns the value of attribute model_name.
-
- (Object) response
readonly
Returns the value of attribute response.
Class Method Summary (collapse)
-
+ (Object) set_api_controller_name(controller_name)
Method to allow override of the default controller name used in the API URLs.
-
+ (Object) set_optional_xml_root_name(optional_root_name)
Method to add an extra top-level node to use in has_many associations.
-
+ (Object) set_permissions(*args)
Set the permissions allowed for this class type.
-
+ (Object) set_xml_node_name(node_name)
Method to allow override of the default XML node name.
-
+ (Object) set_xml_root_name(root_name)
Method to allow override of the default XML root name to use in has_many associations.
Instance Method Summary (collapse)
-
- (Object) all(options = {})
Retreive full record list for this model.
-
- (Object) api_controller_name
Retrieve the controller name.
- - (Object) batch_save(chunk_size = DEFAULT_RECORDS_PER_BATCH_SAVE)
-
- (Object) build(attributes = {})
Build a record with attributes set to the value of attributes.
-
- (Object) create(attributes = {})
Create (build and save) a record with attributes set to the value of attributes.
-
- (Object) find(id, options = {})
Retrieve record matching the passed in ID.
-
- (Object) first(options = {})
Helper method to retrieve just the first element from the full record list.
-
- (BaseModel) initialize(application, model_name)
constructor
A new instance of BaseModel.
- - (Object) mark_clean(resource)
- - (Object) mark_dirty(resource)
- - (Object) parse_response(response_xml, options = {})
Methods included from BaseModelHttpProxy
Methods included from ClassLevelInheritableAttributes
Constructor Details
- (BaseModel) initialize(application, model_name)
A new instance of BaseModel
75 76 77 78 |
# File 'lib/xeroizer/record/base_model.rb', line 75 def initialize(application, model_name) @application = application @model_name = model_name end |
Instance Attribute Details
- (Object) application (readonly)
Returns the value of attribute application
26 27 28 |
# File 'lib/xeroizer/record/base_model.rb', line 26 def application @application end |
- (Object) model_class (readonly)
Returns the value of attribute model_class
28 29 30 |
# File 'lib/xeroizer/record/base_model.rb', line 28 def model_class @model_class end |
- (Object) model_name (readonly)
Returns the value of attribute model_name
27 28 29 |
# File 'lib/xeroizer/record/base_model.rb', line 27 def model_name @model_name end |
- (Object) response (readonly)
Returns the value of attribute response
29 30 31 |
# File 'lib/xeroizer/record/base_model.rb', line 29 def response @response end |
Class Method Details
+ (Object) set_api_controller_name(controller_name)
Method to allow override of the default controller name used in the API URLs.
Default: pluaralized model name (e.g. if the controller name is Invoice then the default is Invoices.
38 39 40 |
# File 'lib/xeroizer/record/base_model.rb', line 38 def set_api_controller_name(controller_name) self.api_controller_name = controller_name end |
+ (Object) set_optional_xml_root_name(optional_root_name)
Method to add an extra top-level node to use in has_many associations.
67 68 69 |
# File 'lib/xeroizer/record/base_model.rb', line 67 def set_optional_xml_root_name(optional_root_name) self.optional_root_name = optional_root_name end |
+ (Object) set_permissions(*args)
Set the permissions allowed for this class type. There are no permissions set by default. Valid permissions are :read, :write, :update.
45 46 47 48 49 50 51 |
# File 'lib/xeroizer/record/base_model.rb', line 45 def (*args) self. = {} args.each do | | raise InvalidPermissionError.new("Permission #{} is invalid.") unless ALLOWED_PERMISSIONS.include?() self.[] = true end end |
+ (Object) set_xml_node_name(node_name)
Method to allow override of the default XML node name.
Default: singularized model name in camel-case.
56 57 58 |
# File 'lib/xeroizer/record/base_model.rb', line 56 def set_xml_node_name(node_name) self.xml_node_name = node_name end |
+ (Object) set_xml_root_name(root_name)
Method to allow override of the default XML root name to use in has_many associations.
62 63 64 |
# File 'lib/xeroizer/record/base_model.rb', line 62 def set_xml_root_name(root_name) self.xml_root_name = root_name end |
Instance Method Details
- (Object) all(options = {})
Retreive full record list for this model.
118 119 120 121 122 123 |
# File 'lib/xeroizer/record/base_model.rb', line 118 def all( = {}) raise MethodNotAllowed.new(self, :all) unless self.class.[:read] response_xml = http_get(parse_params()) response = parse_response(response_xml, ) response.response_items || [] end |
- (Object) api_controller_name
Retrieve the controller name.
Default: pluaralized model name (e.g. if the controller name is Invoice then the default is Invoices.
84 85 86 |
# File 'lib/xeroizer/record/base_model.rb', line 84 def api_controller_name self.class.api_controller_name || model_name.pluralize end |
- (Object) batch_save(chunk_size = DEFAULT_RECORDS_PER_BATCH_SAVE)
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/xeroizer/record/base_model.rb', line 143 def batch_save(chunk_size = DEFAULT_RECORDS_PER_BATCH_SAVE) @objects = {} @allow_batch_operations = true yield if @objects[model_class] objects = @objects[model_class].values.compact return false unless objects.all?(&:valid?) actions = objects.group_by {|o| o.new_record? ? :http_put : :http_post } actions.each_pair do |http_method, records| records.each_slice(chunk_size) do |some_records| request = to_bulk_xml(some_records) response = parse_response(self.send(http_method, request, {:summarizeErrors => false})) response.response_items.each_with_index do |record, i| if record and record.is_a?(model_class) some_records[i].attributes = record.attributes some_records[i].errors = record.errors some_records[i].saved! end end end end end @objects = {} @allow_batch_operations = false true end |
- (Object) build(attributes = {})
Build a record with attributes set to the value of attributes.
93 94 95 96 97 |
# File 'lib/xeroizer/record/base_model.rb', line 93 def build(attributes = {}) model_class.build(attributes, self).tap do |resource| mark_dirty(resource) end end |
- (Object) create(attributes = {})
Create (build and save) a record with attributes set to the value of attributes.
113 114 115 |
# File 'lib/xeroizer/record/base_model.rb', line 113 def create(attributes = {}) build(attributes).tap { |resource| resource.save } end |
- (Object) find(id, options = {})
Retrieve record matching the passed in ID.
134 135 136 137 138 139 140 141 |
# File 'lib/xeroizer/record/base_model.rb', line 134 def find(id, = {}) raise MethodNotAllowed.new(self, :all) unless self.class.[:read] response_xml = @application.http_get(@application.client, "#{url}/#{CGI.escape(id)}", ) response = parse_response(response_xml, ) result = response.response_items.first if response.response_items.is_a?(Array) result.complete_record_downloaded = true if result result end |
- (Object) first(options = {})
Helper method to retrieve just the first element from the full record list.
127 128 129 130 131 |
# File 'lib/xeroizer/record/base_model.rb', line 127 def first( = {}) raise MethodNotAllowed.new(self, :all) unless self.class.[:read] result = all() result.first if result.is_a?(Array) end |
- (Object) mark_clean(resource)
106 107 108 109 110 |
# File 'lib/xeroizer/record/base_model.rb', line 106 def mark_clean(resource) if @objects and @objects[model_class] @objects[model_class].delete(resource.object_id) end end |
- (Object) mark_dirty(resource)
99 100 101 102 103 104 |
# File 'lib/xeroizer/record/base_model.rb', line 99 def mark_dirty(resource) if @allow_batch_operations @objects[model_class] ||= {} @objects[model_class][resource.object_id] ||= resource end end |
- (Object) parse_response(response_xml, options = {})
173 174 175 176 177 178 179 180 |
# File 'lib/xeroizer/record/base_model.rb', line 173 def parse_response(response_xml, = {}) Response.parse(response_xml, ) do | response, elements, response_model_name | if model_name == response_model_name @response = response parse_records(response, elements) end end end |