Class: AddressStandardization::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/address_standardization/address.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address_info) ⇒ Address

Returns a new instance of Address.

Raises:

  • (NotImplementedError)


14
15
16
17
18
19
20
21
# File 'lib/address_standardization/address.rb', line 14

def initialize(address_info)
  raise NotImplementedError, "You must define valid_keys" unless self.class.valid_keys
  raise ArgumentError, "No address given!" if address_info.empty?
  address_info = address_info.stringify_keys
  validate_keys(address_info)
  standardize_values!(address_info)
  @address_info = address_info
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/address_standardization/address.rb', line 31

def method_missing(name, *args)
  name = name.to_s
  if self.class.valid_keys.include?(name)
    if args.empty?
      @address_info[name]
    else
      @address_info[name] = standardize_value(args.first)
    end
  else
    super(name.to_sym, *args)
  end
end

Class Attribute Details

.valid_keysObject

Returns the value of attribute valid_keys.



8
9
10
# File 'lib/address_standardization/address.rb', line 8

def valid_keys
  @valid_keys
end

Instance Attribute Details

#address_infoObject (readonly)

Returns the value of attribute address_info.



12
13
14
# File 'lib/address_standardization/address.rb', line 12

def address_info
  @address_info
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/address_standardization/address.rb', line 44

def ==(other)
  other.kind_of?(self.class) && @address_info == other.address_info
end

#validate_keys(hash) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/address_standardization/address.rb', line 23

def validate_keys(hash)
  # assume keys are already stringified
  invalid_keys = hash.keys - self.class.valid_keys
  unless invalid_keys.empty?
    raise ArgumentError, "Invalid keys: #{invalid_keys.join(', ')}. Valid keys are: #{self.class.valid_keys.join(', ')}"
  end
end