Class: Ace::Item
- Inherits:
-
Object
- Object
- Ace::Item
- Defined in:
- lib/ace.rb
Overview
This class represents the items which will be eventually rendered like concrete posts, tags etc.
Instance Attribute Summary collapse
-
#content ⇒ Object
Content can be anything, not just a string.
-
#metadata ⇒ Object
Content can be anything, not just a string.
-
#original_path ⇒ Object
Returns the value of attribute original_path.
- #output_path ⇒ Object
Class Method Summary collapse
- .after(filter, *args) ⇒ Object
- .after_filters ⇒ Object
- .all_instances ⇒ Object
- .all_subclasses ⇒ Object
- .before(filter, *args) ⇒ Object
- .before_filters ⇒ Object
- .create(*args) ⇒ Object
- .inherited(subclass) ⇒ Object
- .instances ⇒ Object
- .subclasses ⇒ Object
Instance Method Summary collapse
- #base_url ⇒ Object
- #config ⇒ Object
- #digest(data) ⇒ Object
-
#initialize(metadata, content, original_path) ⇒ Item
constructor
A new instance of Item.
- #permalink ⇒ Object
- #register ⇒ Object
- #render ⇒ Object
- #save! ⇒ Object
- #server_path ⇒ Object
- #unregister ⇒ Object
Constructor Details
#initialize(metadata, content, original_path) ⇒ Item
Returns a new instance of Item.
104 105 106 107 108 |
# File 'lib/ace.rb', line 104 def initialize(, content, original_path) @metadata = @content = content @original_path = original_path end |
Instance Attribute Details
#content ⇒ Object
Content can be anything, not just a string.
102 103 104 |
# File 'lib/ace.rb', line 102 def content @content end |
#metadata ⇒ Object
Content can be anything, not just a string.
102 103 104 |
# File 'lib/ace.rb', line 102 def @metadata end |
#original_path ⇒ Object
Returns the value of attribute original_path.
103 104 105 |
# File 'lib/ace.rb', line 103 def original_path @original_path end |
#output_path ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/ace.rb', line 161 def output_path @output_path ||= begin unless self.original_path.nil? self.original_path.sub("content", "output") end end end |
Class Method Details
.after(filter, *args) ⇒ Object
93 94 95 |
# File 'lib/ace.rb', line 93 def self.after(filter, *args) self.after_filters << filter.new(*args) end |
.after_filters ⇒ Object
89 90 91 |
# File 'lib/ace.rb', line 89 def self.after_filters @after_filters ||= Array.new end |
.all_instances ⇒ Object
77 78 79 |
# File 'lib/ace.rb', line 77 def self.all_instances self.all_subclasses.map(&:instances).flatten end |
.all_subclasses ⇒ Object
73 74 75 |
# File 'lib/ace.rb', line 73 def self.all_subclasses self.subclasses + self.subclasses.map(&:subclasses).flatten end |
.before(filter, *args) ⇒ Object
85 86 87 |
# File 'lib/ace.rb', line 85 def self.before(filter, *args) self.before_filters << filter.new(*args) end |
.before_filters ⇒ Object
81 82 83 |
# File 'lib/ace.rb', line 81 def self.before_filters @before_filters ||= Array.new end |
.create(*args) ⇒ Object
97 98 99 |
# File 'lib/ace.rb', line 97 def self.create(*args) self.new(*args).tap(&:register) end |
.inherited(subclass) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ace.rb', line 53 def self.inherited(subclass) self.subclasses << subclass self.before_filters.each do |instance| subclass.before_filters << instance.dup end self.after_filters.each do |instance| subclass.after_filters << instance.dup end end |
.instances ⇒ Object
69 70 71 |
# File 'lib/ace.rb', line 69 def self.instances @instances ||= Array.new end |
.subclasses ⇒ Object
65 66 67 |
# File 'lib/ace.rb', line 65 def self.subclasses @subclasses ||= [self] end |
Instance Method Details
#base_url ⇒ Object
144 145 146 |
# File 'lib/ace.rb', line 144 def base_url self.config[:base_url] end |
#config ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/ace.rb', line 110 def config @config ||= begin YAML::load_file("config.yml").inject(Hash.new) do |hash, pair| hash.merge!(pair[0].to_sym => pair[1]) end end end |
#digest(data) ⇒ Object
156 157 158 |
# File 'lib/ace.rb', line 156 def digest(data) Digest::SHA1.hexdigest(data) end |
#permalink ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/ace.rb', line 148 def permalink if self.config[:base_url].nil? raise "You have to add :base_url into config.yml or redefine #base_url method!" end "#{self.base_url}#{self.server_path}" end |
#register ⇒ Object
118 119 120 121 122 123 |
# File 'lib/ace.rb', line 118 def register instances = self.class.instances unless instances.include?(self) self.class.instances << self end end |
#render ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/ace.rb', line 129 def render output = self.class.before_filters.inject(self.content) do |buffer, filter| filter.call(self, buffer) end self.class.after_filters.inject(output) do |buffer, filter| filter.call(self, buffer) end end |
#save! ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/ace.rb', line 169 def save! puts "~ [RENDER] #{self.output_path}" content = self.render # so filters can influence output_path begin old_content = File.open(self.output_path, "rb") { |f| f.read } rescue old_content = '' end if self.digest(content) != self.digest(old_content) warn "~ CRC isn't same, save new content into #{self.output_path}" # puts old_content.inspect # puts content.inspect FileUtils.mkdir_p File.dirname(self.output_path) File.open(self.output_path, "wb") do |file| file.puts(content) end end end |
#server_path ⇒ Object
139 140 141 142 |
# File 'lib/ace.rb', line 139 def server_path absolute = self.output_path.sub(/^output\//, "") "/#{absolute}" end |
#unregister ⇒ Object
125 126 127 |
# File 'lib/ace.rb', line 125 def unregister self.class.instances.delete(self) end |