Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions lib/fast_jsonapi/multi_to_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,20 @@ def rescue
end
end

def self.logger(device=nil)
return @logger = Logger.new(device) if device
@logger ||= Logger.new(IO::NULL)
def self.to_json(object)
_fast_to_json(object)
rescue NameError
define_to_json(FastJsonapi::MultiToJson)
_fast_to_json(object)
end

private
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you might mean here is to apply # @api private to the below methods, which I disagree with. It might make more sense to document intended usage, but I would say this is all a stable public API.

The private keyword has no effect on the interface.

In any case, the argument I can see for declaring it # @api private would be to say to anyone using the library not to rely on those methods being there, whether or not their public. (In Ruby, nothing's really private anyhow).


def self.define_to_json(receiver)
cl = caller_locations[0]
method_body = to_json_method
logger.debug { "Defining #{receiver}._fast_to_json as #{method_body.inspect}" }
receiver.instance_eval method_body, cl.absolute_path, cl.lineno
end

# Encoder-compatible with default MultiJSON adapters and defaults
Expand Down Expand Up @@ -76,23 +87,9 @@ def self.to_json_method
encode_method << "\nend"
end

def self.to_json(object)
_fast_to_json(object)
rescue NameError
define_to_json(FastJsonapi::MultiToJson)
_fast_to_json(object)
end

def self.define_to_json(receiver)
cl = caller_locations[0]
method_body = to_json_method
logger.debug { "Defining #{receiver}._fast_to_json as #{method_body.inspect}" }
receiver.instance_eval method_body, cl.absolute_path, cl.lineno
end

def self.reset_to_json!
undef :_fast_to_json if method_defined?(:_fast_to_json)
logger.debug { "Undefining #{receiver}._fast_to_json" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ It's not used in the normal lifecycle of the app and because there's no test in the suite, but I wouldn't say it serves no purpose. Especially if this module were made module_function and included.

I do have some tests I started writing in https://github.com/bf4/fast_multi_json but they didn't cover this specifically

def self.logger(device=nil)
return @logger = Logger.new(device) if device
@logger ||= Logger.new(IO::NULL)
end
end
end