Class: Wiris::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/src-generic/Storage.rb

Constant Summary collapse

TYPE_FILE =
0
TYPE_RESOURCE =
1
TYPE_URL =
2
@@resourcesDir =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location = nil) ⇒ Storage

Returns a new instance of Storage.



43
44
45
46
47
# File 'lib/src-generic/Storage.rb', line 43

def initialize(location=nil)
	if location != nil
		@location = location
	end
end

Class Method Details

.getResourcesDirObject



126
127
128
129
130
131
# File 'lib/src-generic/Storage.rb', line 126

def self.getResourcesDir()
	if @@resourcesDir.nil?
		setResourcesDir()
	end
	return @@resourcesDir
end

.newResourceStorage(name) ⇒ Object



74
75
76
77
78
# File 'lib/src-generic/Storage.rb', line 74

def self.newResourceStorage(name)
	s = Storage.new(File.join(getResourcesDir,name))
	s.type = TYPE_RESOURCE
	return s;
end

.newStorage(name) ⇒ Object



49
50
51
52
53
# File 'lib/src-generic/Storage.rb', line 49

def self.newStorage(name)
	s = Storage.new(name)
	s.type = TYPE_FILE
	return s
end

.newStorageWithParent(parent, name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/src-generic/Storage.rb', line 55

def self.newStorageWithParent(parent, name)
    s = Storage.new(parent.location)
    s.type = parent.type;
    if (parent.type == TYPE_FILE)
        # @file = File.new(File.join(parent.location, name), "w+")
        # @file.close
        s.location = File.join(parent.location, name)
    elsif (parent.type == TYPE_RESOURCE)
        s.resourceName = parent.resourceName;
        if (s.resourceName.length() > 0 && !s.resourceName.endsWith("/")) 
            s.resourceName += "/"
        end
        s.resourceName += name
    elsif (parent.type == TYPE_URL)
        url = Url.new(Url.new(parent.url),name).toExternalForm()
    end
    return s
end

.resourcesDirObject



39
40
41
# File 'lib/src-generic/Storage.rb', line 39

def self.resourcesDir
	@@resourcesDir
end

.resourcesDir=(resourcesDir) ⇒ Object



36
37
38
# File 'lib/src-generic/Storage.rb', line 36

def self.resourcesDir=(resourcesDir)
	@@resourcesDir = resourcesDir
end

.setResourcesDirObject



133
134
135
# File 'lib/src-generic/Storage.rb', line 133

def self.setResourcesDir()
	@@resourcesDir = File.dirname(__FILE__)
end

Instance Method Details

#existsObject



111
112
113
# File 'lib/src-generic/Storage.rb', line 111

def exists()
	return File.exists?(location)
end

#fileObject



18
19
20
# File 'lib/src-generic/Storage.rb', line 18

def file
	@file
end

#file=(file) ⇒ Object



15
16
17
# File 'lib/src-generic/Storage.rb', line 15

def file=(file)
	@file = file
end

#locationObject



28
29
30
# File 'lib/src-generic/Storage.rb', line 28

def location
	@location
end

#location=(location) ⇒ Object



32
33
34
# File 'lib/src-generic/Storage.rb', line 32

def location=(location)
	@location = location
end

#mkdirsObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/src-generic/Storage.rb', line 115

def mkdirs()
       if (@type == TYPE_RESOURCE)
           notImplemented()
       end
       if (@type == TYPE_URL)
           notImplemented()
       end        
       Dir.mkdir(@location)
       
end

#notImplementedObject

Raises:

  • (Exception)


137
138
139
# File 'lib/src-generic/Storage.rb', line 137

def notImplemented()
	 raise Exception,'Error: Operation not available on this Storage'
end

#readObject



80
81
82
# File 'lib/src-generic/Storage.rb', line 80

def read()
	return File.read(location)
end

#readBinaryObject



84
85
86
87
# File 'lib/src-generic/Storage.rb', line 84

def readBinary()		
	s = File.binread(location)
	return s.bytes.to_a
end

#resourceNameObject



21
22
23
# File 'lib/src-generic/Storage.rb', line 21

def resourceName
	@resourceName
end

#resourceName=(resourceName) ⇒ Object



24
25
26
# File 'lib/src-generic/Storage.rb', line 24

def resourceName=(resourceName)
	@resourceName = resourceName
end

#toStringObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/src-generic/Storage.rb', line 141

def toString()
	if type == TYPE_FILE
		return location.to_s
	elsif type == TYPE_RESOURCE
		return resourceName
	elsif type == TYPE_URL
		return url
	else
		return nil
	end
end

#typeObject



12
13
14
# File 'lib/src-generic/Storage.rb', line 12

def type
	@type
end

#type=(type) ⇒ Object



9
10
11
# File 'lib/src-generic/Storage.rb', line 9

def type=(type)
	@type = type
end

#write(str) ⇒ Object



89
90
91
# File 'lib/src-generic/Storage.rb', line 89

def write(str)
	writeOrAppend(str, false)
end

#writeBinary(bs) ⇒ Object



93
94
95
96
97
# File 'lib/src-generic/Storage.rb', line 93

def writeBinary(bs)
	File.open(location, 'wb' ) do |output|
 			output.write bs.pack("C*")
 		end
end

#writeOrAppend(str, append) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/src-generic/Storage.rb', line 99

def writeOrAppend(str, append)
	if (@type == TYPE_RESOURCE)
		notImplemented()
	end
	if (@type == TYPE_URL)
		notImplemented()
	end
	@file = File.new(@location, "w")
	@file.write(str)
	@file.close
end