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_for(*args, &block) ⇒ Object
- #asset_for?(*args, &block) ⇒ Boolean
- #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
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/ro/node.rb', line 241 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
474 475 476 |
# File 'lib/ro/node.rb', line 474 def _attributes_yml File.join(@path, 'attributes.yml') end |
#_binding ⇒ Object
441 442 443 |
# File 'lib/ro/node.rb', line 441 def _binding Kernel.binding end |
#_cache_key ⇒ Object
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/ro/node.rb', line 445 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
327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/ro/node.rb', line 327 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
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/ro/node.rb', line 423 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
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/ro/node.rb', line 384 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
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/ro/node.rb', line 368 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
470 471 472 |
# File 'lib/ro/node.rb', line 470 def _load_from_cache false end |
#_load_from_cache_or_disk ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/ro/node.rb', line 341 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
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/ro/node.rb', line 406 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_for(*args, &block) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/ro/node.rb', line 114 def asset_for(*args, &block) = Map.(args) 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 name = path.sub(asset_dir + "/", "") path_info = path.gsub(/^#{ Regexp.escape(Ro.root) }/, '') url = File.join(Ro.route, path_info) end Asset.new(name, :path => path, :url => url) end |
#asset_for?(*args, &block) ⇒ Boolean
145 146 147 148 149 150 151 |
# File 'lib/ro/node.rb', line 145 def asset_for?(*args, &block) begin asset_for(*args, &block) rescue nil end 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
110 111 112 |
# File 'lib/ro/node.rb', line 110 def asset_urls assets.map(&:url) end |
#assets ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/ro/node.rb', line 101 def assets asset_paths.map do |path| name = path.sub(asset_dir + "/", "") path_info = path.gsub(/^#{ Regexp.escape(Ro.root) }/, '') url = File.join(Ro.route, path_info) Asset.new(name, :path => path, :url => url) end end |
#attributes ⇒ Object
263 264 265 |
# File 'lib/ro/node.rb', line 263 def attributes _load{ @attributes } end |
#attributes=(attributes) ⇒ Object
267 268 269 |
# File 'lib/ro/node.rb', line 267 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
271 272 273 |
# File 'lib/ro/node.rb', line 271 def instance_eval(*args, &block) _load{ super } end |
#load!(&block) ⇒ Object
323 324 325 |
# File 'lib/ro/node.rb', line 323 def load!(&block) _load(&block) end |
#loaded ⇒ Object
319 320 321 |
# File 'lib/ro/node.rb', line 319 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
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/ro/node.rb', line 275 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
236 237 238 239 |
# File 'lib/ro/node.rb', line 236 def relative_path re = /^#{ Regexp.escape(Ro.root) }/ @path.to_s.gsub(re, '') end |
#reload(&block) ⇒ Object
314 315 316 317 |
# File 'lib/ro/node.rb', line 314 def reload(&block) @loaded = false _load(&block) end |
#route(*args) ⇒ Object
231 232 233 234 |
# File 'lib/ro/node.rb', line 231 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
226 227 228 229 |
# File 'lib/ro/node.rb', line 226 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
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/ro/node.rb', line 191 def url_for(*args, &block) = Map.(args) opts = Map.new opts[:timestamp] = .delete(:timestamp) args.push() asset = asset_for(*args, &block) if ts = opts.delete(:timestamp) if ts == true opts[:_] = File.stat(asset.path).mtime.utc.to_i else opts[:_] = ts end end if opts.empty? asset.url else query_string = Ro.query_string_for(opts) "#{ asset.url }?#{ query_string }" end end |
#url_for?(*args, &block) ⇒ Boolean
218 219 220 221 222 223 224 |
# File 'lib/ro/node.rb', line 218 def url_for?(*args, &block) begin url_for(*args, &block) rescue nil end end |