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



133
134
135
136
# File 'lib/delorean/base.rb', line 133

def self._err(*args)
  str = args.map(&:to_s).join(", ")
  raise str
end

._get_attr(obj, attr, _e) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/delorean/base.rb', line 63

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.
  if obj.instance_of?(Hash)
    # FIXME: this implementation doesn't handle something like
    # {}.length.  i.e. length is a whitelisted function, but not
    # an attr. This implementation returns nil instead of 0.
    return obj[attr] if obj.member?(attr)
    return attr.is_a?(String) ? obj[attr.to_sym] : nil
  end

  # 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
    return _instance_call(obj, attr, [], _e)
  rescue => exc
    raise InvalidGetAttribute,
    "attr lookup failed: '#{attr}' on <#{obj.class}> #{obj} - #{exc}"
  end
end

._index(obj, args, _e) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/delorean/base.rb', line 102

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.
    return nil
  when Hash, 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) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/delorean/base.rb', line 153

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

  # FIXME: this is pretty hacky -- should probably merge
  # whitelist and SIG mechanisms.
  if obj.is_a?(Class)
    _e[:_engine].parse_check_call_fn(method, args.count, obj)
    return obj.send(msg, *args)
  end

  cls = obj.class

  matcher = ::Delorean::Ruby.whitelist.matcher(method_name: msg)

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

  return(
    _instance_call(obj, matcher.match_to, args, _e)
  ) if matcher.match_to?

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

  obj.send(msg, *args)
end

._node_call(node, _e, params) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/delorean/base.rb', line 138

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

._sanitize_hash(_e) ⇒ Object



124
125
126
127
128
129
# File 'lib/delorean/base.rb', line 124

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