Module: Constant

Included in:
Literal, Module
Defined in:
lib/constant/coerce.rb,
lib/constant/define.rb,
lib/constant/import.rb,
lib/constant/module.rb,
lib/constant/literal.rb,
lib/constant/constant.rb,
lib/constant/import/macro.rb,
lib/constant/controls/constant.rb

Defined Under Namespace

Modules: Coerce, Controls, Define, Import Classes: Literal, Module

Constant Summary collapse

Error =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__nameObject



5
# File 'lib/constant/constant.rb', line 5

alias __name name

.defined?(name, namespace_name_or_module = nil, inherit: nil) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
# File 'lib/constant/constant.rb', line 51

def self.defined?(name, namespace_name_or_module=nil, inherit: nil)
  namespace_name_or_module ||= Object

  namespace_constant = get(namespace_name_or_module)
  namespace_constant.get(name, inherit: inherit)
  true
rescue Constant::Error # #get raises when the name is undefined, or its path runs through a literal
  false
end

.get(value, namespace = nil, inherit: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/constant/constant.rb', line 8

def self.get(value, namespace=nil, inherit: nil)
  namespace ||= Object

  if value.is_a?(::Module)
    Constant::Module.build(value)
  else
    if namespace.is_a?(::Module) || namespace.is_a?(Constant)
      namespace_constant = Constant::Module.build(namespace)
    else
      namespace_constant = get(namespace)
    end

    namespace_constant.get(value, inherit: inherit)
  end
end

.name(mod) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/constant/constant.rb', line 24

def self.name(mod)
  name = mod.name

  if name.nil?
    return nil
  end

  name.rpartition("::").last
end

.namespace(mod) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/constant/constant.rb', line 34

def self.namespace(mod)
  name = mod.name

  if name.nil?
    return nil
  end

  namespace_name = name.rpartition("::").first

  if namespace_name.empty?
    Constant::Module.new(Object)
  else
    namespace_mod = Object.const_get(namespace_name)
    Constant::Module.new(namespace_mod)
  end
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
# File 'lib/constant/constant.rb', line 61

def ==(other)
  other.is_a?(Constant) && identity == other.identity
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/constant/constant.rb', line 65

def eql?(other)
  self == other
end

#hashObject



69
70
71
# File 'lib/constant/constant.rb', line 69

def hash
  identity.hash
end