Class: DisqusRails::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/disqus_rails/model.rb

Direct Known Subclasses

Category, Forum, Post, Thread, User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



33
34
35
36
37
# File 'lib/disqus_rails/model.rb', line 33

def initialize(attributes={})
  attributes.each do |attr_name, attr_value|
    instance_variable_set("@#{attr_name}".to_sym, attr_value)
  end
end

Class Method Details

.inherited(subclass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/disqus_rails/model.rb', line 4

def self.inherited(subclass)
  subclass_name = subclass.name.split(/::/).last
  api_name = Api.const_get subclass_name.pluralize
  collection_name = DisqusRails.const_get subclass_name.pluralize

  subclass.class_exec do

    define_singleton_method :find do |id|
      new(api_name.details(subclass_name.downcase.to_sym => id)[:response])
    end

    if api_name.respond_to?(:list)
      define_singleton_method :where do |attributes={}|
        collection_name.new subclass_name.pluralize.to_sym, :list, attributes
      end
    end

    if api_name.respond_to?(:create)
      define_singleton_method :create do |attributes={}|
        subclass.new(api_name.create(attributes)[:response])
      end
    end

    define_method :reload do
      subclass.new(api_name.details(subclass_name.downcase.to_sym => self.id)[:response])
    end
  end
end

Instance Method Details

#update_attributes(attributes = {}) ⇒ Object



39
40
41
42
43
# File 'lib/disqus_rails/model.rb', line 39

def update_attributes(attributes={})
  attributes.each do |attr_name, attr_value|
    send("#{attr_name}=", attr_value)
  end
end