Class: Liquid::Rails::Drop
- Inherits:
-
Drop
- Object
- Drop
- Liquid::Rails::Drop
- Defined in:
- lib/liquid-rails/drops/drop.rb
Class Attribute Summary collapse
-
._associations ⇒ Object
Returns the value of attribute _associations.
-
._attributes ⇒ Object
Returns the value of attribute _attributes.
Class Method Summary collapse
- .associate(type, attrs) ⇒ Object
- .attributes(*attrs) ⇒ Object
- .belongs_to(*attrs) ⇒ Object
-
.drop_class_for(resource) ⇒ Object
By default, ‘Product` maps to `ProductDrop`.
-
.dropify(resource, options = {}) ⇒ Object
Create a drop instance when it cannot be inferred.
- .has_many(*attrs) ⇒ Object
- .has_one(*attrs) ⇒ Object
- .inherited(base) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #as_json(options = {}) ⇒ Object
- #attributes ⇒ Object
-
#initialize(object, options = {}) ⇒ Drop
constructor
Wraps an object in a new instance of the drop.
- #inspect ⇒ Object
- #liquid_method_missing(method) ⇒ Object
- #to_json(options = {}) ⇒ Object
Constructor Details
#initialize(object, options = {}) ⇒ Drop
Wraps an object in a new instance of the drop.
87 88 89 |
# File 'lib/liquid-rails/drops/drop.rb', line 87 def initialize(object, ={}) @object = object end |
Class Attribute Details
._associations ⇒ Object
Returns the value of attribute _associations.
7 8 9 |
# File 'lib/liquid-rails/drops/drop.rb', line 7 def _associations @_associations end |
._attributes ⇒ Object
Returns the value of attribute _attributes.
6 7 8 |
# File 'lib/liquid-rails/drops/drop.rb', line 6 def _attributes @_attributes end |
Class Method Details
.associate(type, attrs) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/liquid-rails/drops/drop.rb', line 64 def self.associate(type, attrs) = attrs. self._associations = _associations.dup attrs.each do |attr| next if method_defined?(attr) define_method attr do value = instance_variable_get("@_#{attr}") return value if value association = object.send(attr) return nil if association.nil? drop_instance = Liquid::Rails::Drop.dropify(association, ) instance_variable_set("@_#{attr}", drop_instance) end self._associations[attr] = { type: type, options: } end end |
.attributes(*attrs) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/liquid-rails/drops/drop.rb', line 15 def self.attributes(*attrs) @_attributes.concat attrs attrs.each do |attr| next if method_defined?(attr) define_method(attr) do object.send(attr) end end end |
.belongs_to(*attrs) ⇒ Object
52 53 54 |
# File 'lib/liquid-rails/drops/drop.rb', line 52 def self.belongs_to(*attrs) associate(:belongs_to, attrs) end |
.drop_class_for(resource) ⇒ Object
By default, ‘Product` maps to `ProductDrop`.
Array of products maps to `Liquid::Rails::CollectionDrop`.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/liquid-rails/drops/drop.rb', line 29 def self.drop_class_for(resource) if resource.respond_to?(:to_ary) Liquid::Rails::CollectionDrop else if self == Liquid::Rails::Drop resource.drop_class else self end end end |
.dropify(resource, options = {}) ⇒ Object
Create a drop instance when it cannot be inferred.
42 43 44 45 46 47 48 49 50 |
# File 'lib/liquid-rails/drops/drop.rb', line 42 def self.dropify(resource, ={}) drop_class = if [:class_name] [:class_name].constantize else drop_class_for(resource) end drop_class.new(resource, .except(:class_name)) end |
.has_many(*attrs) ⇒ Object
56 57 58 |
# File 'lib/liquid-rails/drops/drop.rb', line 56 def self.has_many(*attrs) associate(:has_many, attrs) end |
.has_one(*attrs) ⇒ Object
60 61 62 |
# File 'lib/liquid-rails/drops/drop.rb', line 60 def self.has_one(*attrs) associate(:has_one, attrs) end |
.inherited(base) ⇒ Object
10 11 12 13 |
# File 'lib/liquid-rails/drops/drop.rb', line 10 def self.inherited(base) base._attributes = [] base._associations = {} end |
Instance Method Details
#==(other) ⇒ Object
117 118 119 |
# File 'lib/liquid-rails/drops/drop.rb', line 117 def ==(other) other.present? && other.object == @object end |
#as_json(options = {}) ⇒ Object
97 98 99 |
# File 'lib/liquid-rails/drops/drop.rb', line 97 def as_json(={}) attributes.as_json() end |
#attributes ⇒ Object
91 92 93 94 95 |
# File 'lib/liquid-rails/drops/drop.rb', line 91 def attributes @attributes ||= self.class._attributes.dup.each_with_object({}) do |name, hash| hash[name.to_s] = send(name) end end |
#inspect ⇒ Object
113 114 115 |
# File 'lib/liquid-rails/drops/drop.rb', line 113 def inspect "#<#{self.class.name} @object: #{object.inspect} @attributes: #{attributes.inspect}>" end |
#liquid_method_missing(method) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/liquid-rails/drops/drop.rb', line 105 def liquid_method_missing(method) return nil unless @context && @context.strict_variables raise ::Liquid::UndefinedDropMethod, <<~HEREDOC undefined method #{method} for #{self.class} Did you forget to add it to `attributes`? HEREDOC end |
#to_json(options = {}) ⇒ Object
101 102 103 |
# File 'lib/liquid-rails/drops/drop.rb', line 101 def to_json(={}) as_json.to_json() end |