Class: EmailOctopus::Model
- Inherits:
-
Object
- Object
- EmailOctopus::Model
- Extended by:
- ActiveModel::Naming
- Defined in:
- lib/email_octopus/model.rb
Overview
Common code for model objects.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
-
.all ⇒ Query
Start a new query on a group of model resources.
-
.attribute(name) ⇒ Object
Define a new attribute method on the model that reads from the
attributes
hash. -
.create(params = {}) ⇒ Object
Instantiate a new resource and immediately persist it on the API.
-
.find(id) ⇒ Object
Find a resource by its given ID.
-
.method_missing(method, *arguments) ⇒ Query
Delegate all class method calls that aren’t defined on the model class to its
Query
.
Instance Method Summary collapse
- #as_json ⇒ Object
- #destroy ⇒ Object
-
#initialize(params = {}) ⇒ Model
constructor
A new instance of Model.
- #persisted? ⇒ Boolean
- #reload! ⇒ Object
-
#save ⇒ Object
Run validations and persist to the database.
- #to_json ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Model
Returns a new instance of Model.
15 16 17 18 |
# File 'lib/email_octopus/model.rb', line 15 def initialize(params = {}) @attributes = params @api = API.new(EmailOctopus.config.api_key) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
12 13 14 |
# File 'lib/email_octopus/model.rb', line 12 def attributes @attributes end |
Class Method Details
.all ⇒ Query
Start a new query on a group of model resources.
37 38 39 |
# File 'lib/email_octopus/model.rb', line 37 def self.all Query.new self end |
.attribute(name) ⇒ Object
Define a new attribute method on the model that reads from the attributes
hash.
57 58 59 60 61 62 63 64 65 |
# File 'lib/email_octopus/model.rb', line 57 def self.attribute(name) define_method name do attributes[name.to_s] end define_method "#{name}=" do |value| attributes[name.to_s] = value end end |
.create(params = {}) ⇒ Object
Instantiate a new resource and immediately persist it on the API.
23 24 25 |
# File 'lib/email_octopus/model.rb', line 23 def self.create(params = {}) new(params).tap(&:save) end |
.find(id) ⇒ Object
Find a resource by its given ID.
30 31 32 |
# File 'lib/email_octopus/model.rb', line 30 def self.find(id) new(id: id).tap(&:persisted?) end |
.method_missing(method, *arguments) ⇒ Query
Delegate all class method calls that aren’t defined on the model class to its Query
.
48 49 50 51 |
# File 'lib/email_octopus/model.rb', line 48 def self.method_missing(method, *arguments) return super unless respond_to? method all.public_send method, *arguments end |
Instance Method Details
#as_json ⇒ Object
77 78 79 |
# File 'lib/email_octopus/model.rb', line 77 def as_json attributes end |
#destroy ⇒ Object
85 86 87 |
# File 'lib/email_octopus/model.rb', line 85 def destroy @api.delete(base_path).success? end |
#persisted? ⇒ Boolean
68 69 70 |
# File 'lib/email_octopus/model.rb', line 68 def persisted? id.present? && reload! end |
#reload! ⇒ Object
89 90 91 |
# File 'lib/email_octopus/model.rb', line 89 def reload! @attributes = @api.get(path).body end |
#save ⇒ Object
Run validations and persist to the database.
73 74 75 |
# File 'lib/email_octopus/model.rb', line 73 def save valid? && persist! end |
#to_json ⇒ Object
81 82 83 |
# File 'lib/email_octopus/model.rb', line 81 def to_json as_json.to_json end |