Class: Smooth::Model

Inherits:
Object
  • Object
show all
Includes:
Virtus
Defined in:
lib/smooth/model.rb

Constant Summary collapse

InvalidRecord =
Class.new(Exception)
InvalidCollection =
Class.new(Exception)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, options = {}) ⇒ Model

Returns a new instance of Model.

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/smooth/model.rb', line 12

def initialize(attributes={},options={})
  options ||= {}
  attributes ||= {}

  @model_options  = options.dup
  @collection     = options[:collection] if options[:collection]

  raise InvalidRecord unless attributes.is_a?(Hash)

  unless respond_to?(:id)
    extend(Virtus)
    self.class.attribute :id, String unless respond_to?(:id)
  end

  super(attributes)
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



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

def collection
  @collection
end

#model_attributesObject

Returns the value of attribute model_attributes.



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

def model_attributes
  @model_attributes
end

#model_optionsObject

Returns the value of attribute model_options.



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

def model_options
  @model_options
end

Instance Method Details

#as_json(options = {}) ⇒ Object



79
80
81
# File 'lib/smooth/model.rb', line 79

def as_json options={}
  to_hash rescue @attributes
end

#fetchObject

the collection should implement this single object find



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/smooth/model.rb', line 57

def fetch
  return self unless self.id

  model = collection.models.detect do |item|
    item[id_field].to_s == self.id.to_s
  end

  if model && model.attributes.is_a?(Hash)
    self.send(:set_attributes, model.attributes)
  end

  self
end

#idObject



75
76
77
# File 'lib/smooth/model.rb', line 75

def id
  attributes.fetch(:id, nil)
end

#is_new?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/smooth/model.rb', line 71

def is_new?
  id.nil?
end

#saveObject



47
48
49
# File 'lib/smooth/model.rb', line 47

def save
  is_new? ? sync(:create) : sync(:update)
end

#sync(method = :read, *args) ⇒ Object

This should delegate to the collection sync method which is capable of getting a single record

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/smooth/model.rb', line 31

def sync method=:read, *args
  raise InvalidCollection unless collection && collection.respond_to?(:sync)

  case

  when is_new?
    self.id = collection.sync(:create, self).id
  when !is_new? && method == :update
    collection.sync(:update, self)
  else
    collection.sync(:read)
  end

  fetch
end

#to_json(options = {}) ⇒ Object



83
84
85
# File 'lib/smooth/model.rb', line 83

def to_json options={}
  JSON.generate(as_json(options))
end

#update_attributes(attributes = {}) ⇒ Object



51
52
53
54
# File 'lib/smooth/model.rb', line 51

def update_attributes attributes={}
  self.send(:set_attributes, attributes)
  save
end