class Beaneater::StatStruct

Represents a stats hash with proper underscored keys

Public Class Methods

from_hash(hash) click to toggle source

Convert a stats hash into a struct.

@param [Hash{String => String}] hash Hash Stats hash to convert to struct @return [Beaneater::StatStruct, nil] Stats struct from hash @example

s = StatStruct.from_hash(:foo => "bar")
s.foo # => 'bar'
# 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

Public Instance Methods

[](key) click to toggle source

Access value for stat with specified key.

@param [String] key Key to fetch from stats. @return [String, Integer] Value for specified stat key. @example

@stats['foo'] # => "bar"
# File lib/beaneater/stats/stat_struct.rb, line 25
def [](key)
  self.send(key.to_s)
end
keys() click to toggle source

Returns set of keys within this struct

@return [Array<String>] Value for specified stat key. @example

@stats.keys # => ['foo', 'bar', 'baz']
# File lib/beaneater/stats/stat_struct.rb, line 35
def keys
  @hash.keys.map { |k| k.to_s }
end
to_h() click to toggle source

Returns the initialization hash

# File lib/beaneater/stats/stat_struct.rb, line 41
def to_h
  @hash
end