Class: Cornerstore::Model::Base
- Inherits:
-
Object
- Object
- Cornerstore::Model::Base
- Includes:
- ActiveModel::Serializers::JSON, ActiveModel::Validations
- Defined in:
- lib/cornerstore/model.rb
Direct Known Subclasses
Address, Cancellation, Carrier, Cart, Collection, Customer, DifferentiatingProperty, Image, LineItem, Order, PaymentMeans, Price, Product, Property, Shipment, Variant
Instance Attribute Summary collapse
-
#_id ⇒ Object
(also: #id)
Returns the value of attribute _id.
-
#_slugs ⇒ Object
Returns the value of attribute _slugs.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #attributes ⇒ Object
- #attributes=(attributes) ⇒ Object
-
#initialize(attributes = {}, parent = nil) {|_self| ... } ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
- #to_param ⇒ Object
- #url(depth = 1) ⇒ Object
Constructor Details
#initialize(attributes = {}, parent = nil) {|_self| ... } ⇒ Base
Returns a new instance of Base.
15 16 17 18 19 |
# File 'lib/cornerstore/model.rb', line 15 def initialize(attributes = {}, parent = nil) self.attributes = attributes self.parent = parent yield self if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/cornerstore/model.rb', line 63 def method_missing(method, *args, &block) if Writable.method_defined?(method) raise "Sorry, this part of the Cornerstore-API is currently read-only" else super end end |
Instance Attribute Details
#_id ⇒ Object Also known as: id
Returns the value of attribute _id.
7 8 9 |
# File 'lib/cornerstore/model.rb', line 7 def _id @_id end |
#_slugs ⇒ Object
Returns the value of attribute _slugs.
8 9 10 |
# File 'lib/cornerstore/model.rb', line 8 def _slugs @_slugs end |
#created_at ⇒ Object
Returns the value of attribute created_at.
10 11 12 |
# File 'lib/cornerstore/model.rb', line 10 def created_at @created_at end |
#parent ⇒ Object
Returns the value of attribute parent.
9 10 11 |
# File 'lib/cornerstore/model.rb', line 9 def parent @parent end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
11 12 13 |
# File 'lib/cornerstore/model.rb', line 11 def updated_at @updated_at end |
Class Method Details
.method_missing(method, *args, &block) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/cornerstore/model.rb', line 55 def self.method_missing(method, *args, &block) if (self.const_defined?("Resource") and self.const_get("Resource").method_defined?(method)) self.const_get("Resource").new.send(method, *args, &block) else super end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
29 30 31 |
# File 'lib/cornerstore/model.rb', line 29 def ==(other) other.id == self.id end |
#attributes ⇒ Object
39 40 41 |
# File 'lib/cornerstore/model.rb', line 39 def attributes {} end |
#attributes=(attributes) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/cornerstore/model.rb', line 43 def attributes=(attributes) attributes ||= {} attributes.each_pair do |name, value| send("#{name}=", value) if respond_to?("#{name}=") end end |
#inspect ⇒ Object
35 36 37 |
# File 'lib/cornerstore/model.rb', line 35 def inspect {class: self.class.name, id: id}.merge!(attributes).to_s end |
#to_param ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/cornerstore/model.rb', line 21 def to_param if _slugs and !_slugs.empty? _slugs.first else _id end end |
#url(depth = 1) ⇒ Object
50 51 52 53 |
# File 'lib/cornerstore/model.rb', line 50 def url(depth = 1) root = (@parent && depth > 0) ? @parent.url(depth-1) : Cornerstore.root_url "#{root}/#{self.class.name.split('::').last.underscore.pluralize}/#{id}" end |