Class: Magneto::Item
Constant Summary
collapse
- INVALID_LOCATION_MATCH_PATTERN =
%r{(^[^/].*$|^.*/$)}
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Readable
#content, #content=, #content?, #metadata, #metadata=, #metadata?, #read
Constructor Details
#initialize(site, origin = '') ⇒ Item
Returns a new instance of Item.
12
13
14
15
16
17
18
19
20
|
# File 'lib/magneto/item.rb', line 12
def initialize(site, origin = '')
super()
@site = site
@origin = origin.sub(INVALID_LOCATION_MATCH_PATTERN, '')
@destination = nil
@metadata = nil
@content = nil
@precomposed_content = nil
end
|
Instance Attribute Details
#origin ⇒ Object
Returns the value of attribute origin.
8
9
10
|
# File 'lib/magneto/item.rb', line 8
def origin
@origin
end
|
#site ⇒ Object
Returns the value of attribute site.
8
9
10
|
# File 'lib/magneto/item.rb', line 8
def site
@site
end
|
Instance Method Details
#abandon ⇒ Object
34
35
36
|
# File 'lib/magneto/item.rb', line 34
def abandon
@destination = ''
end
|
#abandoned? ⇒ Boolean
30
31
32
|
# File 'lib/magneto/item.rb', line 30
def abandoned?
@destination == ''
end
|
#apply_filter(filter_name, args = {}) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/magneto/item.rb', line 66
def apply_filter(filter_name, args = {})
filter = @site.filters[filter_name.to_sym]
if filter.nil?
$stderr.puts "#{File.basename($PROGRAM_NAME)}: #{ex.to_s}"
raise "Couldn't find filter: '#{filter_name.to_s}'"
end
read if @content.nil?
@content = filter.apply(@content, @site.config.deep_merge(@metadata || {}).deep_merge({
:config => @site.config,
:site => @site,
:item => self
}).deep_merge(filter_name.to_sym => args))
end
|
#apply_template(template_name, args = {}) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/magneto/item.rb', line 83
def apply_template(template_name, args = {})
template = @site.templates[template_name.to_sym]
if template.nil?
$stderr.puts "#{File.basename($PROGRAM_NAME)}: #{ex.to_s}"
raise "Couldn't find template: '#{template_name.to_s}'"
end
read if @content.nil?
@precomposed_content ||= @content.dup
@content = template.filter.apply(template.content, {
template.filter.name.to_sym => {}
}.deep_merge(@site.config).deep_merge(@metadata || {}).deep_merge(template.metadata || {}).deep_merge({
:config => @site.config,
:site => @site,
:item => self,
:content => @content
}).deep_merge(args.symbolize_keys))
end
|
#destination ⇒ Object
38
39
40
41
|
# File 'lib/magneto/item.rb', line 38
def destination
@destination ||= @origin.dup
@destination
end
|
#destination=(destination) ⇒ Object
43
44
45
|
# File 'lib/magneto/item.rb', line 43
def destination=(destination)
@destination = (destination || '').sub(INVALID_LOCATION_MATCH_PATTERN, '')
end
|
#destination_path ⇒ Object
53
54
55
|
# File 'lib/magneto/item.rb', line 53
def destination_path
@site.config[:output_path] + (@destination || @origin)
end
|
57
58
59
|
# File 'lib/magneto/item.rb', line 57
def import_metadata
self.destination = @metadata[:destination] unless @metadata.nil? || @metadata[:destination].nil?
end
|
#origin_path ⇒ Object
Also known as:
path
47
48
49
|
# File 'lib/magneto/item.rb', line 47
def origin_path
@site.items_path + @origin
end
|
#precomposed_content ⇒ Object
61
62
63
64
|
# File 'lib/magneto/item.rb', line 61
def precomposed_content
read if @content.nil?
@precomposed_content || @content.dup
end
|
#relocate ⇒ Object
26
27
28
|
# File 'lib/magneto/item.rb', line 26
def relocate
@origin = ''
end
|
#relocated? ⇒ Boolean
22
23
24
|
# File 'lib/magneto/item.rb', line 22
def relocated?
@origin == ''
end
|
#write ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/magneto/item.rb', line 104
def write
unless abandoned?
if content?
FileUtils.mkdir_p File.dirname(destination_path)
File.open(destination_path, 'w') { |f| f.write content }
else
unless relocated?
FileUtils.mkdir_p File.dirname(destination_path)
FileUtils.cp origin_path, destination_path
end
end
end
end
|