Class: Ro::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/ro/node.rb,
lib/ro/node/list.rb

Defined Under Namespace

Classes: Asset, List

Instance Method Summary collapse

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_ymlObject



441
442
443
# File 'lib/ro/node.rb', line 441

def _attributes_yml
  File.join(@path, 'attributes.yml')
end

#_bindingObject



408
409
410
# File 'lib/ro/node.rb', line 408

def _binding
  Kernel.binding
end

#_cache_keyObject



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

    timestamp = [stat.ctime, stat.mtime].max
    relative_path = Ro.relative_path(entry, :to => @path)
    entries.push([relative_path, timestamp.iso8601(2)]) 
  end

  fingerprint = entries.map{|pair| pair.join('@')}.join(', ')

  md5 = Ro.md5(fingerprint)

  [@path, md5]
end

#_idObject



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_assetsObject



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_filesObject



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.expand_asset_urls(html, node)

    @attributes.set(key => html)
  end
end

#_load_attributes_ymlObject



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_cacheObject



437
438
439
# File 'lib/ro/node.rb', line 437

def _load_from_cache
  false
end

#_load_from_cache_or_diskObject



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_sourcesObject



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

#_pathObject



41
42
43
# File 'lib/ro/node.rb', line 41

def _path
  @path
end

#_slugObject



49
50
51
# File 'lib/ro/node.rb', line 49

def _slug
  @slug
end

#_typeObject



33
34
35
# File 'lib/ro/node.rb', line 33

def _type
  @type
end

#asset_dirObject



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_pathsObject



97
98
99
# File 'lib/ro/node.rb', line 97

def asset_paths
  Dir.glob("#{ asset_dir }/**/**").select{|entry| test(?f, entry)}
end

#asset_urlsObject



109
110
111
# File 'lib/ro/node.rb', line 109

def asset_urls
  assets.map(&:url)
end

#assetsObject



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

#attributesObject



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

#basenameObject



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

#hashObject



57
58
59
# File 'lib/ro/node.rb', line 57

def hash
  identifier.hash
end

#idObject



21
22
23
# File 'lib/ro/node.rb', line 21

def id
  @id
end

#identifierObject



53
54
55
# File 'lib/ro/node.rb', line 53

def identifier
  "#{ _type }/#{ _id }"
end

#inspectObject



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

#loadedObject



286
287
288
# File 'lib/ro/node.rb', line 286

def loaded
  attributes
end

#nodeObject



77
78
79
# File 'lib/ro/node.rb', line 77

def node
  self
end

#pathObject



37
38
39
# File 'lib/ro/node.rb', line 37

def path
  attributes[:path] || @path
end


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 related(*args, &block)
  _load{
    related = @attributes.get(:related) || Map.new
    nodes = List.new(root)
    list = root.nodes
    which = Coerce.list_of_strings(args) 

    related.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_pathObject



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

#slugObject



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_sObject



69
70
71
# File 'lib/ro/node.rb', line 69

def to_s
  inspect
end

#typeObject



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)
  options = Map.options_for!(args)

  timestamp = options.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 timestamp
        #options['_'] = Ro.md5(IO.binread(path))
        timestamp = File.stat(path).mtime.utc.to_i
        options['_'] = timestamp
      end
  end

  url = File.join(Ro.route, path_info)

  if options.empty?
    url
  else
    query_string = Ro.query_string_for(options)
    "#{ url }?#{ query_string }"
  end
end

#url_for?(*args, &block) ⇒ Boolean

Returns:

  • (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