Class: Rwc::Core::BaseDecorator
- Inherits:
-
Object
- Object
- Rwc::Core::BaseDecorator
- Defined in:
- lib/rwc/core/base_decorator.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
- .decorate(relation, context: {}) ⇒ Object
- .decorate_collection(collection:, opts: {}) ⇒ Object
- .delegate_all ⇒ Object
- .include_in_json(*methods) ⇒ Object
Instance Method Summary collapse
-
#initialize(object, opts = {}) ⇒ BaseDecorator
constructor
A new instance of BaseDecorator.
Constructor Details
#initialize(object, opts = {}) ⇒ BaseDecorator
Returns a new instance of BaseDecorator.
11 12 13 14 |
# File 'lib/rwc/core/base_decorator.rb', line 11 def initialize(object, opts = {}) @object = object @context = OpenStruct.new(**opts) end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
8 9 10 |
# File 'lib/rwc/core/base_decorator.rb', line 8 def context @context end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
9 10 11 |
# File 'lib/rwc/core/base_decorator.rb', line 9 def object @object end |
Class Method Details
.decorate(relation, context: {}) ⇒ Object
42 43 44 |
# File 'lib/rwc/core/base_decorator.rb', line 42 def decorate(relation, context: {}) new(relation, context) end |
.decorate_collection(collection:, opts: {}) ⇒ Object
21 22 23 |
# File 'lib/rwc/core/base_decorator.rb', line 21 def decorate_collection(collection:, opts: {}) collection.map { |object| new(object, opts) } end |
.delegate_all ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/rwc/core/base_decorator.rb', line 25 def delegate_all define_method(:method_missing) do |method_name, *args| return object.send(method_name, *args) if object.respond_to?(method_name) super(method_name, *args) end end |
.include_in_json(*methods) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/rwc/core/base_decorator.rb', line 33 def include_in_json(*methods) define_method(:as_json) do |*args| included_hash = methods.index_with do |method| send(method) end object.send(:as_json, *args).merge(included_hash) end end |