Class: Ankit::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/ankit/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, values) ⇒ Event

Returns a new instance of Event.



33
34
35
# File 'lib/ankit/event.rb', line 33

def initialize(env, values)
  @envelope, @values = env, values
end

Instance Attribute Details

#envelopeObject (readonly)

Returns the value of attribute envelope.



20
21
22
# File 'lib/ankit/event.rb', line 20

def envelope
  @envelope
end

#valuesObject (readonly)

Returns the value of attribute values.



20
21
22
# File 'lib/ankit/event.rb', line 20

def values
  @values
end

Class Method Details

.for_card(name, verb, env) ⇒ Object



51
52
53
# File 'lib/ankit/event.rb', line 51

def self.for_card(name, verb, env)
  self.new(env, { "type" => "card", "verb" => verb, "name" => name, "maturity" => 0 })
end

.from_hash(hash) ⇒ Object



55
# File 'lib/ankit/event.rb', line 55

def self.from_hash(hash) Event.new(Envelope.from_hash(hash["envelope"]), hash["values"]); end

.parse(text) ⇒ Object



56
# File 'lib/ankit/event.rb', line 56

def self.parse(text) from_hash(JSON.parse(text)); end

.sweep(values) ⇒ Object



58
59
60
61
# File 'lib/ankit/event.rb', line 58

def self.sweep(values)
  values.delete("best") if values.include?("best") and values["best"] <= values["maturity"]
  values
end

Instance Method Details

#==(other) ⇒ Object



37
38
39
# File 'lib/ankit/event.rb', line 37

def ==(other)
  @values == other.values && @envelope == other.envelope
end

#bestObject



27
# File 'lib/ankit/event.rb', line 27

def best() @values["best"] || maturity; end

#card?Boolean

Returns:

  • (Boolean)


28
# File 'lib/ankit/event.rb', line 28

def card?() type == "card"; end

#maturityObject



26
# File 'lib/ankit/event.rb', line 26

def maturity() @values["maturity"] || 0; end

#nameObject



24
# File 'lib/ankit/event.rb', line 24

def name() @values["name"]; end

#next_roundObject

def next_round() round + 2**maturity; end



31
# File 'lib/ankit/event.rb', line 31

def next_round() round + scaled_maturity; end

#roundObject



29
# File 'lib/ankit/event.rb', line 29

def round() @envelope.round or 0; end

#to_failed(env) ⇒ Object



47
48
49
# File 'lib/ankit/event.rb', line 47

def to_failed(env)
  Event.new(env, @values.merge({ "verb" => "failed", "maturity" => -1, "best" => best }))
end

#to_json(*a) ⇒ Object



41
# File 'lib/ankit/event.rb', line 41

def to_json(*a) { envelope: @envelope, values: @values }.to_json(*a); end

#to_passed(env) ⇒ Object



43
44
45
# File 'lib/ankit/event.rb', line 43

def to_passed(env)
  Event.new(env, Event.sweep(@values.merge({ "verb" => "passed", "maturity" => next_maturity })))
end

#typeObject



25
# File 'lib/ankit/event.rb', line 25

def type() @values["type"]; end

#verbObject



22
# File 'lib/ankit/event.rb', line 22

def verb() @values["verb"]; end

#verb=(val) ⇒ Object



23
# File 'lib/ankit/event.rb', line 23

def verb=(val) @values["verb"] = val; end