Class: Mailchimp::Instance
- Inherits:
-
Object
- Object
- Mailchimp::Instance
show all
- Defined in:
- lib/mailchimp_api_v3/instance.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(client, data, collection_path = '') ⇒ Instance
13
14
15
16
17
|
# File 'lib/mailchimp_api_v3/instance.rb', line 13
def initialize(client, data, collection_path = '')
@client = client
@data = data
@collection_path = collection_path
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, options = {}) ⇒ Object
60
61
62
63
64
|
# File 'lib/mailchimp_api_v3/instance.rb', line 60
def method_missing(symbol, options = {})
key = symbol.id2name
return @data[key] if @data.key? key
super
end
|
Class Method Details
.get(client, collection_path, id) ⇒ Object
6
7
8
9
|
# File 'lib/mailchimp_api_v3/instance.rb', line 6
def self.get(client, collection_path, id)
data = client.get "#{collection_path}/#{id}"
data ? new(client, data, collection_path) : nil
end
|
Instance Method Details
#delete ⇒ Object
24
25
26
|
# File 'lib/mailchimp_api_v3/instance.rb', line 24
def delete
@client.delete(path)
end
|
#matches?(match_data) ⇒ Boolean
32
33
34
35
36
37
|
# File 'lib/mailchimp_api_v3/instance.rb', line 32
def matches?(match_data)
match_data.each do |k, v|
break false unless same?(__send__(k), v)
true
end
end
|
#path ⇒ Object
28
29
30
|
# File 'lib/mailchimp_api_v3/instance.rb', line 28
def path
@path ||= "#{@collection_path}/#{@data['id']}"
end
|
#same?(thing, other_thing) ⇒ Boolean
39
40
41
42
43
|
# File 'lib/mailchimp_api_v3/instance.rb', line 39
def same?(thing, other_thing)
return thing.casecmp(other_thing.to_s).zero? if thing.is_a? String
return thing.subhash?(other_thing.to_h) if thing.is_a? Hash
thing == other_thing
end
|
#subclass_from(collection_class, *args) ⇒ Object
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/mailchimp_api_v3/instance.rb', line 45
def subclass_from(collection_class, *args)
id, name, options = parse_args(*args)
return subclass_instance_from(collection_class, id) if id
paging_options = options.divide_on('start', 'page_size') if options
collection = collection_class.new @client, path, paging_options
return collection.find_by collection.name_field => name if name
options.nil? || options.empty? ? collection : collection.where(options)
end
|
#update(new_data) ⇒ Object
19
20
21
22
|
# File 'lib/mailchimp_api_v3/instance.rb', line 19
def update(new_data)
@data = @client.patch(path, new_data)
self
end
|