Class: Stratus::Resources::Base
- Inherits:
-
Object
- Object
- Stratus::Resources::Base
show all
- Defined in:
- lib/stratus/resources/base.rb
Overview
content_path:
- pages/about
- posts/20081105-my-slugline
content_type:
- content
- attachment
collection_type:
- pages
- posts
slug:
- about
- 20081105-my-slugline
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(fullpath, content_path = '', parse = :index) ⇒ Base
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/stratus/resources/base.rb', line 26
def initialize(fullpath, content_path='', parse=:index)
@source_path = fullpath
@slug = (parse == :file) ? File.basename(fullpath) : File.basename(content_path)
@content_type = :content
@content_path = content_path
@collection_type = content_path.split('/').first
@parse_mode = parse
@is_template = false
@index = if @slug =~ /^([\d]{3})_(.*)$/
@slug = $2
$1.to_i
else
nil
end
case parse
when :index
parse_file(File.join(source_path, 'index.html'))
when :file
parse_file(source_path)
when :template
@is_template = true
parse_file(source_path)
else
puts "Not parsing #{fullpath}"
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/stratus/resources/base.rb', line 78
def method_missing(name, *args)
if name.to_s.ends_with? '='
metadata[name.to_s[0..-2].to_sym] = args.first
else
metadata[name]
end
end
|
Instance Attribute Details
#collection_type ⇒ Object
Returns the value of attribute collection_type.
23
24
25
|
# File 'lib/stratus/resources/base.rb', line 23
def collection_type
@collection_type
end
|
#content_path ⇒ Object
Returns the value of attribute content_path.
23
24
25
|
# File 'lib/stratus/resources/base.rb', line 23
def content_path
@content_path
end
|
#content_type ⇒ Object
Returns the value of attribute content_type.
23
24
25
|
# File 'lib/stratus/resources/base.rb', line 23
def content_type
@content_type
end
|
#index ⇒ Object
Returns the value of attribute index.
24
25
26
|
# File 'lib/stratus/resources/base.rb', line 24
def index
@index
end
|
#slug ⇒ Object
Returns the value of attribute slug.
23
24
25
|
# File 'lib/stratus/resources/base.rb', line 23
def slug
@slug
end
|
#source_path ⇒ Object
Returns the value of attribute source_path.
23
24
25
|
# File 'lib/stratus/resources/base.rb', line 23
def source_path
@source_path
end
|
Instance Method Details
#<=>(other) ⇒ Object
86
87
88
89
|
# File 'lib/stratus/resources/base.rb', line 86
def <=>( other )
return unless other.kind_of? Stratus::Resources::Base
self.content_path <=> other.content_path
end
|
#[](key) ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/stratus/resources/base.rb', line 91
def [](key)
if self.respond_to? key.to_sym
self.send(key.to_sym)
else
metadata[key]
end
end
|
#[]=(key, value) ⇒ Object
98
99
100
101
102
103
104
|
# File 'lib/stratus/resources/base.rb', line 98
def []=(key, value)
if self.respond_to? key.to_sym
self.send(key.to_sym)
else
metadata[key] = value
end
end
|
#attachments ⇒ Object
65
66
67
|
# File 'lib/stratus/resources/base.rb', line 65
def attachments
Stratus::Resources.attachments( :content_path=>content_path )
end
|
#attachments_hash ⇒ Object
69
70
71
72
73
74
75
76
|
# File 'lib/stratus/resources/base.rb', line 69
def attachments_hash
returning( {} ) do |hash|
attachments.each do |att|
slug = att.slug.gsub(att.metadata[:ext], '')
hash[slug] = att
end
end
end
|
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/stratus/resources/base.rb', line 146
def fixup_meta
if metadata.has_key? :tags
tags = [metadata[:tags]].flatten.join(',')
tags = tags.split(',')
tags.map {|t| t.strip! }
metadata[:tags] = tags
end
if metadata.has_key? :category
cats = [metadata[:category]].flatten.join(',')
cats = cats.split(',')
cats.map {|t| t.strip! }
metadata[:categories] = cats
end
if metadata.has_key? :publish_date
metadata[:publish_date] = Chronic.parse( metadata[:publish_date] )
else
metadata[:publish_date] = Time.now
end
end
|
#full_path ⇒ Object
57
58
59
|
# File 'lib/stratus/resources/base.rb', line 57
def full_path
is_homepage ? "" : "#{ collection_type }/#{ @slug }"
end
|
#is_homepage ⇒ Object
106
107
108
|
# File 'lib/stratus/resources/base.rb', line 106
def is_homepage
Stratus.setting('homepage') == content_path
end
|
53
54
55
|
# File 'lib/stratus/resources/base.rb', line 53
def metadata
@meta ||= {}
end
|
#next_content ⇒ Object
117
118
119
120
121
122
|
# File 'lib/stratus/resources/base.rb', line 117
def next_content
collection = Stratus::Generator::LiquidContext.site_data[ collection_type ]
idx = collection.index(self)
return false if idx.nil? or idx >= (collection.length - 1)
collection[(idx + 1)]
end
|
#output_path ⇒ Object
61
62
63
|
# File 'lib/stratus/resources/base.rb', line 61
def output_path
File.join(Stratus.output_dir, @collection_type, @slug, 'index.html')
end
|
#prev_content ⇒ Object
110
111
112
113
114
115
|
# File 'lib/stratus/resources/base.rb', line 110
def prev_content
collection = Stratus::Generator::LiquidContext.site_data[ collection_type ]
idx = collection.index(self)
return false if idx.nil? or idx <= 0
collection[(idx - 1)]
end
|
#to_liquid ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/stratus/resources/base.rb', line 124
def to_liquid
@liquid_hash ||= rendered_attributes({
'slug' => @slug,
'index' => @index,
'content_path' => @content_path,
'collection_type' => @collection_type,
'content_type' => @content_type,
'full_path' => full_path,
'source_path' => source_path,
'attachments' => attachments,
'attachment' => attachments_hash,
'next' => next_content,
'prev' => prev_content,
'is_homepage' => is_homepage
})
end
|
#validate! ⇒ Object
Subclasses need to override this to ensure all the proper metadata exists
142
143
144
|
# File 'lib/stratus/resources/base.rb', line 142
def validate!
true
end
|