class Objectable::Interface

This class defines the direct interface with an object. Unlike the resolver, this class

cannot handle traversal or nesting, it can only deal with the immediate object interface.

Public Instance Methods

get(object, key) click to toggle source
# File lib/objectable/interface.rb, line 14
def get(object, key)
  if object.is_a?(Hash)
    indifferent_hash_get(object, key)
  elsif object.respond_to?(key)
    object.public_send(key)
  end
end
set(object, key, val) click to toggle source
# File lib/objectable/interface.rb, line 22
def set(object, key, val)
  object.tap do |o|
    setter_method = "#{key}="
    if o.respond_to?(setter_method)
      o.send(setter_method, val)
    elsif o.respond_to?(:[])
      o[key] = val
    end
  end
end

Private Instance Methods

indifferent_hash_get(hash, key) click to toggle source
# File lib/objectable/interface.rb, line 35
def indifferent_hash_get(hash, key)
  if hash.key?(key.to_s)
    hash[key.to_s]
  elsif hash.key?(key.to_s.to_sym)
    hash[key.to_s.to_sym]
  end
end