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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/ro/node.rb', line 212 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
445 446 447 |
# File 'lib/ro/node.rb', line 445 def _attributes_yml File.join(@path, 'attributes.yml') end |
#_binding ⇒ Object
412 413 414 |
# File 'lib/ro/node.rb', line 412 def _binding Kernel.binding end |
#_cache_key ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/ro/node.rb', line 416 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
298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/ro/node.rb', line 298 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
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/ro/node.rb', line 394 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
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/ro/node.rb', line 355 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
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/ro/node.rb', line 339 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
441 442 443 |
# File 'lib/ro/node.rb', line 441 def _load_from_cache false end |
#_load_from_cache_or_disk ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/ro/node.rb', line 312 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
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/ro/node.rb', line 377 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
234 235 236 |
# File 'lib/ro/node.rb', line 234 def attributes _load{ @attributes } end |
#attributes=(attributes) ⇒ Object
238 239 240 |
# File 'lib/ro/node.rb', line 238 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
242 243 244 |
# File 'lib/ro/node.rb', line 242 def instance_eval(*args, &block) _load{ super } end |
#load!(&block) ⇒ Object
294 295 296 |
# File 'lib/ro/node.rb', line 294 def load!(&block) _load(&block) end |
#loaded ⇒ Object
290 291 292 |
# File 'lib/ro/node.rb', line 290 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
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 280 281 282 283 |
# File 'lib/ro/node.rb', line 246 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
207 208 209 210 |
# File 'lib/ro/node.rb', line 207 def relative_path re = /^#{ Regexp.escape(Ro.root) }/ @path.to_s.gsub(re, '') end |
#reload(&block) ⇒ Object
285 286 287 288 |
# File 'lib/ro/node.rb', line 285 def reload(&block) @loaded = false _load(&block) end |
#route(*args) ⇒ Object
202 203 204 205 |
# File 'lib/ro/node.rb', line 202 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
197 198 199 200 |
# File 'lib/ro/node.rb', line 197 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 184 185 186 187 |
# File 'lib/ro/node.rb', line 141 def url_for(*args, &block) = Map.(args) cache_buster = .delete('_') != false 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)}.flatten.compact.uniq case candidates.size when 0 raise ArgumentError.new("no asset matching #{ globs.inspect }") else if candidates.size > 1 ::Kernel.warn("many assets in (#{ candidates.inspect }) R teh match 4 #{ globs.inspect }") end path = candidates.first path_info = path.gsub(/^#{ Regexp.escape(Ro.root) }/, '') if cache_buster #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
189 190 191 192 193 194 195 |
# File 'lib/ro/node.rb', line 189 def url_for?(*args, &block) begin url_for(*args, &block) rescue nil end end |