Class: Rentlinx::Base
- Inherits:
-
Object
- Object
- Rentlinx::Base
- Defined in:
- lib/rentlinx/models/base.rb
Overview
This is the class on which all other Rentlinx classes are based, it encapsulates much of the important logic used by every class.
It should never be instantiated on its own, use one of its subclasses.
Direct Known Subclasses
Company, Lead, Property, PropertyAmenity, PropertyLink, PropertyPhoto, Unit
Class Method Summary collapse
Instance Method Summary collapse
-
#attributes ⇒ Object
Provides a list of attributes supported by the class.
-
#initialize(attrs) ⇒ Base
constructor
Creates a new instance of the class, but the base class should never be directly instantiated.
-
#patch ⇒ Object
Sends the object to Rentlinx.
-
#patch_valid? ⇒ Boolean
Determines the validity of the object for patch.
-
#post ⇒ Object
Sends the object to Rentlinx.
-
#required_attributes ⇒ Object
Provides the list of required attributes for the class (a subset of Class.attributes).
-
#to_hash ⇒ Object
Converts the object to a hash.
-
#unpost ⇒ Object
Removes the object from Rentlinx.
-
#valid? ⇒ Boolean
Determines the validity of the object for post.
-
#validate(check_required_attributes = true) ⇒ Object
Provides error messages on invalid objects.
Constructor Details
#initialize(attrs) ⇒ Base
Creates a new instance of the class, but the base class should never be directly instantiated.
This method takes in the attributes for the new instance, runs them through the AttributeProcessor and saves them as instance variables.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rentlinx/models/base.rb', line 18 def initialize(attrs) @original_attrs = attrs.dup @processor = AttributeProcessor.new(attrs.dup) attrs = @processor.process attributes.each do |at| send("#{at}=", attrs[at]) end remaining_attrs = attrs.keys - attributes raise UnexpectedAttributes, "Unexpected Attributes: #{remaining_attrs.join(', ')}" unless remaining_attrs.compact.empty? end |
Class Method Details
.get_from_id(type, id) ⇒ Object
125 126 127 |
# File 'lib/rentlinx/models/base.rb', line 125 def get_from_id(type, id) Rentlinx.client.get(type.to_sym, id) end |
.type ⇒ Object
129 130 131 |
# File 'lib/rentlinx/models/base.rb', line 129 def type name.split('::').last.downcase.to_sym end |
Instance Method Details
#attributes ⇒ Object
Provides a list of attributes supported by the class.
32 33 34 |
# File 'lib/rentlinx/models/base.rb', line 32 def attributes self.class::ATTRIBUTES end |
#patch ⇒ Object
Sends the object to Rentlinx
48 49 50 |
# File 'lib/rentlinx/models/base.rb', line 48 def patch Rentlinx.client.patch(self) end |
#patch_valid? ⇒ Boolean
Determines the validity of the object for patch
91 92 93 |
# File 'lib/rentlinx/models/base.rb', line 91 def patch_valid? validate(false).empty? end |
#post ⇒ Object
Sends the object to Rentlinx
57 58 59 |
# File 'lib/rentlinx/models/base.rb', line 57 def post Rentlinx.client.post(self) end |
#required_attributes ⇒ Object
Provides the list of required attributes for the class (a subset of Class.attributes)
39 40 41 |
# File 'lib/rentlinx/models/base.rb', line 39 def required_attributes self.class::REQUIRED_ATTRIBUTES end |
#to_hash ⇒ Object
Converts the object to a hash
73 74 75 76 77 78 79 |
# File 'lib/rentlinx/models/base.rb', line 73 def to_hash {}.tap do |hash| @original_attrs.keys.each do |at| hash[at] = send(at) end end end |
#unpost ⇒ Object
Removes the object from Rentlinx
66 67 68 |
# File 'lib/rentlinx/models/base.rb', line 66 def unpost Rentlinx.client.unpost(type, send(identity)) end |
#valid? ⇒ Boolean
Determines the validity of the object for post
84 85 86 |
# File 'lib/rentlinx/models/base.rb', line 84 def valid? validate.empty? end |
#validate(check_required_attributes = true) ⇒ Object
Provides error messages on invalid objects
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rentlinx/models/base.rb', line 99 def validate(check_required_attributes = true) @processor = AttributeProcessor.new(to_hash) @processor.process # object identity is always required, even for patches missing_errors = {} missing_errors[identity.to_sym] = 'is missing' if self.class.method_defined?(identity) && blank?(send(identity)) if check_required_attributes missing_attrs = required_attributes.select { |at| blank?(send(at)) } missing_attrs.each do |at| missing_errors[at] = 'is missing' end end @processor.errors.merge(missing_errors) end |