Class: ODRL::Asset

Inherits:
Base
  • Object
show all
Defined in:
lib/odrl/asset.rb

Overview

ODRL::Action Describes an action like “use”

Author:

  • Mark D Wilkinson

Direct Known Subclasses

AssetCollection

Instance Attribute Summary collapse

Attributes inherited from Base

#baseURI, #creator, #description, #id, #issued, #label, #subject, #title, #type

Instance Method Summary collapse

Methods inherited from Base

baseURI, clear_repository, #get_writer, getuuid, repository, #repository, #triplify

Constructor Details

#initialize(type: CASSET, hasPolicy: nil, refinements: nil, partOf: nil, **args) ⇒ Asset

Returns a new instance of Asset.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/odrl/asset.rb', line 11

def initialize(type: CASSET, hasPolicy: nil, refinements: nil, partOf: nil, **args)
  @uid = uid
  self.uid = Base.baseURI + "#asset_" + Base.getuuid unless @uid
  super(type: type, uid: @uid, **args)

  @partOf = partOf
  @hasPolicy = hasPolicy

  if @hasPolicy and !(@hasPolicy.is_a? Policy) # if it exists and is the wrong type
    raise "The policy of an Asset must be of type ODRL::Policy.  The provided value will be discarded"
    @hasPolicy = nil
  end
  if @partOf and !(@partOf.is_a? AssetCollection) # if it exists and is the wrong type
    raise "The parent collection of an Asset must be of type ODRL::AssetCollection.  The provided value will be discarded"
    @partOf = nil
  end

  @refinements = {}
  refinements = [refinements] unless refinements.is_a? Array
  return if refinements.first.nil?

  refinements.each do |c|
    addRefinement(refinement: c)
  end
end

Instance Attribute Details

#hasPolicyObject

Returns the value of attribute hasPolicy.



9
10
11
# File 'lib/odrl/asset.rb', line 9

def hasPolicy
  @hasPolicy
end

#partOfObject

Returns the value of attribute partOf.



9
10
11
# File 'lib/odrl/asset.rb', line 9

def partOf
  @partOf
end

#refinementsObject

Returns the value of attribute refinements.



9
10
11
# File 'lib/odrl/asset.rb', line 9

def refinements
  @refinements
end

#uidObject

Returns the value of attribute uid.



9
10
11
# File 'lib/odrl/asset.rb', line 9

def uid
  @uid
end

Instance Method Details

#addPart(part: args) ⇒ Object



37
38
39
40
41
42
# File 'lib/odrl/asset.rb', line 37

def addPart(part: args)
  raise "Asset cannot be added as part of something that is not an asset collection" unless is_a?(AssetCollection)
  raise "Only Assets can be added as part of asset collections" unless part.is_a?(Asset)

  part.partOf[uid] = [PPARTOF, self]
end

#addRefinement(refinement: args) ⇒ Object



44
45
46
47
48
# File 'lib/odrl/asset.rb', line 44

def addRefinement(refinement: args)
  raise "Refinement is not an ODRL Constraint" unless refinement.is_a?(Constraint)

  refinements[refinement.uid] = [PREFINEMENT, refinement]
end

#load_graphObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/odrl/asset.rb', line 50

def load_graph
  super
  # TODO: This is bad DRY!!  Put the bulk of this method into the base object
  %i[refinements partOf hasPolicy].each do |connected_object_type|
    next unless send(connected_object_type)

    send(connected_object_type).each do |_uid, typedconnection|
      predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
      object = odrlobject.uid
      subject = uid
      repo = repository
      triplify(subject, predicate, object, repo)
      odrlobject.load_graph  # start the cascade
    end
  end
end

#serialize(format:) ⇒ Object



67
68
69
# File 'lib/odrl/asset.rb', line 67

def serialize(format:)
  super
end