Class: Ro::Node
- Inherits:
-
Object
- Object
- Ro::Node
- Defined in:
- lib/ro/node.rb,
lib/ro/node/list.rb
Defined Under Namespace
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](*args) ⇒ Object
- #_attributes_yml ⇒ Object
- #_binding ⇒ Object
- #_cache_key ⇒ Object
- #_id ⇒ Object
- #_load(&block) ⇒ Object
- #_load_assets ⇒ Object
- #_load_attribute_files ⇒ Object
- #_load_attributes_yml ⇒ Object
- #_load_from_cache ⇒ Object
- #_load_from_cache_or_disk ⇒ Object
- #_load_sources ⇒ Object
- #_path ⇒ Object
- #_slug ⇒ Object
- #_type ⇒ Object
- #asset_dir ⇒ Object
- #asset_path(*args, &block) ⇒ Object
- #asset_paths ⇒ Object
- #asset_urls ⇒ Object
- #assets ⇒ Object
- #attributes ⇒ Object
- #attributes=(attributes) ⇒ Object
- #basename ⇒ Object
- #get(*args) ⇒ Object
- #hash ⇒ Object
- #id ⇒ Object
- #identifier ⇒ Object
-
#initialize(path) ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ Object
- #instance_eval(*args, &block) ⇒ Object
- #load!(&block) ⇒ Object
- #loaded ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
- #node ⇒ Object
- #path ⇒ Object
- #related(*args, &block) ⇒ Object
- #relative_path ⇒ Object
- #reload(&block) ⇒ Object
- #route(*args) ⇒ Object
- #slug ⇒ Object
- #source_for(*args) ⇒ Object
- #to_s ⇒ Object
- #type ⇒ Object
- #url_for(*args, &block) ⇒ Object
- #url_for?(*args, &block) ⇒ Boolean
Constructor Details
#initialize(path) ⇒ Node
Returns a new instance of Node.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ro/node.rb', line 9 def initialize(path) @path = Ro.realpath(path.to_s) @id = File.basename(@path) @slug = Slug.for(@id) @type = File.basename(File.dirname(@path)) @root = Ro::Root.new(File.dirname(File.dirname(@path))) @loaded = false @loading = false @attributes = Map.new @fields = Map.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/ro/node.rb', line 208 def method_missing(method, *args, &block) super if method.to_s == 'to_ary' Ro.log "Ro::Node(#{ identifier })#method_missing(#{ method.inspect }, #{ args.inspect })" key = method.to_s if @attributes.has_key?(key) return @attributes[key] end _load do return( if @attributes.has_key?(key) @attributes[key] else super end ) end end |
Instance Method Details
#==(other) ⇒ Object
61 62 63 |
# File 'lib/ro/node.rb', line 61 def ==(other) attributes == other.attributes end |
#[](*args) ⇒ Object
85 86 87 |
# File 'lib/ro/node.rb', line 85 def [](*args) attributes.get(*args) end |
#_attributes_yml ⇒ Object
441 442 443 |
# File 'lib/ro/node.rb', line 441 def _attributes_yml File.join(@path, 'attributes.yml') end |
#_binding ⇒ Object
408 409 410 |
# File 'lib/ro/node.rb', line 408 def _binding Kernel.binding end |
#_cache_key ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/ro/node.rb', line 412 def _cache_key glob = File.join(@path, '**/**') entries = [] Dir.glob(glob) do |entry| stat = begin File.stat(entry) rescue next end = [stat.ctime, stat.mtime].max relative_path = Ro.relative_path(entry, :to => @path) entries.push([relative_path, .iso8601(2)]) end fingerprint = entries.map{|pair| pair.join('@')}.join(', ') md5 = Ro.md5(fingerprint) [@path, md5] end |
#_id ⇒ Object
25 26 27 |
# File 'lib/ro/node.rb', line 25 def _id @id end |
#_load(&block) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/ro/node.rb', line 294 def _load(&block) unless @loaded if @loading return(block ? block.call : :loading) end @loading = true @loaded = _load_from_cache_or_disk @loading = false end block ? block.call : @loaded end |
#_load_assets ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/ro/node.rb', line 390 def _load_assets glob = File.join(@path, 'assets/**/**') node = self Dir.glob(glob) do |path| next if test(?d, path) relative_path = Ro.relative_path(path, :to => "#{ @path }/assets") url = url_for(relative_path) key = relative_path.split('/') key.unshift('urls') @attributes.set(key => url) end end |
#_load_attribute_files ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/ro/node.rb', line 351 def _load_attribute_files glob = File.join(@path, '**/**') node = self Dir.glob(glob) do |path| next if test(?d, path) basename = File.basename(path) next if basename == 'attributes.yml' relative_path = Ro.relative_path(path, :to => @path) next if relative_path =~ /^assets\// key = relative_path.split('.', 2).first.split('/') html = Ro.render(path, node) html = Ro.(html, node) @attributes.set(key => html) end end |
#_load_attributes_yml ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/ro/node.rb', line 335 def _load_attributes_yml if test(?s, _attributes_yml) buf = IO.binread(_attributes_yml) data = YAML.load(buf) data = data.is_a?(Hash) ? data : {'_' => data} @attributes.update(data) %w( assets ).each do |key| raise ArgumentError.new("attributes.yml may not contain the key '#{ key }'") if @attributes.has_key?(key) end @attributes end end |
#_load_from_cache ⇒ Object
437 438 439 |
# File 'lib/ro/node.rb', line 437 def _load_from_cache false end |
#_load_from_cache_or_disk ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/ro/node.rb', line 308 def _load_from_cache_or_disk cache_key = _cache_key cached = Ro.cache.read(cache_key) if cached Ro.log "loading #{ identifier } from cache" @attributes = Map.new.update(cached) return :cache else Ro.log "loading #{ identifier } from disk" @attributes = Map.new _load_attributes_yml _load_attribute_files _load_sources _load_assets Ro.cache.write(cache_key, @attributes) return :disk end end |
#_load_sources ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/ro/node.rb', line 373 def _load_sources glob = File.join(@path, 'assets/source/*') node = self Dir.glob(glob) do |path| next if test(?d, path) basename = File.basename(path) key, ext = basename.split('.', 2) next if basename == 'attributes.yml' value = Ro.render_source(path, node) @attributes.set([:assets, :source, basename] => value) end end |
#_path ⇒ Object
41 42 43 |
# File 'lib/ro/node.rb', line 41 def _path @path end |
#_slug ⇒ Object
49 50 51 |
# File 'lib/ro/node.rb', line 49 def _slug @slug end |
#_type ⇒ Object
33 34 35 |
# File 'lib/ro/node.rb', line 33 def _type @type end |
#asset_dir ⇒ Object
93 94 95 |
# File 'lib/ro/node.rb', line 93 def asset_dir File.join(path, 'assets') end |
#asset_path(*args, &block) ⇒ Object
89 90 91 |
# File 'lib/ro/node.rb', line 89 def asset_path(*args, &block) File.join(relative_path, 'assets') end |
#asset_paths ⇒ Object
97 98 99 |
# File 'lib/ro/node.rb', line 97 def asset_paths Dir.glob("#{ asset_dir }/**/**").select{|entry| test(?f, entry)} end |
#asset_urls ⇒ Object
109 110 111 |
# File 'lib/ro/node.rb', line 109 def asset_urls assets.map(&:url) end |
#assets ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/ro/node.rb', line 101 def assets asset_paths.map do |path| name = path.sub(asset_dir + "/", "") url = url_for(name) Asset.new(name, :path => path, :url => url) end end |
#attributes ⇒ Object
230 231 232 |
# File 'lib/ro/node.rb', line 230 def attributes _load{ @attributes } end |
#attributes=(attributes) ⇒ Object
234 235 236 |
# File 'lib/ro/node.rb', line 234 def attributes=(attributes) @attributes = attributes end |
#basename ⇒ Object
73 74 75 |
# File 'lib/ro/node.rb', line 73 def basename name end |
#get(*args) ⇒ Object
81 82 83 |
# File 'lib/ro/node.rb', line 81 def get(*args) attributes.get(*args) end |
#hash ⇒ Object
57 58 59 |
# File 'lib/ro/node.rb', line 57 def hash identifier.hash end |
#id ⇒ Object
21 22 23 |
# File 'lib/ro/node.rb', line 21 def id @id end |
#identifier ⇒ Object
53 54 55 |
# File 'lib/ro/node.rb', line 53 def identifier "#{ _type }/#{ _id }" end |
#inspect ⇒ Object
65 66 67 |
# File 'lib/ro/node.rb', line 65 def inspect identifier end |
#instance_eval(*args, &block) ⇒ Object
238 239 240 |
# File 'lib/ro/node.rb', line 238 def instance_eval(*args, &block) _load{ super } end |
#load!(&block) ⇒ Object
290 291 292 |
# File 'lib/ro/node.rb', line 290 def load!(&block) _load(&block) end |
#loaded ⇒ Object
286 287 288 |
# File 'lib/ro/node.rb', line 286 def loaded attributes end |
#node ⇒ Object
77 78 79 |
# File 'lib/ro/node.rb', line 77 def node self end |
#path ⇒ Object
37 38 39 |
# File 'lib/ro/node.rb', line 37 def path attributes[:path] || @path end |
#related(*args, &block) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/ro/node.rb', line 242 def (*args, &block) _load{ = @attributes.get(:related) || Map.new nodes = List.new(root) list = root.nodes which = Coerce.list_of_strings(args) .each do |relationship, value| unless which.empty? next unless which.include?(relationship.to_s) end type, names = case value when Hash value.to_a.first else [relationship, value] end names = Coerce.list_of_strings(names) names.each do |name| identifier = "#{ type }/#{ name }" node = list.index[identifier] node._load{ nodes.add(node) } end end case when block.nil? nodes when block nodes.where(&block) end } end |
#relative_path ⇒ Object
203 204 205 206 |
# File 'lib/ro/node.rb', line 203 def relative_path re = /^#{ Regexp.escape(Ro.root) }/ @path.to_s.gsub(re, '') end |
#reload(&block) ⇒ Object
281 282 283 284 |
# File 'lib/ro/node.rb', line 281 def reload(&block) @loaded = false _load(&block) end |
#route(*args) ⇒ Object
198 199 200 201 |
# File 'lib/ro/node.rb', line 198 def route(*args) path_info = Ro.absolute_path_for(Ro.route, relative_path) [Ro.asset_host, path_info].compact.join('/') end |
#slug ⇒ Object
45 46 47 |
# File 'lib/ro/node.rb', line 45 def slug attributes[:slug] || @slug end |
#source_for(*args) ⇒ Object
193 194 195 196 |
# File 'lib/ro/node.rb', line 193 def source_for(*args) key = Ro.relative_path_for(:assets, :source, args).split('/') get(key) end |
#to_s ⇒ Object
69 70 71 |
# File 'lib/ro/node.rb', line 69 def to_s inspect end |
#type ⇒ Object
29 30 31 |
# File 'lib/ro/node.rb', line 29 def type attributes[:type] || @type end |
#url_for(*args, &block) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/ro/node.rb', line 141 def url_for(*args, &block) = Map.(args) = .delete('timestamp') path_info = Ro.relative_path_for(args) path = File.join(@path.to_s, 'assets', path_info) glob = path_info.gsub(/[_-]/, '[_-]') globs = [ File.join(@path.to_s, 'assets', "#{ glob }"), File.join(@path.to_s, 'assets', "#{ glob }*"), File.join(@path.to_s, 'assets', "**/#{ glob }") ] candidates = globs.map{|glob| Dir.glob(glob, ::File::FNM_CASEFOLD)}.flatten.compact.uniq case candidates.size when 0 raise ArgumentError.new("no asset matching #{ globs.inspect }") else path = candidates.first path_info = path.gsub(/^#{ Regexp.escape(Ro.root) }/, '') if #options['_'] = Ro.md5(IO.binread(path)) = File.stat(path).mtime.utc.to_i ['_'] = end end url = File.join(Ro.route, path_info) if .empty? url else query_string = Ro.query_string_for() "#{ url }?#{ query_string }" end end |
#url_for?(*args, &block) ⇒ Boolean
185 186 187 188 189 190 191 |
# File 'lib/ro/node.rb', line 185 def url_for?(*args, &block) begin url_for(*args, &block) rescue nil end end |