Class: GoodGuide::Gibbon::Program

Inherits:
Object
  • Object
show all
Defined in:
lib/goodguide/gibbon.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info) ⇒ Program

Returns a new instance of Program.



237
238
239
240
241
# File 'lib/goodguide/gibbon.rb', line 237

def initialize(info)
  @syntax = info[:syntax] or raise 'please pass syntax'
  @semantics = info[:semantics] or raise 'please pass semantics'
  @ruby_code = info[:ruby] or raise 'please pass ruby code'
end

Instance Attribute Details

#ruby_codeObject (readonly)

Returns the value of attribute ruby_code.



235
236
237
# File 'lib/goodguide/gibbon.rb', line 235

def ruby_code
  @ruby_code
end

#semanticsObject (readonly)

Returns the value of attribute semantics.



235
236
237
# File 'lib/goodguide/gibbon.rb', line 235

def semantics
  @semantics
end

#syntaxObject (readonly)

Returns the value of attribute syntax.



235
236
237
# File 'lib/goodguide/gibbon.rb', line 235

def syntax
  @syntax
end

Class Method Details

.compile(source, info = {}) ⇒ Object

Raises:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/goodguide/gibbon.rb', line 215

def self.compile(source, info={})
  client = info[:client] || raise('please pass a static client')
  global = info[:global] || raise('please pass a global table id')

  context = info[:context] || Context.new

  syntax = context.parse(source)
  errors, semantics = context.analyze(syntax, global, client.to_js(context.gibbon))

  raise SemanticError.new(errors, context) if errors

  ruby_code = context.compile_ruby(semantics)

  Program.new(
    syntax: syntax,
    semantics: semantics,
    ruby: ruby_code
  )
end

.from_json(hash) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/goodguide/gibbon.rb', line 251

def self.from_json(hash)
  new(
    :syntax => hash['syntax'],
    :semantics => hash['semantics'],
    :ruby => hash['ruby'],
  )
end

Instance Method Details

#as_jsonObject



243
244
245
246
247
248
249
# File 'lib/goodguide/gibbon.rb', line 243

def as_json
  @as_json ||= {
    'syntax' => @syntax,
    'semantics' => @semantics,
    'ruby' => @ruby_code,
  }
end

#call(runtime_client, entity_id) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/goodguide/gibbon.rb', line 263

def call(runtime_client, entity_id)
  deps, results = runtime_client.with_dependencies do
    runner.run!(runtime_client, entity_id)
  end

  out = {}
  results.each do |key, result|
    out[key] = { 'dependencies' => deps[key] || [] }

    case result[:status]
    when :success
      out[key].merge! 'status' => 'success', 'value' => result[:value]
    when :failure
      out[key].merge! 'status' => 'failure'
    end
  end

  out
end

#runnerObject



259
260
261
# File 'lib/goodguide/gibbon.rb', line 259

def runner
  @runner ||= Runner.new(ruby_code)
end