Class: Constant::Module

Inherits:
Object
  • Object
show all
Includes:
Constant, Initializer
Defined in:
lib/constant/module.rb

Constant Summary

Constants included from Constant

Error

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Constant

#==, __name, defined?, #eql?, get, #hash, name, namespace

Class Method Details

.build(mod) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/constant/module.rb', line 20

def self.build(mod)
  if mod.is_a?(Constant)
    mod
  else
    new(mod)
  end
end

Instance Method Details

#constant_names(include_literal_constants: nil, inherit: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/constant/module.rb', line 69

def constant_names(include_literal_constants: nil, inherit: nil)
  include_literal_constants ||= false
  inherit ||= false

  constant_symbols = value.constants(inherit)

  constant_symbols.filter_map do |constant_symbol|
    resolved = value.const_get(constant_symbol, inherit)

    if resolved.is_a?(::Module) || include_literal_constants
      constant_symbol.to_s
    end
  end
end

#constants(include_literal_constants: nil, inherit: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/constant/module.rb', line 52

def constants(include_literal_constants: nil, inherit: nil)
  include_literal_constants ||= false
  inherit ||= false

  constant_symbols = value.constants(inherit)

  constant_symbols.filter_map do |constant_name|
    resolved = value.const_get(constant_name, inherit)

    if resolved.is_a?(::Module)
      Constant::Module.build(resolved)
    elsif include_literal_constants
      Constant::Literal.build(constant_name, resolved, self)
    end
  end
end

#defined?(name_or_module, inherit: nil) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/constant/module.rb', line 84

def defined?(name_or_module, inherit: nil)
  if name_or_module.is_a?(::Module)
    inherit ||= false

    value.constants(inherit).any? do |constant_name|
      resolved = value.const_get(constant_name, inherit)
      resolved.equal?(name_or_module)
    end
  else
    Constant.defined?(name_or_module, value, inherit: inherit)
  end
end

#full_nameObject



12
13
14
# File 'lib/constant/module.rb', line 12

def full_name
  value.name
end

#get(name, inherit: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/constant/module.rb', line 28

def get(name, inherit: nil)
  inherit ||= false

  name = name.to_s
  head, _, rest = name.partition("::")

  if not rest.empty?
    head_constant = get(head, inherit: inherit)
    return head_constant.get(rest, inherit: inherit)
  end

  if not value.const_defined?(name, inherit)
    raise Constant::Error, "#{name} is not defined in #{value}"
  end

  resolved = value.const_get(name, inherit)

  if resolved.is_a?(::Module)
    Constant::Module.build(resolved)
  else
    Constant::Literal.build(name, resolved, self)
  end
end

#identityObject



97
98
99
# File 'lib/constant/module.rb', line 97

def identity
  value
end

#nameObject



8
9
10
# File 'lib/constant/module.rb', line 8

def name
  Constant.name(value)
end

#namespaceObject



16
17
18
# File 'lib/constant/module.rb', line 16

def namespace
  Constant.namespace(value)
end