Module: NanoService::Base::ClassMethods
- Defined in:
- lib/nano-service/base.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/nano-service/base.rb', line 24
def method_missing(method_name, *args, &block)
if instance_methods.include?(method_name)
begin
caller_object.send(method_name, *args)
rescue Exception => e
handle_exception(e)
end
else
super
end
end
|
Instance Method Details
#caller_object ⇒ Object
20
21
22
|
# File 'lib/nano-service/base.rb', line 20
def caller_object
@caller_object ||= Class.new.extend(self)
end
|
#handle_exception(e) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/nano-service/base.rb', line 36
def handle_exception(e)
log_error(e)
case e
when ActiveRecord::RecordNotFound
raise RecordNotFound, e.message
when ActiveRecord::RecordInvalid
raise RecordInvalid.new("#{e.record.class.name} #{e.message}", e.record.errors)
when ActiveRecord::RecordNotSaved
raise RecordNotSaved.new("#{e.record.class.name} #{e.message}", e.record.errors)
when URI::BadURIError
raise e.message.include?('gid') ? InvalidGlobalID.new(e.message) : e
when URI::GID::MissingModelIdError
raise InvalidGlobalID, e.message
else
raise e
end
end
|
#log_error(e, msg = nil) ⇒ Object
55
56
57
|
# File 'lib/nano-service/base.rb', line 55
def log_error(e, msg = nil)
logger.error "[#{@namespace}] #{msg}: #{e.message}"
end
|
#logger ⇒ Object
59
60
61
|
# File 'lib/nano-service/base.rb', line 59
def logger
@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end
|
#logger=(logger) ⇒ Object
63
64
65
|
# File 'lib/nano-service/base.rb', line 63
def logger=(logger)
@logger = logger
end
|