Module: Zena::Console
Class Method Summary
collapse
Instance Method Summary
collapse
-
#change_prop(pseudo_sql, key) ⇒ Object
Transform every value of a given property by using a block with |node, old_value| and returning the new value.
-
#count(pseudo_sql, opts = {}) ⇒ Object
-
#create(attrs) ⇒ Object
-
#err(obj) ⇒ Object
-
#field_to_prop(list, native_key, prop_key) ⇒ Object
-
#find(count_or_query, pseudo_sql = nil, opts = {}) ⇒ Object
Usage: find(‘contacts in site’) find(:all, ‘contacts in site’) # same as above find(:qb => ‘contacts in site’, :_find => :all) # same as above.
-
#foreach(pseudo_sql, opts = {}) ⇒ Object
-
#login(name, host = nil) ⇒ Object
-
#nodes(zip_or_name) ⇒ Object
-
#profile(node_id) ⇒ Object
-
#rename_prop(pseudo_sql, old_key, new_key) ⇒ Object
-
#set_prop(list, key, value) ⇒ Object
#secure_scope, #secure_write_scope, #visitor=
#construct_id_map, #secure_result
Class Method Details
.included(base) ⇒ Object
5
6
7
8
9
10
|
# File 'lib/zena/console.rb', line 5
def self.included(base)
if base == Object
raise %q{You should not include Zena::Console directly in Object. Use this instead: class << self; include Zena::Console; end
}
end
end
|
Instance Method Details
#change_prop(pseudo_sql, key) ⇒ Object
Transform every value of a given property by using a block with |node, old_value| and returning the new value.
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/zena/console.rb', line 74
def change_prop(pseudo_sql, key)
count = 0
unless block_given?
puts "You need to provide a block |node, old_value| and return the new value"
return
end
foreach(pseudo_sql) do |node|
node.versions.each do |v|
count += 1
prop = v.prop
val = prop[key]
new_val = yield(node, val)
unless new_val == val
if new_val
prop[key] = new_val
else
prop.delete(key)
end
Zena::Db.execute "UPDATE #{v.class.table_name} SET properties=#{Zena::Db.quote(v.class.encode_properties(prop))} WHERE id=#{v[:id]}"
end
end
end
"Changed '#{key}' prop in #{count} versions"
end
|
#count(pseudo_sql, opts = {}) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/zena/console.rb', line 155
def count(pseudo_sql, opts = {})
node = opts.delete(:node) || current_site.root_node
options = {:errors => true, :rubyless_helper => self, :default => {:scope => 'site'}}
count = node.find(:count, pseudo_sql, options)
if count.kind_of?(::QueryBuilder::Error)
puts "Error parsing query #{pseudo_sql.inspect} (#{count.message})"
return nil
else
return count
end
end
|
#err(obj) ⇒ Object
12
13
14
15
16
|
# File 'lib/zena/console.rb', line 12
def err(obj)
obj.errors.each_error do |er,msg|
puts "[#{er}] #{msg}"
end
end
|
#field_to_prop(list, native_key, prop_key) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/zena/console.rb', line 23
def field_to_prop(list, native_key, prop_key)
list = find(list) if list.kind_of?(String)
list.each do |rec|
next unless value = rec[native_key]
if rec.kind_of?(Node)
elems = rec.visible_versions
else
elems = [rec]
end
elems.each do |rec|
prop = rec.prop
prop[prop_key] = value
Zena::Db.execute "UPDATE #{rec.class.table_name} SET properties=#{Zena::Db.quote(rec.class.encode_properties(prop))} WHERE id=#{rec[:id]}"
end
end
end
|
#find(count_or_query, pseudo_sql = nil, opts = {}) ⇒ Object
Usage: find(‘contacts in site’) find(:all, ‘contacts in site’) # same as above find(:qb => ‘contacts in site’, :_find => :all) # same as above
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/zena/console.rb', line 134
def find(count_or_query, pseudo_sql = nil, opts = {})
if count_or_query.kind_of?(Hash)
query = count_or_query
elsif count_or_query.kind_of?(Fixnum)
query = {:qb => "node where id = #{count_or_query} in site", :_find => :first}
elsif pseudo_sql.nil?
query = {:qb => count_or_query}
else
query = {:qb => pseudo_sql, :_find => count_or_query}
end
query[:_find] ||=
count = Node.plural_relation?(query[:qb].split(' ').first) ? :all : :first
nodes = secure(Node) do
Node.search_records(query,
:node => current_site.root_node,
:default => {:scope => 'site'}
)
end
end
|
#foreach(pseudo_sql, opts = {}) ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/zena/console.rb', line 169
def foreach(pseudo_sql, opts = {})
limit = 100
return nil unless block_given?
node = opts.delete(:node) || current_site.root_node
options = {:errors => true, :rubyless_helper => self, :default => {:scope => 'site'}}
count = node.find(:count, pseudo_sql, options)
if count.kind_of?(::QueryBuilder::Error)
puts "Error parsing query #{pseudo_sql.inspect} (#{count.message})"
return nil
end
begin
query = Node.build_query(:all, pseudo_sql,
:node_name => 'self',
:main_class => VirtualClass['Node'],
:rubyless_helper => self,
:default => options[:default]
)
rescue ::QueryBuilder::Error => err
puts "Error parsing query #{pseudo_sql.inspect} (#{err.message})"
return nil
end
query.limit = " LIMIT #{limit}"
page_count = (count.to_f / limit).ceil
puts "#{count} nodes, #{page_count} chunk(s) (100 items)"
curr_page = 1
for curr_page in (1..page_count).to_a.reverse
query.offset = " OFFSET #{limit * (curr_page - 1)}"
if list = Node.do_find(:all, eval(query.to_s(:find)))
puts "Page #{curr_page}/#{page_count}"
list.each do |record|
yield(record)
end
end
end
nil
end
|
#login(name, host = nil) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/zena/console.rb', line 99
def login(name, host = nil)
finder = {}
finder[:conditions] = cond = [[]]
site = nil
if host
site = Site.find_by_host(host)
cond.first << 'site_id = ?'
cond << site.id
end
cond.first << 'users.login = ?'
cond << name.to_s
cond[0] = cond.first.join(' AND ')
if visitor = User.find(:first, finder)
setup_visitor(visitor, site || visitor.site)
puts "Logged #{visitor.login} in #{visitor.site.host}"
else
raise ActiveRecord::RecordNotFound
end
rescue ActiveRecord::RecordNotFound
puts "Could not login with user name: #{name}"
end
|
#nodes(zip_or_name) ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/zena/console.rb', line 122
def nodes(zip_or_name)
if zip_or_name.kind_of?(Fixnum)
secure(Node) { Node.find_by_zip(zip_or_name) }
else
secure(Node) { Node.find_by_title(zip_or_name) }
end
end
|
#profile(node_id) ⇒ Object
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/zena/console.rb', line 215
def profile(node_id)
require 'ruby-prof'
ctrl = NodesController.new
ctrl.request = Struct.new(:format).new(Mime::HTML)
ctrl.instance_variable_set(:@node, nodes(node_id))
start = Time.now
result = RubyProf.profile do
yield(ctrl)
end
puts(Time.now - start)
File.open('grind.log', 'wb') do |f|
RubyProf::CallTreePrinter.new(result).print(f)
end
end
|
#rename_prop(pseudo_sql, old_key, new_key) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/zena/console.rb', line 56
def rename_prop(pseudo_sql, old_key, new_key)
count = 0
foreach(pseudo_sql) do |node|
node.versions.each do |rec|
count += 1
prop = rec.prop
value = prop.delete(old_key)
if !value.blank?
prop[new_key] = value
Zena::Db.execute "UPDATE #{rec.class.table_name} SET properties=#{Zena::Db.quote(rec.class.encode_properties(prop))} WHERE id=#{rec[:id]}"
end
end
end
"Renamed '#{old_key}' to '#{new_key}' in #{count} versions"
end
|
#set_prop(list, key, value) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/zena/console.rb', line 40
def set_prop(list, key, value)
list = find(list) if list.kind_of?(String)
list.each do |rec|
if rec.kind_of?(Node)
elems = rec.visible_versions
else
elems = [rec]
end
elems.each do |rec|
prop = rec.prop
prop[key] = value
Zena::Db.execute "UPDATE #{rec.class.table_name} SET properties=#{Zena::Db.quote(rec.class.encode_properties(prop))} WHERE id=#{rec[:id]}"
end
end
end
|