Class: EmailOctopus::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/email_octopus/model.rb

Overview

Common code for model objects.

Direct Known Subclasses

Campaign, Contact, List

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Model

Returns a new instance of Model.

Parameters:

  • params (Hash) (defaults to: {})

    Initial attributes for this 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

#attributesObject (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

.allQuery

Start a new query on a group of model resources.

Returns:

  • (Query)

    for this model class.



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.

Parameters:

  • name (Symbol)

    Name of the attribute



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.

Parameters:

  • params (Hash) (defaults to: {})

    Initial attributes to save.



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.

Parameters:

  • id (String)

    ID provided by EmailOctopus for this resource.



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.

Parameters:

  • method (Symbol)

    Name of the method to call.

  • arguments (Array)

    Arguments to pass to the method.

Returns:



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_jsonObject



77
78
79
# File 'lib/email_octopus/model.rb', line 77

def as_json
  attributes
end

#destroyObject



85
86
87
# File 'lib/email_octopus/model.rb', line 85

def destroy
  @api.delete(base_path).success?
end

#persisted?Boolean

Returns:

  • (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

#saveObject

Run validations and persist to the database.



73
74
75
# File 'lib/email_octopus/model.rb', line 73

def save
  valid? && persist!
end

#to_jsonObject



81
82
83
# File 'lib/email_octopus/model.rb', line 81

def to_json
  as_json.to_json
end