Module: Kalimba::Persistence::Redlander::ClassMethods

Defined in:
lib/kalimba/persistence/redlander.rb

Instance Method Summary collapse

Instance Method Details

#count(attributes = {}) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/kalimba/persistence/redlander.rb', line 88

def count(attributes = {})
  q = "SELECT (COUNT(?subject) AS _count) WHERE { #{resource_definition} . #{attributes_to_graph_query(attributes.stringify_keys)} }"
  logger.debug(q) if logger

  # using SPARQL 1.1, because SPARQL 1.0 does not support COUNT
  c = Kalimba.repository.query(q, :language => "sparql")[0]
  c ? c["_count"].value : 0
end

#create(attributes = {}) ⇒ Object



73
74
75
76
77
# File 'lib/kalimba/persistence/redlander.rb', line 73

def create(attributes = {})
  record = new(attributes)
  record.save
  record
end

#destroy_allObject



79
80
81
82
83
84
85
86
# File 'lib/kalimba/persistence/redlander.rb', line 79

def destroy_all
  logger.debug("destroying all #{self.name.pluralize}") if logger
  Kalimba.repository.transaction do
    Kalimba.repository.statements.each(:predicate => NS::RDF["type"], :object => type) do |statement|
      Kalimba.repository.statements.delete_all(:subject => statement.subject)
    end
  end
end

#exist?(attributes = {}) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/kalimba/persistence/redlander.rb', line 66

def exist?(attributes = {})
  attributes = attributes.stringify_keys
  q = "ASK { #{resource_definition} . #{attributes_to_graph_query(attributes)} }"
  logger.debug(q) if logger
  Kalimba.repository.query(q)
end

#find_by_id(id_value) ⇒ Object



61
62
63
64
# File 'lib/kalimba/persistence/redlander.rb', line 61

def find_by_id(id_value)
  record = self.for(id_value)
  record.new_record? ? nil : record
end

#find_each(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kalimba/persistence/redlander.rb', line 44

def find_each(options = {})
  if block_given?
    attributes = (options[:conditions] || {}).stringify_keys

    q = "SELECT ?subject WHERE { #{resource_definition} . #{attributes_to_graph_query(attributes)} }"
    q << " LIMIT #{options[:limit]}" if options[:limit]

    logger.debug(q) if logger

    Kalimba.repository.query(q) do |binding|
      yield self.for(binding["subject"].uri.fragment)
    end
  else
    enum_for(:find_each, options)
  end
end