Class: EvalIn::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/eval_in/result.rb

Overview

The data structure containing the final result its attributes default to null-objects for their given type

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Result

Returns a new instance of Result.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/eval_in/result.rb', line 13

def initialize(attributes={})
  attributes = attributes.dup
  self.exitstatus         = attributes.delete(:exitstatus)        || -1
  self.language           = attributes.delete(:language)          || ""
  self.language_friendly  = attributes.delete(:language_friendly) || ""
  self.code               = attributes.delete(:code)              || ""
  self.output             = attributes.delete(:output)            || ""
  self.status             = attributes.delete(:status)            || ""
  self.url                = attributes.delete(:url)               || ""
  stderr                  = attributes.delete(:stderr)            || $stderr
  stderr.puts "Unexpected attributes! #{attributes.keys.inspect}" if attributes.any?
end

Class Attribute Details

.attribute_namesObject (readonly)

Returns the value of attribute attribute_names.



10
11
12
# File 'lib/eval_in/result.rb', line 10

def attribute_names
  @attribute_names
end

Instance Method Details

#as_jsonObject

Returns representation of the result built out of JSON primitives (hash, string, int) This is useful for composing JSON structures and only rendering the JSON string at the end



28
29
30
31
32
# File 'lib/eval_in/result.rb', line 28

def as_json
  self.class.attribute_names.each_with_object Hash.new do |name, attributes|
    attributes[name.to_s] = public_send name
  end
end

#to_jsonObject



34
35
36
# File 'lib/eval_in/result.rb', line 34

def to_json
  JSON.dump(as_json)
end