odrl-ruby 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/odrl/action.rb CHANGED
@@ -1,114 +1,106 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ODRL
4
-
5
- # ODRL::Action
6
- # Describes an action like "use"
7
- #
8
- # @author Mark D Wilkinson
9
- # @attr [URI] (optional) uid the URI of the Action node
10
- # @attr [[ODRL::Refinement]] (optional) ODRL Refinement objects
11
- # @attr [URI] predicate (optional) the predicate you wish to use with this action
12
- # @attr [string] value (required) a string like "use"
13
- # @attr [string] vallabel (optional) a string like "use"
14
- class Action < Base
15
-
16
- attr_accessor :uid, :refinements, :predicate, :type, :value, :vallabel
17
-
18
-
19
- # constructor
20
- # @param [Hash] opts the options to create a message with.
21
- # @option opts [String] :value the string value of rthe action, like "use"
22
- # @option opts [String] :vallabel the string for the label, like "use"
23
- #
24
- def initialize(value:, vallabel: "", type: CACTION, **args)
25
- @value = value
26
- @vallabel = vallabel || @value
27
- raise "Actions must haves a value such as 'use' - I'm dead!" unless @value
28
- @value = "http://www.w3.org/ns/odrl/2/#{@value}" unless @value =~ /http:\/\// # if it is already a URI, then let it go
29
-
30
- @uid = @value
31
- # unless @uid
32
- # self.uid = Base.baseURI + "#action_" + Base.getuuid
33
- # end
34
- super(uid: @uid, type: type, **args)
35
-
36
-
37
- @refinements = Hash.new
38
-
39
- args[:refinements] = [args[:refinements]] unless args[:refinements].is_a? Array
40
- if !(args[:refinements].first.nil?)
41
- args[:refinements].each do |c|
42
- self.addRefinement(refinement: c)
43
- end
44
- end
45
-
46
- self.predicate = PACTION unless self.predicate
47
-
4
+ # ODRL::Action
5
+ # Describes an action like "use"
6
+ #
7
+ # @author Mark D Wilkinson
8
+ # @attr [URI] (optional) uid the URI of the Action node
9
+ # @attr [[ODRL::Refinement]] (optional) ODRL Refinement objects
10
+ # @attr [URI] predicate (optional) the predicate you wish to use with this action
11
+ # @attr [string] value (required) a string like "use"
12
+ # @attr [string] vallabel (optional) a string like "use"
13
+ class Action < Base
14
+ attr_accessor :uid, :refinements, :predicate, :type, :value, :vallabel
15
+
16
+ # constructor
17
+ # @param [Hash] opts the options to create a message with.
18
+ # @option opts [String] :value the string value of rthe action, like "use"
19
+ # @option opts [String] :vallabel the string for the label, like "use"
20
+ #
21
+ def initialize(value:, vallabel: "", type: CACTION, **args)
22
+ @value = value
23
+ @vallabel = vallabel || @value
24
+ raise "Actions must haves a value such as 'use' - I'm dead!" unless @value
25
+
26
+ # if it is already a URI, then let it go
27
+ @value = "http://www.w3.org/ns/odrl/2/#{@value}" unless @value =~ %r{http://}
28
+
29
+ @uid = @value
30
+ # unless @uid
31
+ # self.uid = Base.baseURI + "#action_" + Base.getuuid
32
+ # end
33
+ super(uid: @uid, type: type, **args)
34
+
35
+ @refinements = {}
36
+
37
+ args[:refinements] = [args[:refinements]] unless args[:refinements].is_a? Array
38
+ unless args[:refinements].first.nil?
39
+ args[:refinements].each do |c|
40
+ addRefinement(refinement: c)
48
41
  end
42
+ end
49
43
 
50
- # Adds an ODRL Refinement
51
- #
52
- # @param refinement [ODRL::Refinement] the refinement to the action
53
- #
54
- def addRefinement(refinement: args)
55
- unless refinement.is_a?(Constraint)
56
- raise "Refinement is not an ODRL Constraint"
57
- else
58
- self.refinements[refinement.uid] = [PREFINEMENT, refinement]
59
- end
60
- end
44
+ self.predicate = PACTION unless predicate
45
+ end
61
46
 
47
+ # Adds an ODRL Refinement
48
+ #
49
+ # @param refinement [ODRL::Refinement] the refinement to the action
50
+ #
51
+ def addRefinement(refinement: args)
52
+ raise "Refinement is not an ODRL Constraint" unless refinement.is_a?(Constraint)
62
53
 
63
- # Causes the triples of this object to be formed in the in-memory store
64
- # This includes any "cascading" objects for which this is the subject of the triple
65
- #
66
- def load_graph
67
- super
68
- # TODO This is bad DRY!! Put the bulk of this method into the base object
69
- [:refinements].each do |connected_object_type|
70
- next unless self.send(connected_object_type)
71
- self.send(connected_object_type).each do |uid, typedconnection|
72
- predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
73
- object = odrlobject.uid
74
- subject = self.uid
75
- repo = self.repository
76
- triplify(subject, predicate, object, repo)
77
- odrlobject.load_graph # start the cascade
78
- end
79
- end
80
- subject = self.uid
81
- object = self.vallabel
82
- predicate = SCHEMA.name
83
- repo = self.repository
84
- triplify(subject, predicate, object, repo)
85
- object = self.vallabel
86
- predicate = RDFS.label
87
- repo = self.repository
88
- triplify(subject, predicate, object, repo)
89
- end
54
+ refinements[refinement.uid] = [PREFINEMENT, refinement]
55
+ end
90
56
 
91
- # Returns the serialized RDF for this object and cascading related objects
92
- #
93
- # @param format [Symbol] a valid RDF::Writer format (e.g. :turtle)
94
- #
95
- def serialize(format:)
96
- super
57
+ # Causes the triples of this object to be formed in the in-memory store
58
+ # This includes any "cascading" objects for which this is the subject of the triple
59
+ #
60
+ def load_graph
61
+ super
62
+ # TODO: This is bad DRY!! Put the bulk of this method into the base object
63
+ [:refinements].each do |connected_object_type|
64
+ next unless send(connected_object_type)
65
+
66
+ send(connected_object_type).each do |_uid, typedconnection|
67
+ predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
68
+ object = odrlobject.uid
69
+ subject = uid
70
+ repo = repository
71
+ triplify(subject, predicate, object, repo)
72
+ odrlobject.load_graph # start the cascade
97
73
  end
98
-
74
+ end
75
+ subject = uid
76
+ object = vallabel
77
+ predicate = SCHEMA.name
78
+ repo = repository
79
+ triplify(subject, predicate, object, repo)
80
+ object = vallabel
81
+ predicate = RDFS.label
82
+ repo = repository
83
+ triplify(subject, predicate, object, repo)
99
84
  end
100
85
 
101
-
102
-
103
- class Use < Action
104
- def initialize(type: CACTION, **args)
105
- super(type: :type, **args)
106
- end
86
+ # Returns the serialized RDF for this object and cascading related objects
87
+ #
88
+ # @param format [Symbol] a valid RDF::Writer format (e.g. :turtle)
89
+ #
90
+ def serialize(format:)
91
+ super
107
92
  end
108
- class Transfer < Action
109
- def initialize(type: CACTION, **args)
110
- super(type: :type, **args)
111
- end
93
+ end
94
+
95
+ class Use < Action
96
+ def initialize(type: CACTION, **args)
97
+ super(type: :type, **args)
112
98
  end
99
+ end
113
100
 
101
+ class Transfer < Action
102
+ def initialize(type: CACTION, **args)
103
+ super(type: :type, **args)
104
+ end
105
+ end
114
106
  end
data/lib/odrl/asset.rb CHANGED
@@ -1,90 +1,77 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ODRL
4
+ # ODRL::Action
5
+ # Describes an action like "use"
6
+ #
7
+ # @author Mark D Wilkinson
8
+ class Asset < Base
9
+ attr_accessor :uid, :hasPolicy, :refinements, :partOf
10
+
11
+ def initialize(type: CASSET, hasPolicy: nil, refinements: nil, partOf: nil, **args)
12
+ @uid = uid
13
+ self.uid = Base.baseURI + "#asset_" + Base.getuuid unless @uid
14
+ super(type: type, uid: @uid, **args)
15
+
16
+ @partOf = partOf
17
+ @hasPolicy = hasPolicy
18
+
19
+ if @hasPolicy and !(@hasPolicy.is_a? Policy) # if it exists and is the wrong type
20
+ raise "The policy of an Asset must be of type ODRL::Policy. The provided value will be discarded"
21
+ @hasPolicy = nil
22
+ end
23
+ if @partOf and !(@partOf.is_a? AssetCollection) # if it exists and is the wrong type
24
+ raise "The parent collection of an Asset must be of type ODRL::AssetCollection. The provided value will be discarded"
25
+ @partOf = nil
26
+ end
27
+
28
+ @refinements = {}
29
+ refinements = [refinements] unless refinements.is_a? Array
30
+ return if refinements.first.nil?
31
+
32
+ refinements.each do |c|
33
+ addRefinement(refinement: c)
34
+ end
35
+ end
4
36
 
37
+ def addPart(part: args)
38
+ raise "Asset cannot be added as part of something that is not an asset collection" unless is_a?(AssetCollection)
39
+ raise "Only Assets can be added as part of asset collections" unless part.is_a?(Asset)
5
40
 
6
- # ODRL::Action
7
- # Describes an action like "use"
8
- #
9
- # @author Mark D Wilkinson
10
- class Asset < Base
11
- attr_accessor :uid, :hasPolicy, :refinements, :partOf
12
-
13
- def initialize(type: CASSET, hasPolicy: nil, refinements: nil, partOf: nil, **args)
14
- @uid = uid
15
- unless @uid
16
- self.uid = Base.baseURI + "#asset_" + Base.getuuid
17
- end
18
- super(type: type, uid: @uid, **args)
19
-
20
- @partOf = partOf
21
- @hasPolicy = hasPolicy
22
-
23
- if @hasPolicy and !(@hasPolicy.is_a? Policy) # if it exists and is the wrong type
24
- raise "The policy of an Asset must be of type ODRL::Policy. The provided value will be discarded"
25
- @hasPolicy = nil
26
- end
27
- if @partOf and !(@partOf.is_a? AssetCollection) # if it exists and is the wrong type
28
- raise "The parent collection of an Asset must be of type ODRL::AssetCollection. The provided value will be discarded"
29
- @partOf = nil
30
- end
31
-
32
- @refinements = Hash.new
33
- refinements = [refinements] unless refinements.is_a? Array
34
- if !(refinements.first.nil?)
35
- refinements.each do |c|
36
- self.addRefinement(refinement: c)
37
- end
38
- end
39
- end
40
-
41
-
42
- def addPart(part: args)
43
- unless self.is_a?(AssetCollection)
44
- raise "Asset cannot be added as part of something that is not an asset collection"
45
- end
46
- unless part.is_a?(Asset)
47
- raise "Only Assets can be added as part of asset collections"
48
- end
49
- part.partOf[self.uid] = [PPARTOF, self]
50
- end
51
-
52
- def addRefinement(refinement: args)
53
- unless refinement.is_a?(Constraint)
54
- raise "Refinement is not an ODRL Constraint"
55
- else
56
- self.refinements[refinement.uid] = [PREFINEMENT, refinement]
57
- end
58
- end
59
-
60
- def load_graph
61
- super
62
- # TODO This is bad DRY!! Put the bulk of this method into the base object
63
- [:refinements, :partOf, :hasPolicy].each do |connected_object_type|
64
- next unless self.send(connected_object_type)
65
- self.send(connected_object_type).each do |uid, typedconnection|
66
- predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
67
- object = odrlobject.uid
68
- subject = self.uid
69
- repo = self.repository
70
- triplify(subject, predicate, object, repo)
71
- odrlobject.load_graph # start the cascade
72
- end
73
- end
74
- end
41
+ part.partOf[uid] = [PPARTOF, self]
42
+ end
75
43
 
76
- def serialize(format:)
77
- super
78
- end
44
+ def addRefinement(refinement: args)
45
+ raise "Refinement is not an ODRL Constraint" unless refinement.is_a?(Constraint)
79
46
 
47
+ refinements[refinement.uid] = [PREFINEMENT, refinement]
80
48
  end
81
49
 
82
- class AssetCollection < Asset
83
-
84
- def initialize(type: CASSETCOLLECTION, **args)
85
- super(type: type, **args)
50
+ def load_graph
51
+ super
52
+ # TODO: This is bad DRY!! Put the bulk of this method into the base object
53
+ %i[refinements partOf hasPolicy].each do |connected_object_type|
54
+ next unless send(connected_object_type)
55
+
56
+ send(connected_object_type).each do |_uid, typedconnection|
57
+ predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
58
+ object = odrlobject.uid
59
+ subject = uid
60
+ repo = repository
61
+ triplify(subject, predicate, object, repo)
62
+ odrlobject.load_graph # start the cascade
86
63
  end
64
+ end
87
65
  end
88
66
 
67
+ def serialize(format:)
68
+ super
69
+ end
70
+ end
89
71
 
72
+ class AssetCollection < Asset
73
+ def initialize(type: CASSETCOLLECTION, **args)
74
+ super(type: type, **args)
75
+ end
76
+ end
90
77
  end