Module: Wukong::HashLike::ClassMethods

Defined in:
lib/wukong/extensions/hash_like.rb

Instance Method Summary collapse

Instance Method Details

#class_basenameObject

The last portion of the class name memoized

Examples:

This::That::TheOther.new.class_basename   # => TheOther


121
122
123
# File 'lib/wukong/extensions/hash_like.rb', line 121

def class_basename
  @class_basename ||= self.to_s.gsub(%r{.*::}, '')
end

#from_hash(hsh, has_symbol_keys = false) ⇒ Object

Instantiate an instance of the struct from a hash

Specify has_symbol_keys if the supplied hash’s keys are symbolic; otherwise they must be uniformly strings



105
106
107
108
# File 'lib/wukong/extensions/hash_like.rb', line 105

def from_hash(hsh, has_symbol_keys=false)
  extract_keys = has_symbol_keys ? self.keys.map(&:to_sym) : self.keys.map(&:to_s)
  self.new(*hsh.values_of(*extract_keys))
end

#resource_nameObject

The last portion of the class in underscored form memoized



113
114
115
# File 'lib/wukong/extensions/hash_like.rb', line 113

def resource_name
  @resource_name ||= self.class_basename.underscore.to_sym
end