Class: Cornerstore::Model::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON, ActiveModel::Validations
Defined in:
lib/cornerstore/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, parent = nil) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:



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

#_idObject Also known as: id

Returns the value of attribute _id.



7
8
9
# File 'lib/cornerstore/model.rb', line 7

def _id
  @_id
end

#_slugsObject

Returns the value of attribute _slugs.



8
9
10
# File 'lib/cornerstore/model.rb', line 8

def _slugs
  @_slugs
end

#created_atObject

Returns the value of attribute created_at.



10
11
12
# File 'lib/cornerstore/model.rb', line 10

def created_at
  @created_at
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/cornerstore/model.rb', line 9

def parent
  @parent
end

#updated_atObject

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

#attributesObject



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

#inspectObject



35
36
37
# File 'lib/cornerstore/model.rb', line 35

def inspect
  {class: self.class.name, id: id}.merge!(attributes).to_s
end

#to_paramObject



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