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.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mill/resource.rb', line 16

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
  if output_file
    @output_file = Path.new(output_file)
    @path = '/' + @output_file.relative_to(site.output_dir).to_s
  end
  self.date = date if date
  self.public = public
  @content = content
  @site = site
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

#dateObject

Returns the value of attribute date.



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

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

#nodeObject

Returns the value of attribute node.



14
15
16
# File 'lib/mill/resource.rb', line 14

def node
  @node
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

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#publicObject

Returns the value of attribute public.



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

def public
  @public
end

#siteObject

Returns the value of attribute site.



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

def site
  @site
end

Instance Method Details

#absolute_uriObject



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

def absolute_uri
  @site.site_uri + uri
end

#buildObject



128
129
130
# File 'lib/mill/resource.rb', line 128

def build
  # implemented in subclass
end

#change_frequencyObject



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

def change_frequency
  :weekly
end

#childrenObject



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

def children
  @node.children.map(&:content).compact
end

#final_contentObject



120
121
122
# File 'lib/mill/resource.rb', line 120

def final_content
  @content
end

#inspectObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mill/resource.rb', line 76

def inspect
  "<%p> input_file: %p, output_file: %p, path: %s, date: %s, public: %p, content: <%p>, parent: %p, siblings: %p, children: %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,
    @path,
    @date.to_s,
    @public,
    @content&.class,
    parent&.path,
    siblings.map(&:path),
    children.map(&:path),
  ]
end

#loadObject



124
125
126
# File 'lib/mill/resource.rb', line 124

def load
  # implemented in subclass
end

#parentObject



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

def parent
  @node.parent&.content
end

#public?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/mill/resource.rb', line 64

def public?
  @public == true
end

#redirect?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/mill/resource.rb', line 72

def redirect?
  kind_of?(Resource::Redirect)
end

#saveObject



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mill/resource.rb', line 132

def save
  @output_file.dirname.mkpath
  if (content = final_content)
    # ;;warn "#{path}: 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 "#{path}: copying #{@input_file} to #{@output_file}"
    @input_file.copy(@output_file)
  else
    raise Error, "Can't build resource without content or input file: #{path}"
  end
end

#siblingsObject



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

def siblings
  @node.siblings.map(&:content).compact
end

#tag_uriObject



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

def tag_uri
  @site.tag_uri + uri
end

#text?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/mill/resource.rb', line 68

def text?
  kind_of?(Resource::Text) && public?
end

#uriObject

Raises:



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

def uri
  raise Error, "#{@input_file}: No path defined for #{self.class}" unless @path
  Addressable::URI.encode(@path, Addressable::URI)
end