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 Stats hash to convert to struct

Returns:

  • Stats struct from hash



14
15
16
17
18
# File 'lib/beaneater/stats/stat_struct.rb', line 14

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 to fetch from stats.

Returns:

  • Value for specified stat key.



27
28
29
# File 'lib/beaneater/stats/stat_struct.rb', line 27

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

#keysArray<String>

Returns set of keys within this struct

Examples:

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

Returns:

  • Value for specified stat key.



37
38
39
# File 'lib/beaneater/stats/stat_struct.rb', line 37

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

#to_hObject

Returns the initialization hash



43
44
45
# File 'lib/beaneater/stats/stat_struct.rb', line 43

def to_h
  @hash
end