libisi 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +677 -0
- data/Manifest +89 -0
- data/Rakefile +34 -0
- data/lib/inifile.rb +119 -0
- data/lib/libisi.rb +948 -0
- data/lib/libisi/attribute.rb +32 -0
- data/lib/libisi/attribute/activerecord.rb +34 -0
- data/lib/libisi/attribute/base.rb +33 -0
- data/lib/libisi/base.rb +109 -0
- data/lib/libisi/bridge.rb +21 -0
- data/lib/libisi/bridge/base.rb +23 -0
- data/lib/libisi/bridge/java.rb +71 -0
- data/lib/libisi/bridge/python.rb +37 -0
- data/lib/libisi/cache.rb +21 -0
- data/lib/libisi/cache/base.rb +67 -0
- data/lib/libisi/cache/file_cache.rb +24 -0
- data/lib/libisi/chart.rb +21 -0
- data/lib/libisi/chart/base.rb +320 -0
- data/lib/libisi/chart/jfreechart.rb +682 -0
- data/lib/libisi/chart/jfreechart_generator.rb +206 -0
- data/lib/libisi/color.rb +21 -0
- data/lib/libisi/color/base.rb +66 -0
- data/lib/libisi/color/colortools.rb +92 -0
- data/lib/libisi/color/java.rb +44 -0
- data/lib/libisi/concept.rb +33 -0
- data/lib/libisi/concept/activerecord.rb +39 -0
- data/lib/libisi/concept/base.rb +58 -0
- data/lib/libisi/doc.rb +35 -0
- data/lib/libisi/doc/base.rb +414 -0
- data/lib/libisi/doc/html.rb +85 -0
- data/lib/libisi/doc/text.rb +98 -0
- data/lib/libisi/doc/wiki.rb +55 -0
- data/lib/libisi/environment.rb +21 -0
- data/lib/libisi/environment/base.rb +36 -0
- data/lib/libisi/environment/http.rb +105 -0
- data/lib/libisi/environment/rails.rb +27 -0
- data/lib/libisi/environment/root.rb +23 -0
- data/lib/libisi/fake_logger/logger.rb +61 -0
- data/lib/libisi/function/base.rb +30 -0
- data/lib/libisi/hal.rb +558 -0
- data/lib/libisi/instance.rb +27 -0
- data/lib/libisi/instance/activerecord.rb +21 -0
- data/lib/libisi/instance/base.rb +42 -0
- data/lib/libisi/log.rb +237 -0
- data/lib/libisi/mail/base.rb +32 -0
- data/lib/libisi/mail/tmail.rb +120 -0
- data/lib/libisi/parameter/base.rb +41 -0
- data/lib/libisi/property.rb +27 -0
- data/lib/libisi/property/base.rb +28 -0
- data/lib/libisi/reciever/base.rb +31 -0
- data/lib/libisi/reciever/socket.rb +31 -0
- data/lib/libisi/relation.rb +23 -0
- data/lib/libisi/request.rb +22 -0
- data/lib/libisi/request/base.rb +29 -0
- data/lib/libisi/request/http.rb +129 -0
- data/lib/libisi/response/base.rb +27 -0
- data/lib/libisi/task/base.rb +27 -0
- data/lib/libisi/task/http.rb +90 -0
- data/lib/libisi/tee.rb +296 -0
- data/lib/libisi/ui/base.rb +116 -0
- data/lib/libisi/ui/console.rb +238 -0
- data/lib/libisi/ui/kde.rb +94 -0
- data/lib/libisi/ui/nobody.rb +29 -0
- data/lib/libisi/ui/rails.rb +150 -0
- data/lib/libisi/ui/x11.rb +55 -0
- data/lib/libisi/uri.rb +42 -0
- data/lib/libisi/uri/activerecord.rb +152 -0
- data/lib/libisi/uri/base.rb +115 -0
- data/lib/libisi/uri/file.rb +43 -0
- data/lib/libisi/uri/ldap.rb +72 -0
- data/lib/libisi/uri/mysql.rb +98 -0
- data/lib/libisi/value.rb +31 -0
- data/lib/libisi/value/attribute_value.rb +19 -0
- data/lib/libisi/value/base.rb +55 -0
- data/lib/libisi/value/property_value.rb +19 -0
- data/lib/libisi/value/relation_value.rb +19 -0
- data/lib/ordered_hash.rb +228 -0
- data/libisi.gemspec +31 -0
- data/test/bridge_test.rb +77 -0
- data/test/cache_test.rb +65 -0
- data/test/chart_test.rb +179 -0
- data/test/color_test.rb +64 -0
- data/test/concept_test.rb +56 -0
- data/test/doc_test.rb +172 -0
- data/test/fixtures/test.db +0 -0
- data/test/ordered_hash_test.rb +39 -0
- data/test/profile_test.rb +36 -0
- data/test/request_test.rb +121 -0
- data/test/test +0 -0
- data/test/ui_test.rb +62 -0
- metadata +244 -0
@@ -0,0 +1,32 @@
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
2
|
+
#
|
3
|
+
# This file is part of Libisi.
|
4
|
+
#
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require "libisi/base.rb"
|
19
|
+
require "libisi/concept/base.rb"
|
20
|
+
require "libisi/attribute/base.rb"
|
21
|
+
class Attribute < Base
|
22
|
+
|
23
|
+
def self.create(concept, val, options = {})
|
24
|
+
if concept.class == ActiverecordConcept
|
25
|
+
require "libisi/attribute/activerecord.rb"
|
26
|
+
return ActiverecordAttribute.new(concept, val)
|
27
|
+
end
|
28
|
+
return BaseAttribute.new(concept,val)
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
2
|
+
#
|
3
|
+
# This file is part of Libisi.
|
4
|
+
#
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
class ActiverecordAttribute < BaseAttribute
|
19
|
+
|
20
|
+
def name; @activerecord_column.name; end
|
21
|
+
|
22
|
+
def initialize(concept, column, options = {})
|
23
|
+
@activerecord_column = column
|
24
|
+
super(concept, column.name, options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def type
|
28
|
+
@activerecord_column.type
|
29
|
+
end
|
30
|
+
def unit
|
31
|
+
return nil
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
2
|
+
#
|
3
|
+
# This file is part of Libisi.
|
4
|
+
#
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
class BaseAttribute
|
19
|
+
|
20
|
+
attr_reader :instance, :name
|
21
|
+
|
22
|
+
def initialize(concept, name, options = {})
|
23
|
+
@concept = concept
|
24
|
+
@name = name
|
25
|
+
end
|
26
|
+
|
27
|
+
def type
|
28
|
+
method = "#{name}_type"
|
29
|
+
return concept.send(method) if context.respond_to?(method)
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/lib/libisi/base.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
2
|
+
#
|
3
|
+
# This file is part of Libisi.
|
4
|
+
#
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require "libisi/tee"
|
19
|
+
class Base
|
20
|
+
|
21
|
+
def output_types; {}; end
|
22
|
+
def self.output_endings
|
23
|
+
output_types.map {|key, endings| endings}.flatten.sort
|
24
|
+
end
|
25
|
+
def self.type_from_ending(ending)
|
26
|
+
output_types.each {|t, endings|
|
27
|
+
return t if endings.include?(ending)
|
28
|
+
}
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.output(file)
|
33
|
+
$log.debug("Looking for outputter in #{self.name} for file #{file}")
|
34
|
+
return false unless (file.to_s =~ /\.([^\.]+)$/)
|
35
|
+
ending = $1
|
36
|
+
t = type_from_ending(ending)
|
37
|
+
$log.debug("Type for ending #{ending.inspect} is #{t.inspect}")
|
38
|
+
return false unless t
|
39
|
+
|
40
|
+
file = Pathname.new(file)
|
41
|
+
|
42
|
+
new_child = create_output(t, ending, file)
|
43
|
+
# we have to add this to the
|
44
|
+
# global variabel - if this is
|
45
|
+
# nil, the method handes itself
|
46
|
+
return true unless new_child
|
47
|
+
|
48
|
+
add_output(new_child)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.add_output(new_child)
|
52
|
+
curr = global_variable
|
53
|
+
$log.debug("Current #{self.name} is #{curr.class.name}")
|
54
|
+
case curr
|
55
|
+
when NilClass
|
56
|
+
$log.debug("Setting new child as only instance.")
|
57
|
+
curr = new_child
|
58
|
+
when Tee
|
59
|
+
$log.debug("Setting new child as tee to #{curr.children.length} other instance.")
|
60
|
+
curr.add_child(new_child)
|
61
|
+
else
|
62
|
+
$log.debug("Setting new child with new tee to another instance.")
|
63
|
+
curr = Tee.new([curr, new_child])
|
64
|
+
end
|
65
|
+
self.global_variable = curr
|
66
|
+
curr
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.create_output(type, ending, file)
|
70
|
+
$log.debug("Create new output of type #{type} to file #{file}")
|
71
|
+
create(type,:writer => file.open("w"))
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.init(options = {})
|
75
|
+
$log.debug("Initialize #{self.class.name}")
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.global_variable
|
79
|
+
eval("$#{self.name.downcase}")
|
80
|
+
end
|
81
|
+
def self.global_variable=(val)
|
82
|
+
eval("$#{self.name.downcase} = val")
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.change(type, options = {})
|
86
|
+
$log.debug("Change #{self.class.name} to type #{type} with options #{options.inspect}")
|
87
|
+
self.global_variable = create(type, options)
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.load(type_name, options = {})
|
91
|
+
type_name = type_name.to_s
|
92
|
+
raise "Hacking attack!!" unless type_name.class == String
|
93
|
+
raise "Unexpected #{self.name} name #{type_name}." unless type_name =~ /^[a-zA-Z][a-zA-Z0-9]*$/
|
94
|
+
require "libisi/#{self.name.downcase}/#{type_name}.rb"
|
95
|
+
eval("#{type_name.capitalize}#{self.name}")
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.create(type_name, options = {})
|
99
|
+
klass = load(type_name)
|
100
|
+
if klass.respond_to?(:instanciate)
|
101
|
+
ret = klass.instanciate(options)
|
102
|
+
raise "#{klass.name} created null object!" if ret.nil?
|
103
|
+
ret
|
104
|
+
else
|
105
|
+
klass.new(options)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
2
|
+
#
|
3
|
+
# This file is part of Libisi.
|
4
|
+
#
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require "libisi/base.rb"
|
19
|
+
class Bridge < Base
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
2
|
+
#
|
3
|
+
# This file is part of Libisi.
|
4
|
+
#
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
class BaseBridge
|
19
|
+
|
20
|
+
def import(library)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
2
|
+
#
|
3
|
+
# This file is part of Libisi.
|
4
|
+
#
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require "libisi/bridge/base"
|
19
|
+
|
20
|
+
raise "Java home is not set" unless
|
21
|
+
ENV["JAVA_HOME"]
|
22
|
+
|
23
|
+
ld_path = [ENV["LD_LIBRARY_PATH"],
|
24
|
+
Pathname.new("#{ENV["JAVA_HOME"]}/jre/lib/i386").realpath.to_s,
|
25
|
+
Pathname.new("#{ENV["JAVA_HOME"]}/jre/lib/i386/client").realpath.to_s].compact.join(":")
|
26
|
+
|
27
|
+
ENV["LD_LIBRARY_PATH"] = ld_path unless ENV["LD_LIBRARY_PATH"]
|
28
|
+
|
29
|
+
$log.debug("JAVA_HOME is #{ENV["JAVA_HOME"]}")
|
30
|
+
$log.debug("LD_LIBRARY_PATH is #{ENV["LD_LIBRARY_PATH"]}")
|
31
|
+
|
32
|
+
# maybe this must be executed to work
|
33
|
+
# ln -s /usr/lib/jvm/java-1.5.0-sun-1.5.0.14/jre/lib/i386/libmlib_image.so /usr/lib
|
34
|
+
|
35
|
+
require 'rjb'
|
36
|
+
|
37
|
+
class JavaBridge < BaseBridge
|
38
|
+
DEFAULT_LIBRARIES = [
|
39
|
+
"/usr/share/java/jcommon.jar",
|
40
|
+
ENV["LD_LIBRARY_PATH"]
|
41
|
+
]
|
42
|
+
|
43
|
+
def self.load(jar_files)
|
44
|
+
jar_files = [jar_files] if jar_files.class == String
|
45
|
+
Rjb::load(classpath = (DEFAULT_LIBRARIES + jar_files).join(":"), jvmargs=[])
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.import(class_name, options = {})
|
49
|
+
@classes ||= []
|
50
|
+
|
51
|
+
import_command = "Rjb::import('#{class_name}')"
|
52
|
+
|
53
|
+
new_class_name = "#{class_name.split(".")[-1]}"
|
54
|
+
if options[:prefix]
|
55
|
+
new_class_name = "#{options[:prefix]}#{new_class_name}"
|
56
|
+
end
|
57
|
+
|
58
|
+
if @classes.include?(new_class_name)
|
59
|
+
# $log.warn("#{new_class_name} already imported")
|
60
|
+
return eval("#{new_class_name}")
|
61
|
+
end
|
62
|
+
|
63
|
+
eval_string = "#{new_class_name} = #{import_command}"
|
64
|
+
|
65
|
+
$log.info("Loading java class by #{eval_string.inspect}")
|
66
|
+
res = eval(eval_string)
|
67
|
+
@classes << new_class_name
|
68
|
+
res
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
2
|
+
#
|
3
|
+
# This file is part of Libisi.
|
4
|
+
#
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require "libisi/bridge/base"
|
19
|
+
require 'rubypython'
|
20
|
+
|
21
|
+
# mh, python ruby needs this method
|
22
|
+
unless "x".respond_to?("end_with?")
|
23
|
+
class String
|
24
|
+
def end_with?(xx)
|
25
|
+
self[(-xx.length)..-1] == xx
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
RubyPython.start
|
31
|
+
|
32
|
+
class PythonBridge < BaseBridge
|
33
|
+
|
34
|
+
def self.import(klass)
|
35
|
+
RubyPython.import(klass)
|
36
|
+
end
|
37
|
+
end
|
data/lib/libisi/cache.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
2
|
+
#
|
3
|
+
# This file is part of Libisi.
|
4
|
+
#
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require "libisi/base.rb"
|
19
|
+
class Cache < Base
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# Copyright (C) 2007-2010 Logintas AG Switzerland
|
2
|
+
#
|
3
|
+
# This file is part of Libisi.
|
4
|
+
#
|
5
|
+
# Libisi is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Libisi is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Libisi. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
class BaseCache
|
19
|
+
|
20
|
+
attr_accessor :maxcount
|
21
|
+
def initialize(options = {})
|
22
|
+
@maxcount = (options[:maxcount] or 1000)
|
23
|
+
@cache = OrderedHash.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def fetch(key, options = {}, &block)
|
27
|
+
if has_key?(key)
|
28
|
+
return get(key)
|
29
|
+
else
|
30
|
+
raise "No block given" unless block
|
31
|
+
value = case block.arity
|
32
|
+
when 0
|
33
|
+
yield
|
34
|
+
when 1
|
35
|
+
yield(key)
|
36
|
+
when 2
|
37
|
+
yield(key, options)
|
38
|
+
else
|
39
|
+
raise "Unexpected arity count of fetch block #{block.arity}"
|
40
|
+
end
|
41
|
+
|
42
|
+
set(key,value)
|
43
|
+
value
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def get(key)
|
48
|
+
@cache[key]
|
49
|
+
end
|
50
|
+
|
51
|
+
def set(key, value)
|
52
|
+
while @cache.length >= maxcount
|
53
|
+
@cache.delete(@cache.first.keys[0])
|
54
|
+
end
|
55
|
+
|
56
|
+
@cache[key] = value
|
57
|
+
end
|
58
|
+
|
59
|
+
def delete(key)
|
60
|
+
@cache.delete(key)
|
61
|
+
end
|
62
|
+
|
63
|
+
def has_key?(key)
|
64
|
+
@cache.has_key?(key)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|