Module: Lycra::Attributes::InstanceMethods
- Defined in:
- lib/lycra/attributes.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
126
127
128
129
130
131
|
# File 'lib/lycra/attributes.rb', line 126
def method_missing(meth, *args, &block)
return subject if meth == subject_type.to_s.underscore.to_sym
return attributes[meth].resolve!(self, *args, &block) if attributes.key?(meth)
return subject.send(meth, *args, &block) if subject && subject.respond_to?(meth)
super
end
|
Instance Method Details
#attributes ⇒ Object
100
101
102
|
# File 'lib/lycra/attributes.rb', line 100
def attributes
@attributes ||= self.class.attributes.dup
end
|
#initialize(subject) ⇒ Object
96
97
98
|
# File 'lib/lycra/attributes.rb', line 96
def initialize(subject)
@subject = subject
end
|
#inspect ⇒ Object
137
138
139
140
141
142
143
|
# File 'lib/lycra/attributes.rb', line 137
def inspect
if resolved?
"#<#{self.class.name} subject: #{subject.class}, #{attributes.map { |key,attr| "#{key}: #{resolved[key].try(:to_json) || (attr.nested? ? "[#{attr.type.type}]" : attr.type.type)}"}.join(', ')}>"
else
"#<#{self.class.name} subject: #{subject.class}, #{attributes.map { |key,attr| "#{attr.name}: #{attr.nested? ? "[#{attr.type.type}]" : attr.type.type}"}.join(', ')}>"
end
end
|
#reload ⇒ Object
119
120
121
122
123
124
|
# File 'lib/lycra/attributes.rb', line 119
def reload
@resolved = nil
attributes.values.each(&:reload)
subject.send(:reload) if subject.respond_to?(:reload)
self
end
|
#resolve!(*args, **options) ⇒ Object
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/lycra/attributes.rb', line 104
def resolve!(*args, **options)
raise Lycra::MissingSubjectError.new(self) if subject.nil?
context = options.slice!(:only, :except)
@resolved = attributes.map do |key,attr|
next if (options.key?(:only) && ![options[:only]].flatten.include?(key)) ||
(options.key?(:except) && [options[:except]].flatten.include?(key))
[ key, attr.dup.resolve!(self, args, context) ]
end.compact.to_h
end
|
#resolved? ⇒ Boolean
115
116
117
|
# File 'lib/lycra/attributes.rb', line 115
def resolved?
!!@resolved
end
|
#respond_to_missing?(meth, priv = false) ⇒ Boolean
133
134
135
|
# File 'lib/lycra/attributes.rb', line 133
def respond_to_missing?(meth, priv=false)
meth == subject_type.to_s.underscore.to_sym || attributes.key?(meth) || (subject && subject.respond_to?(meth, priv)) || super
end
|