Skip to content

Commit

Permalink
in 1.8 every object responds to #id. it was renamed to #object_id in 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
elucid committed Sep 8, 2011
1 parent 02176c4 commit 3a33ec4
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/boffin/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ def blank?(obj)
obj.respond_to?(:empty?) ? obj.empty? : !obj
end

# @param [Object] obj any Ruby object
# @return [true, false]
# `true` if the provided object responds to :id, other than it's
# internal object identifier
# `false` if the object does not respond to :id
def respond_to_id?(obj)
# NOTE: this feels like a hack. I'm sure there is a more elegant way
# to determine whether the :id method is the built in Object#id but
# I can't think of it
obj.respond_to?(:id) and obj.id != obj.object_id
end

# Pulls time interval information from a hash of options.
# @example
# extract_time_unit(this: 'is ignored', days: 6, so_is: 'this')
Expand Down Expand Up @@ -134,7 +146,7 @@ def object_as_namespace(obj)
# generated value will be Base64 encoded.
# @return [String]
def object_as_identifier(obj, opts = {})
if obj.respond_to?(:as_member) || obj.respond_to?(:id)
if obj.respond_to?(:as_member) || respond_to_id?(obj)
''.tap do |s|
s << "#{underscore(obj.class)}:" if opts[:namespace]
s << (obj.respond_to?(:as_member) ? obj.as_member : obj.id).to_s
Expand Down

0 comments on commit 3a33ec4

Please sign in to comment.