Class: Redlander::ModelProxy

Inherits:
Object
  • Object
show all
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.

Examples:

model = Redlander::Model.new
model.statements
# => ModelProxy
model.statements.add(...)
model.statements.each(...)
model.statements.find(...)
# etc...

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ ModelProxy

Returns a new instance of ModelProxy.

Parameters:



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: <<

Note:

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.

Parameters:

Returns:

  • (Boolean)


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)).

Parameters:

Returns:



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.

Parameters:

  • source (Hash)

    subject, predicate and object nodes of the statement to be created (see Statement#initialize).

Options Hash (source):

  • :subject (Node, URI, String, nil)
  • :predicate (Node, URI, String, nil)
  • :object (Node, URI, String, nil)

Returns:



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

Note:

All of subject, predicate, object nodes of the statement must be present.

Delete a statement from the model.

Parameters:

Returns:

  • (Boolean)


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

Parameters:

Returns:

  • (Boolean)


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.

Parameters:

  • args (Statement, Hash, void)

    if given Statement or Hash, filter the model statements according to the specified pattern (see #find pattern).

Yield Parameters:



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.

Returns:

  • (Boolean)


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

Parameters:

Returns:

  • (Boolean)


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.

Parameters:

  • scope (:first, :all)

    find just one or all matches

  • pattern (Hash, Statement) (defaults to: {})

    matching pattern made of:

    • Hash with :subject, :predicate or :object nodes, or

    • “patternized” Statement (nil nodes are matching anything).

Returns:



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

#first(pattern = {}) ⇒ Statement?

Find a first statement matching the given criteria. (Shortcut for #find(:first, pattern)).

Parameters:

Returns:



143
144
145
# File 'lib/redlander/model_proxy.rb', line 143

def first(pattern = {})
  find(:first, pattern)
end