Module: YmlErrorResponder::YmlMapper

Extended by:
YmlMapper
Included in:
YmlMapper
Defined in:
lib/yml_error_responder/yml_mapper.rb

Constant Summary collapse

FILE_MASK =
'**/*.yml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errors_handlersObject (readonly)

Returns the value of attribute errors_handlers.



4
5
6
# File 'lib/yml_error_responder/yml_mapper.rb', line 4

def errors_handlers
  @errors_handlers
end

Instance Method Details

#add_handler(handlers) ⇒ Object



49
50
51
# File 'lib/yml_error_responder/yml_mapper.rb', line 49

def add_handler(handlers)
  @errors_handlers.merge!(handlers) if handlers.present?
end

#error_defined?(error) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/yml_error_responder/yml_mapper.rb', line 13

def error_defined?(error)
  @errors_handlers.key?(error.class.to_s)
end

#error_payload(error) ⇒ Object



9
10
11
# File 'lib/yml_error_responder/yml_mapper.rb', line 9

def error_payload(error)
  @errors_handlers[error.class.to_s]
end

#load!Object



17
18
19
20
21
22
23
# File 'lib/yml_error_responder/yml_mapper.rb', line 17

def load!
  raise 'Configuration path is not set!' if YmlErrorResponder.configuration_path.blank?

  read_dir do |path|
    add_handler(load_handlers(path, path_to_namespace(path)))
  end
end

#load_handlers(path, namespace) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/yml_error_responder/yml_mapper.rb', line 31

def load_handlers(path, namespace)
  YAML
    .load_file(path)
    .try(:transform_keys) do |key|
      namespace.blank? ? key.to_s.camelize : [namespace, key.to_s.camelize].join('::')
    end
end

#path_to_namespace(path) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/yml_error_responder/yml_mapper.rb', line 39

def path_to_namespace(path)
  path.match(/#{YmlErrorResponder.configuration_path}([^.]*)\/(.*).yml/)
    .try(:[], 1)
    .try do |match|
      match.split('/')
      .map{|m| m.camelize}
      .join('::')
    end
end

#read_dirObject



25
26
27
28
29
# File 'lib/yml_error_responder/yml_mapper.rb', line 25

def read_dir
  Dir[[YmlErrorResponder.configuration_path, FILE_MASK].join].each do |path|
    yield path
  end
end