Class: Beaneater::StatStruct
- Inherits:
-
FasterOpenStruct
- Object
- FasterOpenStruct
- Beaneater::StatStruct
- Defined in:
- lib/beaneater/stats/stat_struct.rb
Overview
Represents a stats hash with proper underscored keys
Class Method Summary collapse
-
.from_hash(hash) ⇒ Beaneater::StatStruct?
Convert a stats hash into a struct.
Instance Method Summary collapse
-
#[](key) ⇒ String, Integer
Access value for stat with specified key.
-
#keys ⇒ Array<String>
Returns set of keys within this struct.
-
#to_h ⇒ Object
Returns the initialization hash.
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.
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.
25 26 27 |
# File 'lib/beaneater/stats/stat_struct.rb', line 25 def [](key) self.send(key.to_s) end |
#keys ⇒ Array<String>
Returns set of keys within this struct
35 36 37 |
# File 'lib/beaneater/stats/stat_struct.rb', line 35 def keys @hash.keys.map { |k| k.to_s } end |
#to_h ⇒ Object
Returns the initialization hash
41 42 43 |
# File 'lib/beaneater/stats/stat_struct.rb', line 41 def to_h @hash end |