class TarvitHelpers::HashPresenter::Simple

Attributes

_hash[R]
_levels[R]
_parent[R]

Public Class Methods

new(hash, levels=[], parent=nil) click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 9
def initialize(hash, levels=[], parent=nil)
  @_hash = _modify_hash(_prepare_keys(hash))
  @_levels = levels
  @_parent = parent
end

Public Instance Methods

_root?() click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 15
def _root?
  _parent.nil?
end
method_missing(m, *args) click to toggle source
Calls superclass method
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 19
def method_missing(m, *args)
  return _value(m) if _accessor_method?(m)
  super
end

Protected Instance Methods

_accessor_method?(method_name) click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 39
def _accessor_method?(method_name)
  self._hash.keys.include?(method_name)
end
_hash_value(method_name) click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 30
def _hash_value(method_name)
  _hash[method_name]
end
_key_to_method(key) click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 43
def _key_to_method(key)
  key.to_s.gsub(/\s+/, ?_).underscore.to_sym
end
_modify_hash(hash) click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 66
def _modify_hash(hash)
  hash
end
_new_level_presenter(value, method_name) click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 58
def _new_level_presenter(value, method_name)
  _new_level_presenter_klass.new(value, _path(method_name), self)
end
_new_level_presenter_klass() click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 62
def _new_level_presenter_klass
  self.class
end
_path(key) click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 54
def _path(key)
  _levels + [ key ]
end
_prepare_keys(hash) click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 47
def _prepare_keys(hash)
  res = hash.map do |k ,v|
    [ _key_to_method(k), v ]
  end
  Hash[res]
end
_transform_value(method_name, value) click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 34
def _transform_value(method_name, value)
  return value.map{|key| _transform_value(method_name, key) } if value.is_a?(Array)
  value.is_a?(Hash) ? _new_level_presenter(value, method_name) : value
end
_value(method_name) click to toggle source
# File lib/tarvit-helpers/modules/hash_presenter/simple.rb, line 26
def _value(method_name)
  _transform_value(method_name, _hash_value(method_name))
end