Class: Stats::Stat

Inherits:
Object show all
Defined in:
lib/stats/stat.rb

Overview

Describes a bit of high level information about the universe

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Stat

Statistic initializer

Options Hash (args):

  • :id,'id' (String)

    id to assign to the stat

  • :description,'description' (String)

    description to assign to the stat

  • :generator,'generator' (Callable)

    generator to assign to the stat


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

#descriptionObject

description of the statistic


16
17
18
# File 'lib/stats/stat.rb', line 16

def description
  @description
end

#generatorObject

method used to generator the statistic


19
20
21
# File 'lib/stats/stat.rb', line 19

def generator
  @generator
end

#idObject

id of the statistic


13
14
15
# File 'lib/stats/stat.rb', line 13

def id
  @id
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