Module: Shape::Base::ClassMethods
- Included in:
- Shape::Base, PropertyShaper, ViewDecorator
- Defined in:
- lib/shape/base.rb
Instance Method Summary collapse
- #_properties_from ⇒ Object
- #associations ⇒ Object
-
#delegate(*methods, options = {}) ⇒ void
Overrides Module.delegate to make ‘:_source` the default delegation target.
- #properties ⇒ Object
- #properties_from(name, options = {}) ⇒ Object
-
#property(property_name, options = {}, &block) ⇒ Object
(also: #association)
Expose a property as “…” To expose a property using a different attribute on the resource:.
- #shape(source, options = {}) ⇒ Object
- #shape_collection(collection, options = {}) ⇒ Object
- #shaper_context ⇒ Object
Instance Method Details
#_properties_from ⇒ Object
94 95 96 |
# File 'lib/shape/base.rb', line 94 def _properties_from @properties_from ||= [] end |
#associations ⇒ Object
86 87 88 |
# File 'lib/shape/base.rb', line 86 def associations @associations ||= {} end |
#delegate(*methods, options = {}) ⇒ void
103 104 105 106 |
# File 'lib/shape/base.rb', line 103 def delegate(*methods) = methods. super *methods, .reverse_merge(to: :_source) end |
#properties ⇒ Object
90 91 92 |
# File 'lib/shape/base.rb', line 90 def properties @properties ||= {} end |
#properties_from(name, options = {}) ⇒ Object
81 82 83 84 |
# File 'lib/shape/base.rb', line 81 def properties_from(name, ={}) except = Array([:except]) _properties_from << [name, except] end |
#property(property_name, options = {}, &block) ⇒ Object Also known as: association
Expose a property as “…” To expose a property using a different attribute on the resource:
property :display, from: :display_name
To expose a property with inline definition:
property :display do
from do
#{last_name}, #{first_name}
end
end
To expose a decorated collection:
property :practices, with: PracticeDecorator
To expose a decorated collection with view context
property :practices, with: PracticeDecorator, context: {view: :summary}
74 75 76 77 78 |
# File 'lib/shape/base.rb', line 74 def property(property_name, ={}, &block) properties[property_name] = Shape::PropertyShaper.new( shaper_context, property_name, , &block ) end |
#shape(source, options = {}) ⇒ Object
44 45 46 |
# File 'lib/shape/base.rb', line 44 def shape(source, ={}) self.new(source, ) end |
#shape_collection(collection, options = {}) ⇒ Object
108 109 110 111 112 |
# File 'lib/shape/base.rb', line 108 def shape_collection(collection, = {}) Array(collection).map do |item| self.shape(item, .clone) end end |
#shaper_context ⇒ Object
48 49 50 |
# File 'lib/shape/base.rb', line 48 def shaper_context @shaper_context || self end |