Class: Mill::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/mill/resource.rb,
lib/mill/resources/dir.rb,
lib/mill/resources/blob.rb,
lib/mill/resources/feed.rb,
lib/mill/resources/text.rb,
lib/mill/resources/image.rb,
lib/mill/resources/robots.rb,
lib/mill/resources/sitemap.rb,
lib/mill/resources/redirect.rb,
lib/mill/resources/stylesheet.rb,
lib/mill/resources/google_site_verification.rb

Defined Under Namespace

Classes: Blob, Feed, GoogleSiteVerification, Image, Index, Redirect, Robots, Sitemap, Stylesheet, Text

Constant Summary collapse

FileTypes =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_file: nil, output_file: nil, date: nil, public: false, content: nil, site: nil) ⇒ Resource

Returns a new instance of Resource.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mill/resource.rb', line 14

def initialize(input_file: nil,
               output_file: nil,
               date: nil,
               public: false,
               content: nil,
               site: nil)
  if input_file
    @input_file = Path.new(input_file)
    @date = input_file.mtime.to_datetime
  else
    @date = DateTime.now
  end
  @output_file = Path.new(output_file) if output_file
  self.date = date if date
  self.public = public
  @content = content
  @site = site
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



11
12
13
# File 'lib/mill/resource.rb', line 11

def content
  @content
end

#dateObject

Returns the value of attribute date.



9
10
11
# File 'lib/mill/resource.rb', line 9

def date
  @date
end

#input_fileObject

Returns the value of attribute input_file.



7
8
9
# File 'lib/mill/resource.rb', line 7

def input_file
  @input_file
end

#output_fileObject

Returns the value of attribute output_file.



8
9
10
# File 'lib/mill/resource.rb', line 8

def output_file
  @output_file
end

#publicObject

Returns the value of attribute public.



10
11
12
# File 'lib/mill/resource.rb', line 10

def public
  @public
end

#siteObject

Returns the value of attribute site.



12
13
14
# File 'lib/mill/resource.rb', line 12

def site
  @site
end

Instance Method Details

#absolute_uriObject



95
96
97
# File 'lib/mill/resource.rb', line 95

def absolute_uri
  @site.site_uri + uri
end

#buildObject



115
116
117
# File 'lib/mill/resource.rb', line 115

def build
  # implemented in subclass
end

#change_frequencyObject



103
104
105
# File 'lib/mill/resource.rb', line 103

def change_frequency
  :weekly
end

#final_contentObject



107
108
109
# File 'lib/mill/resource.rb', line 107

def final_content
  @content
end

#find_sibling_resources(klass = nil) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/mill/resource.rb', line 74

def find_sibling_resources(klass=nil)
  # parent_uri = parent_uri
  @site.resources.select do |resource|
    resource != self &&
      (klass.nil? || resource.kind_of?(klass)) &&
      resource.parent_uri == parent_uri
  end
end

#inspectObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/mill/resource.rb', line 63

def inspect
  "<%p> input_file: %p, output_file: %p, date: %s, public: %p, content: <%p>" % [
    self.class,
    @input_file ? @input_file.relative_to(@site.input_dir).to_s : nil,
    @output_file ? @output_file.relative_to(@site.output_dir).to_s : nil,
    @date.to_s,
    @public,
    @content && @content.class,
  ]
end

#loadObject



111
112
113
# File 'lib/mill/resource.rb', line 111

def load
  # implemented in subclass
end

#parent_uriObject



91
92
93
# File 'lib/mill/resource.rb', line 91

def parent_uri
  uri + '.'
end

#public?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/mill/resource.rb', line 59

def public?
  @public
end

#saveObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mill/resource.rb', line 119

def save
  @output_file.dirname.mkpath
  if (content = final_content)
    # ;;warn "#{uri}: writing #{@input_file} to #{@output_file}"
    @output_file.write(content.to_s)
    @output_file.utime(@date.to_time, @date.to_time)
  elsif @input_file
    # ;;warn "#{uri}: copying #{@input_file} to #{@output_file}"
    @input_file.copy(@output_file)
  else
    raise Error, "Can't build resource without content or input file: #{uri}"
  end
end

#tag_uriObject



99
100
101
# File 'lib/mill/resource.rb', line 99

def tag_uri
  @site.tag_uri + uri
end

#uriObject

Raises:



83
84
85
86
87
88
89
# File 'lib/mill/resource.rb', line 83

def uri
  raise Error, "#{@input_file}: No output file defined for #{self.class}" unless @output_file
  path = '/' + @output_file.relative_to(@site.output_dir).to_s
  path.sub!(%r{/index\.html$}, '/')
  path.sub!(%r{\.html$}, '') if @site.shorten_uris
  Addressable::URI.encode(path, Addressable::URI)
end