Module: Lycra::Attributes::ClassMethods
- Defined in:
- lib/lycra/attributes.rb
Instance Method Summary
collapse
-
#attribute(name = nil, type = nil, *args, **opts, &block) ⇒ Object
-
#attribute!(name = nil, type = nil, *args, **opts, &block) ⇒ Object
-
#attributes ⇒ Object
-
#cache(cache = nil) ⇒ Object
-
#inherited(child) ⇒ Object
-
#inspect ⇒ Object
-
#method_missing(meth, *args, &block) ⇒ Object
-
#resolve!(subj, *args, **context) ⇒ Object
-
#respond_to_missing?(meth, priv = false) ⇒ Boolean
-
#subject_type(klass = nil) ⇒ Object
-
#subject_type=(klass) ⇒ Object
-
#types ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/lycra/attributes.rb', line 70
def method_missing(meth, *args, &block)
if subject_type && subject_type.respond_to?(meth)
result = subject_type.send(meth, *args, &block)
return result.try(:document) || new(result) if result.is_a?(subject_type)
return result if result.is_a?(ActiveRecord::Relation)
return result.map { |r| r.try(:document) || new(r) } if result.is_a?(Enumerable) && result.first.is_a?(subject_type)
return result
else
super
end
end
|
Instance Method Details
#attribute(name = nil, type = nil, *args, **opts, &block) ⇒ Object
32
33
34
35
36
|
# File 'lib/lycra/attributes.rb', line 32
def attribute(name=nil, type=nil, *args, **opts, &block)
opts = {cache: cache}.merge(opts)
attr = Attribute.new(name, type, *args, **opts.merge({klass: self}), &block)
attributes[attr.name] = attr
end
|
#attribute!(name = nil, type = nil, *args, **opts, &block) ⇒ Object
38
39
40
|
# File 'lib/lycra/attributes.rb', line 38
def attribute!(name=nil, type=nil, *args, **opts, &block)
attribute(name, type, *args, **opts.merge({required: true}), &block)
end
|
#attributes ⇒ Object
42
43
44
|
# File 'lib/lycra/attributes.rb', line 42
def attributes
@_lycra_attributes ||= Collection.new(self)
end
|
#cache(cache = nil) ⇒ Object
55
56
57
58
|
# File 'lib/lycra/attributes.rb', line 55
def cache(cache=nil)
@_lycra_cache = cache unless cache.nil?
@_lycra_cache
end
|
#inherited(child) ⇒ Object
19
20
21
22
23
24
25
26
|
# File 'lib/lycra/attributes.rb', line 19
def inherited(child)
super if defined?(super)
child.send :instance_variable_set,
:@_lycra_attributes,
self.attributes.dup(child)
end
|
#inspect ⇒ Object
88
89
90
|
# File 'lib/lycra/attributes.rb', line 88
def inspect
"#{name}(subject: #{subject_type}, #{attributes.map { |key,attr| "#{attr.name}: #{attr.nested? ? "[#{attr.type.type}]" : attr.type.type}"}.join(', ')})"
end
|
#resolve!(subj, *args, **context) ⇒ Object
60
61
62
63
64
65
66
67
68
|
# File 'lib/lycra/attributes.rb', line 60
def resolve!(subj, *args, **context)
if subj.is_a?(subject_type)
return new(subj).resolve!(*args, **context)
elsif subj.is_a?(Enumerable) && subj.first.is_a?(subject_type)
return subj.map { |s| resolve!(s, *args, **context) }
end
raise "Invalid subject: #{subj}"
end
|
#respond_to_missing?(meth, priv = false) ⇒ Boolean
84
85
86
|
# File 'lib/lycra/attributes.rb', line 84
def respond_to_missing?(meth, priv=false)
(subject_type && subject_type.respond_to?(meth, priv)) || super
end
|
#subject_type(klass = nil) ⇒ Object
46
47
48
49
|
# File 'lib/lycra/attributes.rb', line 46
def subject_type(klass=nil)
@_lycra_subject_type = klass if klass
@_lycra_subject_type ||= (name.gsub(/(Decorator|Document|Serializer)\Z/, '').constantize rescue nil)
end
|
#subject_type=(klass) ⇒ Object
51
52
53
|
# File 'lib/lycra/attributes.rb', line 51
def subject_type=(klass)
subject_type klass
end
|
#types ⇒ Object
28
29
30
|
# File 'lib/lycra/attributes.rb', line 28
def types
Lycra::Types
end
|