Class: Redlander::ModelProxy
- Inherits:
-
Object
- Object
- Redlander::ModelProxy
- Includes:
- Enumerable
- Defined in:
- lib/redlander/model_proxy.rb
Overview
Proxy between model and its statements, allowing to scope actions on statements within a certain model.
Instance Method Summary collapse
-
#add(statement) ⇒ Boolean
(also: #<<)
Add a statement to the model.
-
#all(pattern = {}) ⇒ Array<Statement>
Find all statements matching the given criteria.
-
#create(source) ⇒ Statement?
Create a statement and add it to the model.
-
#delete(statement) ⇒ Boolean
Delete a statement from the model.
-
#delete_all(pattern = {}) ⇒ Boolean
Delete all statements from the model, matching the given pattern.
-
#each(*args) {|| ... } ⇒ void
Enumerate (and filter) model statements.
-
#empty? ⇒ Boolean
Checks whether there are no statements in the model.
-
#exist?(pattern) ⇒ Boolean
Checks the existence of statements in the model matching the given criteria.
-
#find(scope, pattern = {}) ⇒ Statement, ...
Find statements satisfying the given criteria.
-
#first(pattern = {}) ⇒ Statement?
Find a first statement matching the given criteria.
-
#initialize(model) ⇒ ModelProxy
constructor
A new instance of ModelProxy.
Constructor Details
#initialize(model) ⇒ ModelProxy
Returns a new instance of ModelProxy.
18 19 20 |
# File 'lib/redlander/model_proxy.rb', line 18 def initialize(model) @model = model end |
Instance Method Details
#add(statement) ⇒ Boolean Also known as: <<
All of subject, predicate, object nodes of the statement must be present. Only statements that are legal RDF can be added. If the statement already exists in the model, it is not added.
Add a statement to the model.
31 32 33 |
# File 'lib/redlander/model_proxy.rb', line 31 def add(statement) Redland.librdf_model_add_statement(@model.rdf_model, statement.rdf_statement).zero? end |
#all(pattern = {}) ⇒ Array<Statement>
Find all statements matching the given criteria. (Shortcut for #find(:all, pattern)).
152 153 154 |
# File 'lib/redlander/model_proxy.rb', line 152 def all(pattern = {}) find(:all, pattern) end |
#create(source) ⇒ Statement?
Create a statement and add it to the model.
66 67 68 69 |
# File 'lib/redlander/model_proxy.rb', line 66 def create(source) statement = Statement.new(source) add(statement) ? statement : nil end |
#delete(statement) ⇒ Boolean
All of subject, predicate, object nodes of the statement must be present.
Delete a statement from the model.
43 44 45 |
# File 'lib/redlander/model_proxy.rb', line 43 def delete(statement) Redland.librdf_model_remove_statement(@model.rdf_model, statement.rdf_statement).zero? end |
#delete_all(pattern = {}) ⇒ Boolean
Delete all statements from the model, matching the given pattern
52 53 54 55 56 |
# File 'lib/redlander/model_proxy.rb', line 52 def delete_all(pattern = {}) result = true each(pattern) { |st| result &&= delete(st) } result end |
#each(*args) {|| ... } ⇒ void
This method returns an undefined value.
Enumerate (and filter) model statements. If given no block, returns Enumerator.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/redlander/model_proxy.rb', line 95 def each(*args) if block_given? rdf_stream = if args.empty? Redland.librdf_model_as_stream(@model.rdf_model) else pattern = args.first.is_a?(Statement) ? args.first : Statement.new(args.first) Redland.librdf_model_find_statements(@model.rdf_model, pattern.rdf_statement) end raise RedlandError, "Failed to create a new stream" if rdf_stream.null? begin while Redland.librdf_stream_end(rdf_stream).zero? statement = Statement.new(Redland.librdf_stream_get_object(rdf_stream)) yield statement Redland.librdf_stream_next(rdf_stream) end ensure Redland.librdf_free_stream(rdf_stream) end else enum_for(:each, *args) end end |
#empty? ⇒ Boolean
Checks whether there are no statements in the model.
74 75 76 |
# File 'lib/redlander/model_proxy.rb', line 74 def empty? size.zero? end |
#exist?(pattern) ⇒ Boolean
Checks the existence of statements in the model matching the given criteria
83 84 85 |
# File 'lib/redlander/model_proxy.rb', line 83 def exist?(pattern) !first(pattern).nil? end |
#find(scope, pattern = {}) ⇒ Statement, ...
Find statements satisfying the given criteria.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/redlander/model_proxy.rb', line 127 def find(scope, pattern = {}) case scope when :first each(pattern).first when :all each(pattern).to_a else raise RedlandError, "Invalid search scope '#{scope}' specified." end end |