Class: Beaneater::StatStruct

Inherits:
FasterOpenStruct
  • Object
show all
Defined in:
lib/beaneater/stats/stat_struct.rb

Overview

Represents a stats hash with proper underscored keys

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Beaneater::FasterOpenStruct

Class Method Details

.from_hash(hash) ⇒ Beaneater::StatStruct?

Convert a stats hash into a struct.

Examples:

s = StatStruct.from_hash(:foo => "bar")
s.foo # => 'bar'

Parameters:

  • hash (Hash{String => String})

    Hash Stats hash to convert to struct

Returns:



12
13
14
15
16
# File 'lib/beaneater/stats/stat_struct.rb', line 12

def self.from_hash(hash)
  return unless hash.is_a?(Hash)
  underscore_hash = hash.inject({}) { |r, (k, v)| r[k.to_s.gsub(/-/, '_')] = v; r }
  self.new(underscore_hash)
end

Instance Method Details

#[](key) ⇒ String, Integer

Access value for stat with specified key.

Examples:

@stats['foo'] # => "bar"

Parameters:

  • key (String)

    Key to fetch from stats.

Returns:

  • (String, Integer)

    Value for specified stat key.



25
26
27
# File 'lib/beaneater/stats/stat_struct.rb', line 25

def [](key)
  self.send(key.to_s)
end

#keysArray<String>

Returns set of keys within this struct

Examples:

@stats.keys # => ['foo', 'bar', 'baz']

Returns:

  • (Array<String>)

    Value for specified stat key.



35
36
37
# File 'lib/beaneater/stats/stat_struct.rb', line 35

def keys
  @hash.keys.map { |k| k.to_s }
end

#to_hObject

Returns the initialization hash



41
42
43
# File 'lib/beaneater/stats/stat_struct.rb', line 41

def to_h
  @hash
end