Class: Reflect::Reflection
- Inherits:
-
Object
- Object
- Reflect::Reflection
- Defined in:
- lib/reflect/reflection.rb
Defined Under Namespace
Modules: Default
Instance Attribute Summary collapse
-
#constant ⇒ Object
readonly
Returns the value of attribute constant.
-
#strict ⇒ Object
readonly
Returns the value of attribute strict.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Class Method Summary collapse
Instance Method Summary collapse
- #call(method_name, arg = nil) ⇒ Object
- #constant_accessor?(name) ⇒ Boolean
- #get(accessor_name, strict: nil) ⇒ Object
- #get_constant(accessor_name, strict: nil) ⇒ Object
-
#initialize(subject, constant, strict) ⇒ Reflection
constructor
A new instance of Reflection.
- #subject_constant ⇒ Object
Constructor Details
#initialize(subject, constant, strict) ⇒ Reflection
Returns a new instance of Reflection.
11 12 13 14 15 |
# File 'lib/reflect/reflection.rb', line 11 def initialize(subject, constant, strict) @subject = subject @constant = constant @strict = strict end |
Instance Attribute Details
#constant ⇒ Object (readonly)
Returns the value of attribute constant.
4 5 6 |
# File 'lib/reflect/reflection.rb', line 4 def constant @constant end |
#strict ⇒ Object (readonly)
Returns the value of attribute strict.
5 6 7 |
# File 'lib/reflect/reflection.rb', line 5 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 |
Class Method Details
.build(subject, constant_name, strict: nil, ancestors: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/reflect/reflection.rb', line 17 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.subject_constant(subject) constant = Reflect.get_constant(subject_constant, constant_name, strict: strict, ancestors: ancestors) return nil if constant.nil? instance = new(subject, constant, strict) end |
Instance Method Details
#call(method_name, arg = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/reflect/reflection.rb', line 29 def call(method_name, arg=nil) unless constant.respond_to?(method_name) raise Reflect::Error, "Constant #{constant.name} does not define method #{method_name}" end arg ||= subject constant.send(method_name, arg) end |
#constant_accessor?(name) ⇒ Boolean
39 40 41 |
# File 'lib/reflect/reflection.rb', line 39 def constant_accessor?(name) constant.respond_to?(name) end |
#get(accessor_name, strict: nil) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/reflect/reflection.rb', line 43 def get(accessor_name, strict: nil) strict = self.strict if strict.nil? result = get_constant(accessor_name, strict: strict) return nil if result.nil? constant = Reflect.subject_constant(result) self.class.new(subject, constant, strict) end |
#get_constant(accessor_name, strict: nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/reflect/reflection.rb', line 53 def get_constant(accessor_name, strict: nil) strict = self.strict if strict.nil? if !constant_accessor?(accessor_name) if strict raise Reflect::Error, "Constant #{constant.name} does not have accessor #{accessor_name}" else return nil end end constant.send(accessor_name) end |
#subject_constant ⇒ Object
7 8 9 |
# File 'lib/reflect/reflection.rb', line 7 def subject_constant @subject_constant ||= Reflect.subject_constant(subject) end |