Class: Delorean::BaseModule::BaseClass

Inherits:
Object
  • Object
show all
Defined in:
lib/delorean/base.rb

Class Method Summary collapse

Class Method Details

._err(*args) ⇒ Object



186
187
188
# File 'lib/delorean/base.rb', line 186

def self._err(*args)
  ::Delorean::Ruby.error_handler.call(*args)
end

._get_attr(obj, attr, _e) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/delorean/base.rb', line 92

def self._get_attr(obj, attr, _e)
  # REALLY FIXME: this really needs to be another "when" in the
  # case statement below. However, Gemini appears to create Hash
  # objects when running Delorean modules in delayed jobs that
  # return true when we called obj.instance_of?(Hash) and do not
  # work with the "case/when" matcher!!!  For now, this is a
  # hacky workaround.  This is likely some sort of Ruby bug.
  return _get_hash_attr(obj, attr, _e) if obj.instance_of?(Hash)

  # NOTE: should keep this function consistent with _index
  case obj
  when nil
    # FIXME: even Javascript which is superpermissive raises an
    # exception on null getattr.
    return nil
  when NodeCall
    return obj.evaluate(attr)
  when OpenStruct
    return obj[attr.to_sym]
  when Class
    return obj.send((attr + POST).to_sym, _e) if obj < BaseClass
  end

  begin
    _instance_call(obj, attr, [], _e)
  rescue StandardError => exc
    raise(
      InvalidGetAttribute,
      "attr lookup failed: '#{attr}' on <#{obj.class}> #{obj} - #{exc}"
    )
  end
end

._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

._index(obj, args, _e) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/delorean/base.rb', line 150

def self._index(obj, args, _e)
  # NOTE: should keep this function consistent with _get_attr
  case obj
  when nil
    # FIXME: even Javascript which is superpermissive raises an
    # exception on null getattr.
    nil
  when Hash
    raise InvalidIndex unless args.length == 1

    _get_hash_attr(obj, args[0], _e, true)
  when NodeCall, Class, OpenStruct
    raise InvalidIndex unless args.length == 1

    _get_attr(obj, args[0], _e)
  when Array, String, MatchData
    raise InvalidIndex unless args.length <= 2 &&
                              args[0].is_a?(Integer) &&
                              (args[1].nil? || args[1].is_a?(Integer))

    obj[*args]
  else
    raise InvalidIndex
  end
end

._instance_call(obj, method, args, _e, &block) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/delorean/base.rb', line 210

def self._instance_call(obj, method, args, _e, &block)
  begin
    msg = method.to_sym
  rescue NoMethodError
    raise "bad method #{method}"
  end

  if obj.is_a?(Class) || obj.is_a?(Module)
    matcher = ::Delorean::Ruby.whitelist.class_method_matcher(
      method_name: msg
    )
    klass = obj
  else
    matcher = ::Delorean::Ruby.whitelist.matcher(method_name: msg)
    klass = obj.class
  end

  raise "no such method #{method}" unless matcher

  if matcher.match_to?
    if block
      return(
        _instance_call(obj, matcher.match_to, args, _e, &block)
      )
    else
      return(
        _instance_call(obj, matcher.match_to, args, _e)
      )
    end
  end

  matcher.match!(klass: klass, args: args)

  return obj.public_send(msg, *args) unless block

  obj.public_send(msg, *args, &block)
end

._node_call(node, _e, params) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/delorean/base.rb', line 190

def self._node_call(node, _e, params)
  context = _e[:_engine]

  # a node call is being called with amended args
  return node + params if node.is_a?(NodeCall)

  engine = node.is_a?(Class) &&
    context.module_name != node.module_name ?
  context.get_import_engine(node.module_name) : context

  NodeCall.new(_e, engine, node || self, params)
end

._safe_navigation_get_attr(obj, attr, _e) ⇒ Object



86
87
88
89
90
# File 'lib/delorean/base.rb', line 86

def self._safe_navigation_get_attr(obj, attr, _e)
  return nil if obj.nil?

  _get_attr(obj, attr, _e)
end

._safe_navigation_instance_call(obj, method, args, _e, &block) ⇒ Object



204
205
206
207
208
# File 'lib/delorean/base.rb', line 204

def self._safe_navigation_instance_call(obj, method, args, _e, &block)
  return nil if obj.nil?

  _instance_call(obj, method, args, _e, &block)
end

._sanitize_hash(_e) ⇒ Object



178
179
180
181
182
# File 'lib/delorean/base.rb', line 178

def self._sanitize_hash(_e)
  _e.each_with_object({}) do |(k, v), h|
    h[k] = v if k.is_a?(Integer) || k =~ /\A[a-z][A-Za-z0-9_]*\z/
  end
end