Class: Tumblargh::Resource::Blog

Inherits:
Base
  • Object
show all
Defined in:
lib/tumblargh/resource/blog.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes, #context

Instance Method Summary collapse

Methods inherited from Base

#method_missing

Constructor Details

#initialize(domain, attrs = nil) ⇒ Blog

Returns a new instance of Blog.



8
9
10
11
# File 'lib/tumblargh/resource/blog.rb', line 8

def initialize(domain, attrs=nil)
  @domain = domain
  self.attributes = attrs.nil? ? fetch : attrs
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Tumblargh::Resource::Base

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



6
7
8
# File 'lib/tumblargh/resource/blog.rb', line 6

def domain
  @domain
end

Instance Method Details

#attributes=(attrs) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tumblargh/resource/blog.rb', line 13

def attributes=(attrs)
  attrs = attrs.with_indifferent_access

  # We passed in result from /posts, or a local file
  if attrs.include?(:posts) && attrs.include?(:blog)
    self.posts = attrs[:posts]
    attrs.delete(:posts)

    self.attributes = attrs[:blog]
  else
    super(attrs)
  end

  @attributes
end

#fetchObject



29
30
31
# File 'lib/tumblargh/resource/blog.rb', line 29

def fetch
  API.blog(domain)
end

#fetch!Object



33
34
35
# File 'lib/tumblargh/resource/blog.rb', line 33

def fetch!
  self.attributes = fetch
end

#postsObject



37
38
39
40
# File 'lib/tumblargh/resource/blog.rb', line 37

def posts
  @posts || self.posts = API.posts(domain)
  @posts # Whyyy??? Must I do this?
end

#posts=(ary) ⇒ Object



42
43
44
# File 'lib/tumblargh/resource/blog.rb', line 42

def posts=(ary)
  @posts = ary.map { |p| Post.new(p, self) }
end