Module: Bogo::Constants

Included in:
Utility
Defined in:
lib/bogo/constants.rb

Overview

Constant helper

Instance Method Summary collapse

Instance Method Details

#const_val(name) ⇒ Object

Return constant value localized to calling instance class

Parameters:

  • name (String, Symbol)

    constant name

Returns:

  • (Object)


26
27
28
# File 'lib/bogo/constants.rb', line 26

def const_val(name)
  self.class.const_get(name)
end

#constantize(string) ⇒ Object

Convert string to constant

Parameters:

  • string (String)

    full constant name

Returns:

  • (Object)


12
13
14
15
16
17
18
19
20
# File 'lib/bogo/constants.rb', line 12

def constantize(string)
  string.split('::').inject(ObjectSpace) do |memo, key|
    begin
      memo.const_get(key)
    rescue NameError
      break
    end
  end
end

#namespace(inst = self) ⇒ Class, Module

Provides namespace constant

Parameters:

  • inst (Object) (defaults to: self)

Returns:

  • (Class, Module)


34
35
36
37
38
39
40
41
42
# File 'lib/bogo/constants.rb', line 34

def namespace(inst = self)
  klass = inst.class.name.split('::')
  klass.pop
  if(klass.empty?)
    ObjectSpace
  else
    constantize(klass.join('::'))
  end
end