Method: Delorean::BaseModule::BaseClass._get_hash_attr

Defined in:
lib/delorean/base.rb

._get_hash_attr(obj, attr, _e, index_call = false) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/delorean/base.rb', line 125

def self._get_hash_attr(obj, attr, _e, index_call = false)
  return obj[attr] if obj.key?(attr)

  return obj[attr.to_sym] if attr.is_a?(String) && obj.key?(attr.to_sym)

  # Shouldn't try to call the method if hash['length'] was called.
  return nil if index_call

  # Return nil when it's obviously not a method
  return nil unless attr.is_a?(String) || attr.is_a?(Symbol)

  # hash.length might be either hash['length'] or hash.length call.
  # If key is not found, check if object responds to method and call it.
  # If not succeeded, return nil, assuming that it was an attribute call.
  return nil unless obj.respond_to?(attr)

  begin
    _instance_call(obj, attr, [], _e)
  rescue StandardError
    nil
  end
end