Module: Justimmo::API::Mapper

Included in:
RealtyMapper
Defined in:
lib/justimmo/api/v1/mapper.rb

Overview

Maps values to other values.

Defined Under Namespace

Classes: KeyNotFound, MappingNotFound

Constant Summary collapse

DICTIONARY_PATH =
File.join(__dir__, 'dictionaries')

Instance Method Summary collapse

Instance Method Details

#dictionaryString

The path to the dictionary for this mapper.


45
46
47
48
# File 'lib/justimmo/api/v1/mapper.rb', line 45

def dictionary
  filename = "#{module_name.underscore.split('_').first}.yml"
  File.join(DICTIONARY_PATH, filename)
end

#generic_mappingHash

The content of the generic dicitonary for all mappers.


58
59
60
61
# File 'lib/justimmo/api/v1/mapper.rb', line 58

def generic_mapping
  hash = YAML.load_file(File.join(DICTIONARY_PATH, 'generic.yml'))
  hash.with_indifferent_access
end

#get(key, map: :main, reverse: false) ⇒ Symbol Also known as: []

Get a key from a mapping.

Raises:

  • (KeyNotFound)

    If on_mapper_error is set to :raise.


94
95
96
97
98
99
100
101
102
103
# File 'lib/justimmo/api/v1/mapper.rb', line 94

def get(key, map: :main, reverse: false)
  return nil unless key.respond_to?(:to_s)
  handle_key_error(key, map) do
    if reverse
      mapping(map).key(key.to_s)&.to_sym
    else
      mapping(map).fetch(key, nil)&.to_sym
    end
  end
end

#handle_key_error(key, map) ⇒ String, Symbol

Handle key lookup errors.

Raises:

  • (KeyNotFoundError)

    If on_mapper_error is set to :raise.


129
130
131
132
133
134
135
136
137
# File 'lib/justimmo/api/v1/mapper.rb', line 129

def handle_key_error(key, map)
  val = yield
  return val.to_sym unless val.nil?
  case on_mapper_error
  when :raise  then raise KeyNotFound.new(key, map)
  when :mark   then "!#{key.upcase}"
  else         key.respond_to?(:to_sym) ? key.to_sym : key
  end
end

#keys(map = :main) ⇒ Object


107
108
109
110
# File 'lib/justimmo/api/v1/mapper.rb', line 107

def keys(map = :main)
  return [] unless mapping?(map)
  mapping(map).keys.map(&:to_sym)
end

#load_dictionaryHash

The content of the dictionary file as Ruby hash.


52
53
54
# File 'lib/justimmo/api/v1/mapper.rb', line 52

def load_dictionary
  YAML.load_file(dictionary)
end

#mapping(map) ⇒ Hash

Gets a specific mapping from the mappings hash.

Raises:


77
78
79
80
81
# File 'lib/justimmo/api/v1/mapper.rb', line 77

def mapping(map)
  mappings.fetch(map)
rescue KeyError
  raise MappingNotFound, map
end

#mapping?(map) ⇒ true, false

Check if a mapping is available.


120
121
122
# File 'lib/justimmo/api/v1/mapper.rb', line 120

def mapping?(map)
  mappings.key?(map)
end

#mappingsHash

Get all mappings from this mapper. Will initialize on first call.


65
66
67
68
69
70
71
# File 'lib/justimmo/api/v1/mapper.rb', line 65

def mappings
  return @mappings unless @mappings.nil?
  @mappings ||= generic_mapping
  @mappings.deep_merge!(load_dictionary)
  @mappings = @mappings.with_indifferent_access
  @mappings.freeze
end

#module_nameString

The module name without the leading path


39
40
41
# File 'lib/justimmo/api/v1/mapper.rb', line 39

def module_name
  name.split('::').last
end

#on_mapper_error(key = nil) ⇒ Symbol

Defines the action that's being used on errors


31
32
33
34
35
# File 'lib/justimmo/api/v1/mapper.rb', line 31

def on_mapper_error(key = nil)
  @on_mapper_error ||= Justimmo::Config.on_mapper_error
  return @on_mapper_error unless key.respond_to?(:to_sym)
  key.nil? ? @on_mapper_error : @on_mapper_error = key.to_sym
end

#values(map = :main) ⇒ Object


112
113
114
115
# File 'lib/justimmo/api/v1/mapper.rb', line 112

def values(map = :main)
  return [] unless mapping?(map)
  mapping(map).values.map(&:to_sym)
end