Class: Reflect::Reflection
- Inherits:
-
Object
- Object
- Reflect::Reflection
- Defined in:
- lib/reflect/reflection.rb
Defined Under Namespace
Modules: Default
Instance Attribute Summary collapse
-
#strict ⇒ Object
readonly
Returns the value of attribute strict.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
-
#target ⇒ Object
(also: #constant)
readonly
Returns the value of attribute target.
Class Method Summary collapse
Instance Method Summary collapse
- #call(method_name, arg = nil) ⇒ Object
- #get(accessor_name, strict: nil, coerce_constant: nil) ⇒ Object
- #get_target(accessor_name, strict: nil) ⇒ Object
-
#initialize(subject, target, strict) ⇒ Reflection
constructor
A new instance of Reflection.
- #subject_constant ⇒ Object
- #target_accessor?(name, subject = nil) ⇒ Boolean
Constructor Details
#initialize(subject, target, strict) ⇒ Reflection
Returns a new instance of Reflection.
12 13 14 15 16 |
# File 'lib/reflect/reflection.rb', line 12 def initialize(subject, target, strict) @subject = subject @target = target @strict = strict end |
Instance Attribute Details
#strict ⇒ Object (readonly)
Returns the value of attribute strict.
6 7 8 |
# File 'lib/reflect/reflection.rb', line 6 def strict @strict end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
3 4 5 |
# File 'lib/reflect/reflection.rb', line 3 def subject @subject end |
#target ⇒ Object (readonly) Also known as: constant
Returns the value of attribute target.
4 5 6 |
# File 'lib/reflect/reflection.rb', line 4 def target @target end |
Class Method Details
.build(subject, constant_name, strict: nil, ancestors: nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# 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 |
Instance Method Details
#call(method_name, arg = nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# 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) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# 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 ⇒ Object
8 9 10 |
# File 'lib/reflect/reflection.rb', line 8 def subject_constant @subject_constant ||= Reflect.constant(subject) end |
#target_accessor?(name, subject = nil) ⇒ Boolean
41 42 43 44 |
# File 'lib/reflect/reflection.rb', line 41 def target_accessor?(name, subject=nil) subject ||= constant subject.respond_to?(name) end |