Class: ExistDB::Collection

Inherits:
Object
  • Object
show all
Extended by:
ClassWrappingForwardable
Defined in:
lib/existdb/collection.rb

Instance Method Summary collapse

Methods included from ClassWrappingForwardable

delegate_to_java

Constructor Details

#initialize(java_obj) ⇒ Collection

Returns a new instance of Collection.



7
8
9
# File 'lib/existdb/collection.rb', line 7

def initialize(java_obj)
    @obj = java_obj
end

Instance Method Details

#[](path) ⇒ Object

Supports relative paths including slashes e.g. ‘foo/bar/baz’ Wish I could figure out how to do absolute paths from here…



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/existdb/collection.rb', line 34

def [](path)
    (myconcern, remainder) = path.split('/', 2)
    if myconcern.nil? or myconcern == '' then
        return nil
    elsif myconcern == '..' then
        target = parent
    else
        target = collection(myconcern) || resource(myconcern)
    end

    if target.nil? then
        return nil
    elsif remainder then
        return target[remainder]
    else
        return target
    end
end

#child_collection_namesObject



83
84
85
# File 'lib/existdb/collection.rb', line 83

def child_collection_names
    @obj.getChildCollections.collect
end

#collection(name) ⇒ Object



17
18
19
# File 'lib/existdb/collection.rb', line 17

def collection(name)
    Collection.new( @obj.getChildCollection(name) ) if child_collection_names.include?(name)
end

#collectionsObject



11
12
13
14
15
# File 'lib/existdb/collection.rb', line 11

def collections
    child_collection_names.map{|name|
        collection(name)
    }
end

#create_collection(path) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/existdb/collection.rb', line 66

def create_collection(path)
    if path =~ /^\// then
        ClassWrap[ collection_manager.createCollection( path ) ]
    else
        ClassWrap[ collection_manager.createCollection( self.path + '/' + path ) ]
    end
end

#delete(resource = nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/existdb/collection.rb', line 53

def delete(resource = nil)
    if resource.nil? then
        collection_manager.removeCollection(@obj.uri)
    else
        @obj.removeResource( ClassUnwrap[ resource ] )
    end
    true
end

#inspectObject



79
80
81
# File 'lib/existdb/collection.rb', line 79

def inspect
    "#<#{self.class}:0x#{self.hash.to_s(16)} name=#{self.name.inspect}>"
end

#resource(name) ⇒ Object



27
28
29
# File 'lib/existdb/collection.rb', line 27

def resource(name)
    Resource.new( @obj.getResource(name) ) if resource_names.include?(name)
end

#resource_namesObject



87
88
89
# File 'lib/existdb/collection.rb', line 87

def resource_names
    @obj.getResources.collect
end

#resourcesObject



21
22
23
24
25
# File 'lib/existdb/collection.rb', line 21

def resources
    resource_names.map{ |name|
        resource(name)
    }
end

#store_url(url, name) ⇒ Object



74
75
76
77
# File 'lib/existdb/collection.rb', line 74

def store_url(url, name)
    url = url.to_s.gsub(/&(?!amp;)/, '&amp;')
    xquery.query("xmldb:store( '#{self.uri}', '#{name}', doc(#{url.inspect})  )")
end

#xqueryObject



62
63
64
# File 'lib/existdb/collection.rb', line 62

def xquery
    ClassWrap[ @obj.getService('XQueryService', '1.0') ]
end