Module: Obfusk::Util::BetterStruct
- Defined in:
- lib/obfusk/util/struct.rb
Overview
better Struct; create using Obfusk::Util.struct
Defined Under Namespace
Modules: ClassMethods Classes: IncompleteError
Class Method Summary collapse
-
.included(base) ⇒ Object
include class methods as well.
Instance Method Summary collapse
-
#check! ⇒ Object
checks for missing fields, returns self.
-
#to_h ⇒ Object
convert to hash (ruby 2 has this already).
-
#to_str_h ⇒ Object
convert to hash w/ string keys.
Class Method Details
.included(base) ⇒ Object
include class methods as well
23 24 25 |
# File 'lib/obfusk/util/struct.rb', line 23 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#check! ⇒ Object
checks for missing fields, returns self
39 40 41 42 43 |
# File 'lib/obfusk/util/struct.rb', line 39 def check! members.each do |f| self[f].nil? and raise IncompleteError, "empty field: #{f}" end; self end |
#to_h ⇒ Object
convert to hash (ruby 2 has this already)
52 53 54 |
# File 'lib/obfusk/util/struct.rb', line 52 def to_h Hash[each_pair.to_a] end |
#to_str_h ⇒ Object
convert to hash w/ string keys
58 59 60 |
# File 'lib/obfusk/util/struct.rb', line 58 def to_str_h Hash[to_h.map { |k,v| [k.to_s,v] }] end |