Class: Tributary::Item

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/tributary/item.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Item

Returns a new instance of Item.



5
6
7
8
9
# File 'lib/tributary/item.rb', line 5

def initialize file
  @file = file
  yaml, @body = File.read(@file).split "\n\n", 2
  super YAML.load yaml
end

Instance Method Details

#<=>(other) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tributary/item.rb', line 11

def <=> other
  case
  when other.date && date then other.date <=> date
  when date               then -1
  when other.date         then 1
  else                         0
  end.nonzero? or
  (other.path <=> path).nonzero? or
  case
  when lang       == App.locale then -1
  when other.lang == App.locale then 1
  when lang.nil?                then -1
  when other.lang.nil?          then 1
  else                               lang <=> other.lang
  end
end

#bodyObject



28
29
30
# File 'lib/tributary/item.rb', line 28

def body
  Kramdown::Document.new(@body).to_html
end

#dateObject



32
33
34
35
36
37
38
# File 'lib/tributary/item.rb', line 32

def date
  @date ||= case @table[:date]
            when Date   then @table[:date].to_time
            when String then Time.parse @table[:date]
            when Time   then @table[:date]
            end
end

#hashObject



42
43
44
# File 'lib/tributary/item.rb', line 42

def hash
  @file.hash
end

#langObject



46
47
48
# File 'lib/tributary/item.rb', line 46

def lang
  File.basename(@file, '.md').split('.')[1]
end

#pathObject



50
51
52
# File 'lib/tributary/item.rb', line 50

def path
  @path ||= File.basename(@file, '.md').split('.').first
end

#published?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/tributary/item.rb', line 54

def published?
  date and date < Time.now
end

#titleObject



58
59
60
# File 'lib/tributary/item.rb', line 58

def title
  @table[:title] or @body.scan(/\p{L}+/).first + ''
end

#typeObject



62
63
64
# File 'lib/tributary/item.rb', line 62

def type
  File.dirname(@file).split('/').last.to_sym
end