class Reflect::Reflection

Attributes

constant[R]
strict[R]
subject[R]
target[R]

Public Class Methods

build(subject, constant_name, strict: nil, ancestors: nil) click to toggle source
# File lib/reflect/reflection.rb, line 18
def self.build(subject, constant_name, strict: nil, ancestors: nil)
  strict = Default.strict if strict.nil?
  ancestors = Default.ancestors if ancestors.nil?

  subject_constant = Reflect.constant(subject)

  target = Reflect.get_constant(subject_constant, constant_name, strict: strict, ancestors: ancestors)
  return nil if target.nil?

  new(subject, target, strict)
end
new(subject, target, strict) click to toggle source
# File lib/reflect/reflection.rb, line 12
def initialize(subject, target, strict)
  @subject = subject
  @target = target
  @strict = strict
end

Public Instance Methods

call(method_name, arg=nil) click to toggle source
# File lib/reflect/reflection.rb, line 30
def call(method_name, arg=nil)
  unless target.respond_to?(method_name)
    target_name = Reflect.constant(target).name
    raise Reflect::Error, "#{target_name} does not define method #{method_name}"
  end

  arg ||= subject

  target.send(method_name, arg)
end
get(accessor_name, strict: nil, coerce_constant: nil) click to toggle source
# File lib/reflect/reflection.rb, line 46
def get(accessor_name, strict: nil, coerce_constant: nil)
  strict = self.strict if strict.nil?
  coerce_constant = true if coerce_constant.nil?

  target = get_target(accessor_name, strict: strict)
  return nil if target.nil?

  if coerce_constant
    target = Reflect.constant(target)
  end

  self.class.new(subject, target, strict)
end
get_target(accessor_name, strict: nil) click to toggle source
# File lib/reflect/reflection.rb, line 60
def get_target(accessor_name, strict: nil)
  strict = self.strict if strict.nil?

  if !target_accessor?(accessor_name)
    if strict
      target_name = Reflect.constant(target).name
      raise Reflect::Error, "#{target_name} does not have accessor #{accessor_name}"
    else
      return nil
    end
  end

  target.send(accessor_name)
end
subject_constant() click to toggle source
# File lib/reflect/reflection.rb, line 8
def subject_constant
  @subject_constant ||= Reflect.constant(subject)
end
target_accessor?(name, subject=nil) click to toggle source
# File lib/reflect/reflection.rb, line 41
def target_accessor?(name, subject=nil)
  subject ||= constant
  subject.respond_to?(name)
end