Class: Stratus::Resources::Base

Inherits:
Object
  • Object
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

Direct Known Subclasses

Attachment, Content, Template

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? '='
    [name.to_s[0..-2].to_sym] = args.first
  else
    [name]
  end
end

Instance Attribute Details

#collection_typeObject (readonly)

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_pathObject (readonly)

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_typeObject (readonly)

Returns the value of attribute content_type.



23
24
25
# File 'lib/stratus/resources/base.rb', line 23

def content_type
  @content_type
end

#indexObject

Returns the value of attribute index.



24
25
26
# File 'lib/stratus/resources/base.rb', line 24

def index
  @index
end

#slugObject (readonly)

Returns the value of attribute slug.



23
24
25
# File 'lib/stratus/resources/base.rb', line 23

def slug
  @slug
end

#source_pathObject (readonly)

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
    [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
    [key] = value
  end
end

#attachmentsObject



65
66
67
# File 'lib/stratus/resources/base.rb', line 65

def attachments
  Stratus::Resources.attachments( :content_path=>content_path )
end

#attachments_hashObject



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.[:ext], '')
      hash[slug] = att
    end
  end
end

#fixup_metaObject



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 .has_key? :tags
    tags = [[:tags]].flatten.join(',')
    tags = tags.split(',')
    tags.map {|t| t.strip! }
    [:tags] = tags
  end
  if .has_key? :category
    cats = [[:category]].flatten.join(',')
    cats = cats.split(',')
    cats.map {|t| t.strip! }
    [:categories] = cats
  end
  if .has_key? :publish_date
    [:publish_date] = Chronic.parse( [:publish_date] )
  else
    [:publish_date] = Time.now
  end
end

#full_pathObject



57
58
59
# File 'lib/stratus/resources/base.rb', line 57

def full_path
  is_homepage ? "" : "#{ collection_type }/#{ @slug }"
end

#is_homepageObject



106
107
108
# File 'lib/stratus/resources/base.rb', line 106

def is_homepage
  Stratus.setting('homepage') == content_path
end

#metadataObject



53
54
55
# File 'lib/stratus/resources/base.rb', line 53

def 
  @meta ||= {} # Hash.new {|h,k| h[k] = []}
end

#next_contentObject



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_pathObject



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_contentObject



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_liquidObject



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