Module: Reflect

Defined in:
lib/reflect/reflect.rb,
lib/reflect/reflection.rb,
lib/reflect/controls/subject.rb,
lib/reflect/controls/ancestor.rb,
lib/reflect/controls/namespace.rb

Defined Under Namespace

Modules: Controls Classes: Reflection

Constant Summary collapse

Error =
Class.new(RuntimeError)

Class Method Summary collapse

Class Method Details

.call(subject, constant_name, strict: nil, ancestors: nil) ⇒ Object



4
5
6
# File 'lib/reflect/reflect.rb', line 4

def self.call(subject, constant_name, strict: nil, ancestors: nil)
  Reflection.build(subject, constant_name, strict: strict, ancestors: ancestors)
end

.constant(subject) ⇒ Object



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

def self.constant(subject)
  [Module, Class].include?(subject.class) ? subject : subject.class
end

.constant?(subject_constant, constant_name, ancestors: nil) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/reflect/reflect.rb', line 34

def self.constant?(subject_constant, constant_name, ancestors: nil)
  ancestors = Reflection::Default.ancestors if ancestors.nil?
  subject_constant.const_defined?(constant_name, ancestors)
end

.get_constant(subject_constant, constant_name, strict: nil, ancestors: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/reflect/reflect.rb', line 12

def self.get_constant(subject_constant, constant_name, strict: nil, ancestors: nil)
  strict = Reflection::Default.strict if strict.nil?
  ancestors = Reflection::Default.ancestors if ancestors.nil?

  constant = nil

  if constant?(subject_constant, constant_name, ancestors: ancestors)
    constant = get_constant!(subject_constant, constant_name, ancestors: ancestors)
  end

  if constant.nil? && strict
    raise Reflect::Error, "Namespace #{constant_name} is not defined in #{subject_constant.name}"
  end

  constant
end

.get_constant!(subject_constant, constant_name, ancestors: nil) ⇒ Object



29
30
31
32
# File 'lib/reflect/reflect.rb', line 29

def self.get_constant!(subject_constant, constant_name, ancestors: nil)
  ancestors = Reflection::Default.ancestors if ancestors.nil?
  subject_constant.const_get(constant_name, ancestors)
end