Class: Smooth::Model
Constant Summary collapse
- InvalidRecord =
Class.new(Exception)
- InvalidCollection =
Class.new(Exception)
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#model_attributes ⇒ Object
Returns the value of attribute model_attributes.
-
#model_options ⇒ Object
Returns the value of attribute model_options.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#fetch ⇒ Object
the collection should implement this single object find.
- #id ⇒ Object
-
#initialize(attributes = {}, options = {}) ⇒ Model
constructor
A new instance of Model.
- #is_new? ⇒ Boolean
- #save ⇒ Object
-
#sync(method = :read, *args) ⇒ Object
This should delegate to the collection sync method which is capable of getting a single record.
- #to_json(options = {}) ⇒ Object
- #update_attributes(attributes = {}) ⇒ Object
Constructor Details
#initialize(attributes = {}, options = {}) ⇒ Model
Returns a new instance of Model.
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={},={}) ||= {} attributes ||= {} @model_options = .dup @collection = [:collection] if [: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
#collection ⇒ Object
Returns the value of attribute collection.
7 8 9 |
# File 'lib/smooth/model.rb', line 7 def collection @collection end |
#model_attributes ⇒ Object
Returns the value of attribute model_attributes.
7 8 9 |
# File 'lib/smooth/model.rb', line 7 def model_attributes @model_attributes end |
#model_options ⇒ Object
Returns the value of attribute model_options.
7 8 9 |
# File 'lib/smooth/model.rb', line 7 def @model_options end |
Instance Method Details
#as_json(options = {}) ⇒ Object
79 80 81 |
# File 'lib/smooth/model.rb', line 79 def as_json ={} to_hash rescue @attributes end |
#fetch ⇒ Object
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 |
#id ⇒ Object
75 76 77 |
# File 'lib/smooth/model.rb', line 75 def id attributes.fetch(:id, nil) end |
#is_new? ⇒ Boolean
71 72 73 |
# File 'lib/smooth/model.rb', line 71 def is_new? id.nil? end |
#save ⇒ Object
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
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 ={} JSON.generate(as_json()) 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 |