Class: Post

Inherits:
Object
  • Object
show all
Defined in:
lib/simpleblog/post.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, post_content) ⇒ Post

Returns a new instance of Post.



10
11
12
13
# File 'lib/simpleblog/post.rb', line 10

def initialize(id, post_content)
  @id = id
  @post_content = post_content
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



21
22
23
24
25
26
27
28
29
# File 'lib/simpleblog/post.rb', line 21

def method_missing(method, *args, &block)
  if post_content[method.to_s]
    self.class.define_method(method.to_s) do
      post_content[method.to_s]
    end

    send method, *args, &block
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/simpleblog/post.rb', line 8

def id
  @id
end

#post_contentObject (readonly)

Returns the value of attribute post_content.



8
9
10
# File 'lib/simpleblog/post.rb', line 8

def post_content
  @post_content
end

Class Method Details

.build_list_of_posts(posts) ⇒ Object



2
3
4
5
6
# File 'lib/simpleblog/post.rb', line 2

def self.build_list_of_posts(posts)
  posts.map do |id, post_content|
    Post.new(id, post_content)
  end
end

Instance Method Details

#tagsObject



15
16
17
# File 'lib/simpleblog/post.rb', line 15

def tags
  post_content["tags"].gsub(/\s+/, "").split(",")
end