Class: Stats::Stat
Overview
Describes a bit of high level information about the universe
Instance Attribute Summary collapse
-
#description ⇒ Object
description of the statistic.
-
#generator ⇒ Object
method used to generator the statistic.
-
#id ⇒ Object
id of the statistic.
Class Method Summary collapse
-
.json_create(o) ⇒ Object
Create new stat from json representation.
Instance Method Summary collapse
-
#generate(*args) ⇒ Object
Invoke generator w/ the specified params to collect stat and return value.
-
#initialize(args = {}) ⇒ Stat
constructor
Statistic initializer.
-
#to_json(*a) ⇒ Object
Convert stat to json representation and return it.
Constructor Details
#initialize(args = {}) ⇒ Stat
Statistic initializer
35 36 37 38 39 40 |
# File 'lib/stats/stat.rb', line 35 def initialize(args = {}) attr_from_args args, :id => nil, :description => nil, :generator => nil end |
Instance Attribute Details
#description ⇒ Object
description of the statistic
16 17 18 |
# File 'lib/stats/stat.rb', line 16 def description @description end |
#generator ⇒ Object
method used to generator the statistic
19 20 21 |
# File 'lib/stats/stat.rb', line 19 def generator @generator end |
Class Method Details
.json_create(o) ⇒ Object
Create new stat from json representation
52 53 54 55 |
# File 'lib/stats/stat.rb', line 52 def self.json_create(o) stat = new(o['data']) return stat end |
Instance Method Details
#generate(*args) ⇒ Object
Invoke generator w/ the specified params to collect stat and return value.
22 23 24 25 26 27 |
# File 'lib/stats/stat.rb', line 22 def generate(*args) # TODO support for caching results for a period (would require concurrent access protection) value = @generator.call(*args) StatResult.new :stat_id => self.id, :stat => self, :args => args, :value => value end |
#to_json(*a) ⇒ Object
Convert stat to json representation and return it
43 44 45 46 47 48 49 |
# File 'lib/stats/stat.rb', line 43 def to_json(*a) { 'json_class' => self.class.name, 'data' => {:id => id, :description => description} }.to_json(*a) end |