Class: Rentlinx::AttributeProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/rentlinx/validators/attribute_processor_service.rb

Overview

Provides processing and validation for attributes on Property and Unit objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ AttributeProcessor

Returns a new instance of AttributeProcessor.

Parameters:

  • attrs (Hash)

    the attributes to be processed



18
19
20
21
22
# File 'lib/rentlinx/validators/attribute_processor_service.rb', line 18

def initialize(attrs)
  @attrs = attrs
  @processed_attrs = @attrs
  @errors = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



14
15
16
# File 'lib/rentlinx/validators/attribute_processor_service.rb', line 14

def errors
  @errors
end

#processed_attrsObject (readonly)

Returns the value of attribute processed_attrs.



15
16
17
# File 'lib/rentlinx/validators/attribute_processor_service.rb', line 15

def processed_attrs
  @processed_attrs
end

Instance Method Details

#processObject

Processes the attributes, both validating them and processing them into a format suitable for Rentlinx.

This method also populates the errors attribute, so it must be run before calling .valid? or querying for errors.

Currently, phone number, state, zip, and leads URL are validated. They are passed into their own validator objects: PhoneValidator, StateValidator, ZipValidator, and UrlValidator

Returns:

  • the processed attributes ready to submit to Rentlinx



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rentlinx/validators/attribute_processor_service.rb', line 35

def process
  validators = { phoneNumber: PhoneValidator,
                 state: StateValidator,
                 zip: ZipValidator,
                 leadsURL: UrlValidator,
                 city: CityValidator,
                 address: AddressValidator,
                 capAmount: AmountValidator,
                 companyCapAmount: AmountValidator }

  @attrs.each do |field, val|
    next unless validators.keys.include?(field)
    validator = validators[field].new(val)
    @processed_attrs[field] = validator.processed_value
    @errors[field] = validator.error unless validator.valid?
  end

  @processed_attrs
end

#valid?Boolean

Determines the validity of the attributes it contains.

Returns:

  • (Boolean)

    true if valid, false if not



58
59
60
# File 'lib/rentlinx/validators/attribute_processor_service.rb', line 58

def valid?
  @errors.empty?
end