knjrbfw 0.0.111 → 0.0.113

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,153 +0,0 @@
1
- require "#{$knjpath}event_handler"
2
-
3
- class Knj::Datarow_custom
4
- #Used to determine if this is a knj-datarow-object.
5
- def is_knj?
6
- return true
7
- end
8
-
9
- #Initializes variables on the class from objects.
10
- def self.datarow_init(d)
11
- @@ob = d.ob
12
- @@db = d.db
13
- end
14
-
15
- def self.has_one(arr)
16
- arr.each do |val|
17
- methodname = nil
18
- colname = nil
19
- classname = nil
20
-
21
- if val.is_a?(Symbol)
22
- classname = val
23
- methodname = val.to_s.downcase.to_sym
24
- colname = "#{val.to_s.downcase}_id".to_sym
25
- elsif val.is_a?(Array)
26
- classname, colname, methodname = *val
27
- elsif val.is_a?(Hash)
28
- classname, colname, methodname = val[:class], val[:col], val[:method]
29
- else
30
- raise "Unknown argument-type: '#{arr.class.name}'."
31
- end
32
-
33
- methodname = classname.to_s.downcase if !methodname
34
- colname = "#{classname.to_s.downcase}_id".to_sym if !colname
35
-
36
- define_method(methodname) do
37
- return @@ob.get_try(self, colname, classname)
38
- end
39
-
40
- methodname_html = "#{methodname.to_s}_html".to_sym
41
- define_method(methodname_html) do |*args|
42
- obj = self.send(methodname)
43
- return @@ob.events.call(:no_html, classname) if !obj
44
-
45
- raise "Class '#{classname}' does not have a 'html'-method." if !obj.respond_to?(:html)
46
- return obj.html(*args)
47
- end
48
- end
49
- end
50
-
51
- def self.events
52
- if !@events
53
- @events = Knj::Event_handler.new
54
- @events.add_event(:name => :add, :connections_max => 1)
55
- @events.add_event(:name => :update, :connections_max => 1)
56
- @events.add_event(:name => :data_from_id, :connections_max => 1)
57
- @events.add_event(:name => :delete, :connections_max => 1)
58
- end
59
-
60
- return @events
61
- end
62
-
63
- def self.classname
64
- self.name.split("::").last
65
- end
66
-
67
- def self.add(d)
68
- return @events.call(:add, d)
69
- end
70
-
71
- def self.table
72
- return self.name.split("::").last
73
- end
74
-
75
- def deleted?
76
- return true if !@data
77
- return false
78
- end
79
-
80
- def table
81
- return self.class.table
82
- end
83
-
84
- def initialize(data, args)
85
- if data.is_a?(Hash)
86
- @data = Knj::ArrayExt.hash_sym(data)
87
- @id = self.id
88
- else
89
- @id = data
90
- self.reload
91
- end
92
- end
93
-
94
- def reload
95
- raise "No 'data_from_id'-event connected to class." if !self.class.events.connected?(:data_from_id)
96
- data = self.class.events.call(:data_from_id, Knj::Hash_methods.new(:id => @id))
97
- raise "No data was received from the event: 'data_from_id'." if !data
98
- raise "Data expected to be a hash but wasnt: '#{data.class.name}'." if !data.is_a?(Hash)
99
- @data = Knj::ArrayExt.hash_sym(data)
100
- end
101
-
102
- def update(data)
103
- ret = self.class.events.call(:update, Knj::Hash_methods.new(:object => self, :data => data))
104
- self.reload
105
- return ret
106
- end
107
-
108
- #Returns a key from the hash that this object is holding or raises an error if it doesnt exist.
109
- def [](key)
110
- raise "No data spawned on object." if !@data
111
- raise "No such key: '#{key}'. Available keys are: '#{@data.keys.sort.join(", ")}'." if !@data.key?(key)
112
- return @data[key]
113
- end
114
-
115
- #Returns the ID of the object.
116
- def id
117
- return self[:id]
118
- end
119
-
120
- #Returns the name of the object, which can be taken from various data or various defined methods.
121
- def name
122
- if @data.key?(:title)
123
- return @data[:title]
124
- elsif @data.key?(:name)
125
- return @data[:name]
126
- end
127
-
128
- obj_methods = self.class.instance_methods(false)
129
- [:name, :title].each do |method_name|
130
- return self.method(method_name).call if obj_methods.index(method_name)
131
- end
132
-
133
- raise "Couldnt figure out the title/name of the object on class #{self.class.name}."
134
- end
135
-
136
- alias :title :name
137
-
138
- def delete
139
- self.class.events.call(:delete, Knj::Hash_methods.new(:object => self))
140
- end
141
-
142
- def destroy
143
- @data = nil
144
- end
145
-
146
- def each(&args)
147
- return @data.each(&args)
148
- end
149
-
150
- def to_hash
151
- return @data.clone
152
- end
153
- end
@@ -1,144 +0,0 @@
1
- #!/usr/bin/env ruby1.9.1
2
-
3
- Dir.chdir(File.dirname(__FILE__))
4
- require "../../knjrbfw"
5
- require "knj/process"
6
- require "knj/process_meta"
7
- require "knj/strings"
8
-
9
- objects = {}
10
- @process = Knj::Process.new(
11
- :out => $stdout,
12
- :in => $stdin,
13
- :listen => true,
14
- :debug => false,
15
- :on_rec => proc{|d, &block|
16
- obj = d.obj
17
- block_res = nil
18
-
19
- if obj.is_a?(Hash) and obj.key?("args")
20
- obj["args"] = Knj::Process_meta.args_parse_back(obj["args"], objects)
21
- end
22
-
23
- if obj.is_a?(Hash)
24
- if obj["type"] == "spawn_object"
25
- raise "An object by that name already exists: '#{obj["var_name"]}'." if objects.key?(obj["var_name"])
26
-
27
- #Fix new integer.
28
- if obj["class_name"].to_s == "Integer" or obj["class_name"].to_s == "Fixnum"
29
- objects[obj["var_name"]] = obj["args"].first.to_i
30
- elsif obj["class_name"].to_s == "{}" and obj["args"].first.is_a?(Hash)
31
- objects[obj["var_name"]] = obj["args"].first
32
- else
33
- class_obj = Knj::Strings.const_get_full(obj["class_name"])
34
- objects[obj["var_name"]] = class_obj.new(*obj["args"])
35
- end
36
-
37
- d.answer("type" => "success")
38
- elsif obj["type"] == "proxy_from_call"
39
- raise "No 'var_name' was given in arguments." if !obj["var_name"]
40
- raise "No object by that name when trying to do 'proxy_from_call': '#{obj["proxy_obj"]}' in '#{objects}'." if !objects.key?(obj["proxy_obj"])
41
- obj_to_call = objects[obj["proxy_obj"]]
42
- res = obj_to_call.__send__(obj["method_name"], *obj["args"])
43
- objects[obj["var_name"]] = res
44
-
45
- d.answer("type" => "success")
46
- elsif obj["type"] == "proxy_from_eval"
47
- res = eval(obj["str"])
48
- objects[obj["var_name"]] = res
49
- d.answer("type" => "success")
50
- elsif obj["type"] == "proxy_from_static"
51
- const = Knj::Strings.const_get_full(obj["const"])
52
- res = const.__send__(obj["method_name"], *obj["args"])
53
- objects[obj["var_name"]] = res
54
- d.answer("type" => "success")
55
- elsif obj["type"] == "call_object"
56
- raise "Invalid var-name: '#{obj["var_name"]}'." if obj["var_name"].to_s.strip.length <= 0
57
-
58
- obj_to_call = objects[obj["var_name"]]
59
- raise "No object by that name: '#{obj["var_name"]}'." if !obj
60
- res = obj_to_call.__send__(obj["method_name"], *obj["args"])
61
- res = nil if obj["capture_return"] == false
62
- d.answer("type" => "call_object_success", "result" => res)
63
- elsif obj["type"] == "call_object_buffered"
64
- raise "Invalid var-name: '#{obj["var_name"]}'." if obj["var_name"].to_s.strip.length <= 0
65
-
66
- obj_to_call = objects[obj["var_name"]]
67
- raise "No object by that name: '#{obj["var_name"]}'." if !obj
68
- capt_return = obj["capture_return"]
69
-
70
- if capt_return != false
71
- ret = []
72
- else
73
- ret = nil
74
- end
75
-
76
- obj["args"].each do |args|
77
- if capt_return != false
78
- ret << obj_to_call.__send__(obj["method_name"], *args)
79
- else
80
- obj_to_call.__send__(obj["method_name"], *args)
81
- end
82
- end
83
-
84
- d.answer("type" => "call_object_success", "result" => res)
85
- elsif obj["type"] == "call_object_block"
86
- raise "Invalid var-name: '#{obj["var_name"]}'." if obj["var_name"].to_s.strip.length <= 0
87
- res = nil
88
-
89
- begin
90
- raise Errno::ENOENT, "No object by that name: '#{obj["var_name"]}' in '#{objects}'." if !objects.key?(obj["var_name"])
91
- obj_to_call = objects[obj["var_name"]]
92
- raise "No object by that name: '#{obj["var_name"]}'." if !obj
93
-
94
- res = obj_to_call.__send__(obj["method_name"], *obj["args"]) do |*args|
95
- block_res = block.call(*args) if block
96
- end
97
- ensure
98
- #This has to be ensured, because this block wont be runned any more after enumerable has been broken...
99
- res = nil if obj["capture_return"] == false
100
- d.answer("type" => "call_object_success", "result" => res)
101
- end
102
- elsif obj["type"] == "unset"
103
- raise "Invalid var-name: '#{obj["var_name"]}'." if obj["var_name"].to_s.strip.length <= 0
104
- raise Errno::ENOENT, "Var-name didnt exist when trying to unset: '#{obj["var_name"]}'." if !objects.key?(obj["var_name"])
105
- objects.delete(obj["var_name"])
106
- d.answer("type" => "unset_success")
107
- elsif obj["type"] == "unset_multiple"
108
- err = nil
109
- obj["var_names"].each do |var_name|
110
- err = [ArgumentError, "Invalid var-name: '#{var_name}'."] if var_name.to_s.strip.length <= 0
111
- err = [Errno::ENOENT, "Var-name didnt exist when trying to unset: '#{var_name}'."] if !objects.key?(var_name)
112
- objects.delete(var_name)
113
- end
114
-
115
- raise err[0], err[1] if err
116
- d.answer("type" => "unset_success")
117
- elsif obj["type"] == "static"
118
- const = Knj::Strings.const_get_full(obj["const"])
119
- res = const.__send__(obj["method_name"], *obj["args"], &block)
120
- res = nil if obj["capture_return"] == false
121
- d.answer("type" => "call_const_success", "result" => res)
122
- elsif obj["type"] == "str_eval"
123
- res = eval(obj["str"])
124
- d.answer("type" => "call_eval_success", "result" => res)
125
- elsif obj["type"] == "exit"
126
- d.answer("type" => "exit_success")
127
- sleep 0.1
128
- @process.destroy
129
- exit
130
- elsif obj["type"] == "process_data"
131
- d.answer("type" => "process_data_success", "pid" => Process.pid)
132
- else
133
- raise "Didnt know how to handle hash: '#{Php4r.print_r(obj, true)}'."
134
- end
135
- else
136
- raise "Unknown object: '#{obj.class.name}'."
137
- end
138
-
139
- block_res
140
- }
141
- )
142
-
143
- print "process_meta_started\n"
144
- @process.join
@@ -1,133 +0,0 @@
1
- class Knj::Translations
2
- attr_accessor :args, :db, :ob, :cache
3
-
4
- def initialize(args)
5
- @args = args
6
-
7
- raise "No DB given." if !@args[:db]
8
- @db = @args[:db]
9
-
10
- @ob = Knj::Objects.new(
11
- :db => @args[:db],
12
- :extra_args => [self],
13
- :class_path => File.dirname(__FILE__),
14
- :module => Knj::Translations,
15
- :require => false,
16
- :datarow => true
17
- )
18
- end
19
-
20
- #Returns the translated value for an object by the given key.
21
- def get(obj, key, args = {})
22
- return "" if !obj
23
-
24
- if args[:locale]
25
- locale = args[:locale].to_sym
26
- else
27
- locale = @args[:locale].to_sym
28
- end
29
-
30
- #Force to symbol to save memory when caching.
31
- key = key.to_sym
32
-
33
- #Set-get the cache-hash for the object.
34
- if !obj.instance_variable_defined?("@knj_translations_cache")
35
- obj.instance_variable_set("@knj_translations_cache", {})
36
- end
37
-
38
- cache = obj.instance_variable_get("@knj_translations_cache")
39
-
40
- #Return from cache if set.
41
- if cache.key?(key) and cache[key].key?(locale)
42
- return cache[key][locale]
43
- end
44
-
45
- trans = @ob.list(:Translation, {
46
- "object_class" => obj.class.name,
47
- "object_id" => obj.id,
48
- "key" => key,
49
- "locale" => locale
50
- })
51
-
52
- if trans.empty?
53
- print "Nothing found - returning empty string.\n" if @args[:debug] or args[:debug]
54
- return ""
55
- end
56
-
57
- trans.each do |tran|
58
- if !cache[key]
59
- cache[key] = {
60
- locale => tran[:value]
61
- }
62
- elsif !cache[key][locale]
63
- cache[key][locale] = tran[:value]
64
- end
65
- end
66
-
67
- return cache[key][locale]
68
- end
69
-
70
- #Sets translations for an object by the given hash-keys and hash-values.
71
- def set(obj, values, args = {})
72
- #Reset cache to reflect the updates when read next time.
73
- obj.instance_variable_set("@knj_translations_cache", {})
74
-
75
- if args[:locale]
76
- locale = args[:locale]
77
- else
78
- locale = @args[:locale]
79
- end
80
-
81
- values.each do |key, val|
82
- trans = @ob.get_by(:Translation, {
83
- "object_id" => obj.id,
84
- "object_class" => obj.class.name,
85
- "key" => key,
86
- "locale" => locale
87
- })
88
-
89
- if trans
90
- trans.update(:value => val)
91
- else
92
- @ob.add(:Translation, {
93
- :object => obj,
94
- :key => key,
95
- :locale => locale,
96
- :value => val
97
- })
98
- end
99
- end
100
- end
101
-
102
- #Deletes all translations for a given object.
103
- def delete(obj)
104
- classn = obj.class.name
105
- objid = obj.id.to_s
106
-
107
- if obj.instance_variable_defined?("@knj_translations_cache")
108
- cache = obj.instance_variable_get("@knj_translations_cache")
109
- end
110
-
111
- trans = @ob.list(:Translation, {
112
- "object_id" => obj.id,
113
- "object_class" => obj.class.name
114
- })
115
- trans.each do |tran|
116
- #Delete the cache if defined on the object.
117
- cache.delete(tran[:key].to_sym) if cache and cache.key?(tran[:key].to_sym)
118
-
119
- #Delete the translation object.
120
- @ob.delete(tran)
121
- end
122
- end
123
- end
124
-
125
- class Knj::Translations::Translation < Knj::Datarow
126
- def self.add(d)
127
- if d.data[:object]
128
- d.data[:object_class] = d.data[:object].class.name
129
- d.data[:object_id] = d.data[:object].id
130
- d.data.delete(:object)
131
- end
132
- end
133
- end