Module: IsResource

Included in:
Spooky::Author, Spooky::Post, Spooky::Tag
Defined in:
lib/spooky/is_resource.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spooky/is_resource.rb', line 2

def self.included(base)
  base.class_eval do
    attr_reader(*const_get("ATTRIBUTES"))

    def initialize(attrs = {})
      self.class.const_get("ATTRIBUTES").each do |attribute|
        instance_variable_set("@#{attribute}", attrs[attribute])
      end

      parse_datetimes(attrs)
      parse_attributes(attrs)
    end

    def parse_datetimes(attrs)
      ["created_at", "updated_at", "published_at"].each do |date_attr|
        instance_variable_set("@#{date_attr}", DateTime.iso8601(attrs[date_attr])) if attrs[date_attr].present?
      end
    end

    def parse_attributes(attrs)
      # Abstract method, should be overridden in child if needed.
    end
  end
end