Class: Rentlinx::Base

Inherits:
Object
  • Object
show all
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.

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • attrs (Hash)

    a hash of attributes to save.

Raises:



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

.typeObject



129
130
131
# File 'lib/rentlinx/models/base.rb', line 129

def type
  name.split('::').last.downcase.to_sym
end

.unpost(id) ⇒ Object



121
122
123
# File 'lib/rentlinx/models/base.rb', line 121

def unpost(id)
  Rentlinx.client.unpost(type, id)
end

Instance Method Details

#attributesObject

Provides a list of attributes supported by the class.

Returns:

  • an array of attribute keys



32
33
34
# File 'lib/rentlinx/models/base.rb', line 32

def attributes
  self.class::ATTRIBUTES
end

#patchObject

Sends the object to Rentlinx

Examples:

prop = Rentlinx::Property.new(attrs)
prop.patch


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

Returns:

  • (Boolean)

    true if valid, false if invalid



91
92
93
# File 'lib/rentlinx/models/base.rb', line 91

def patch_valid?
  validate(false).empty?
end

#postObject

Sends the object to Rentlinx

Examples:

prop = Rentlinx::Property.new(attrs)
prop.post


57
58
59
# File 'lib/rentlinx/models/base.rb', line 57

def post
  Rentlinx.client.post(self)
end

#required_attributesObject

Provides the list of required attributes for the class (a subset of Class.attributes)

Returns:

  • an array of attribute keys



39
40
41
# File 'lib/rentlinx/models/base.rb', line 39

def required_attributes
  self.class::REQUIRED_ATTRIBUTES
end

#to_hashObject

Converts the object to a hash

Returns:

  • a hash including the attributes and values



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

#unpostObject

Removes the object from Rentlinx

Examples:

prop = Rentlinx::Property.new(attrs)
prop.unpost


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

Returns:

  • (Boolean)

    true if valid, false if invalid



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

Parameters:

  • check_required_attributes (defaults to: true)

    whether to check for required attributes

Returns:

  • a hash of error messages



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