Class: RDF::SAK::Transform::PartialCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/sak/transform.rb

Overview

This class implements a cache for partial transformation function applications, which bundle transforms with a set of instance parameters under a reusable identity.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(harness) ⇒ PartialCache

Initialize an empty cache.

Parameters:



395
396
397
398
399
400
# File 'lib/rdf/sak/transform.rb', line 395

def initialize harness
  @harness    = harness
  @cache      = {}
  @mapping    = {}
  @transforms = {}
end

Instance Attribute Details

#harnessObject (readonly)

Returns the value of attribute harness.



390
391
392
# File 'lib/rdf/sak/transform.rb', line 390

def harness
  @harness
end

Class Method Details

.load(harness) ⇒ RDF::SAK::Transform::PartialCache

Initialize the cache with all partials pre-loaded.

Parameters:

Returns:



386
387
388
# File 'lib/rdf/sak/transform.rb', line 386

def self.load harness
  new(harness).load
end

Instance Method Details

#get(transform, params) ⇒ Object

Retrieve a Partial from the cache based on its



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/rdf/sak/transform.rb', line 429

def get transform, params
  ts = case transform
       when RDF::SAK::Transform then transform.subject
       when RDF::URI
         # XXX transforms resolved here may not get implemented
         transform = RDF::SAK::Transform.resolve @repo, transform
         transform.subject
       else
         raise ArgumentError, "Don't know what to do with #{transform}"
       end

  # return direct cache entry if transform is really the subject
  return @cache[ts] if @cache.key? 

  # otherwise return the mapping
  @mapping[transform][coerce_params params]
end

#loadself

Load an initialized partial cache.

Returns:

  • (self)

    daisy-chainable self-reference



406
407
408
409
410
411
412
413
414
# File 'lib/rdf/sak/transform.rb', line 406

def load
  RDF::SAK::Util.subjects_for(repo, RDF.type,
                              RDF::SAK::TFO.Partial).each do |s|
    resolve subject: s
  end

  # return self to daisy-chain
  self
end

#partialsObject



416
417
418
# File 'lib/rdf/sak/transform.rb', line 416

def partials
  @cache.keys.select { |x| x.is_a? RDF::Resource }
end

#repoObject



420
421
422
# File 'lib/rdf/sak/transform.rb', line 420

def repo
  @harness.repo
end

#resolve(subject: nil, transform: nil, params: {}) ⇒ RDF::SAK::Transform::Partial

Resolves a partial either by subject or by transform + parameter set.

Parameters:

  • subject (RDF::URI) (defaults to: nil)

    The subject URI of the partial

  • transform (RDF::URI, RDF::SAK::Transform) (defaults to: nil)

    the transform

  • params (Hash) (defaults to: {})

    an instance of parameters

Returns:



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/rdf/sak/transform.rb', line 455

def resolve subject: nil, transform: nil, params: {}
  if subject
    if subject.is_a? RDF::SAK::Transform::Partial
      # snag the transform
      transform = @harness.resolve(subject.transform) or 
        raise 'Could not resolve the transform associated with ' + 
        subject.subject

      # mkay now add this to the cache
      t = @mapping[transform.subject] ||= {} # lol got all that?
      @cache[subject.subject] ||= t[subject.params] ||= subject
    else
      # resolve the partial
      partial = @cache[subject] || RDF::SAK::Transform::Partial.resolve(
        @harness, subject: subject) or return

      # initialize the mapping if not present
      t = @mapping[partial.transform.subject] ||= {}

      # off we go
      @cache[subject] ||= t[partial.params] ||= partial
    end
  elsif transform
    transform = @harness.resolve transform unless
      transform.is_a? RDF::SAK::Transform

    params = transform.validate params, defaults: false

    # note the *presence* of the key means the cache item has been
    # checked already; its *value* may be nil
    t = @mapping[transform.subject] ||= {}
    return t[params] if t.key? params

    # try to resolve the partial
    partial = RDF::SAK::Transform::Partial.resolve(@harness,
      transform: transform, params: params) or return

    # update the caches
    @cache[partial.subject] = t[params] = partial
  end
end

#transformsObject



424
425
426
# File 'lib/rdf/sak/transform.rb', line 424

def transforms
  @transforms.dup
end