Skip to content

Commit

Permalink
fix standardrb complaints (then fix fixed code)
Browse files Browse the repository at this point in the history
  • Loading branch information
oldmoe committed Jun 8, 2024
1 parent 5320d68 commit 76a3bcd
Show file tree
Hide file tree
Showing 22 changed files with 134 additions and 150 deletions.
2 changes: 1 addition & 1 deletion lib/active_job/queue_adapters/litejob_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def enqueue(job) # :nodoc:
end

def enqueue_at(job, time) # :nodoc:
time = time.from_now if time.respond_to?(:from_now) #is_a?(ActiveSupport::Duration)
time = time.from_now if time.respond_to?(:from_now) # is_a?(ActiveSupport::Duration)
Job.queue = job.queue_name
Job.perform_at(time, job.serialize)
end
Expand Down
12 changes: 6 additions & 6 deletions lib/active_record/connection_adapters/litedb_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ module DatabaseTasks
end
end

ActiveRecord::ConnectionAdapters.register(
"litedb", "ActiveRecord::ConnectionAdapters::LitedbAdapter",
"active_record/connection_adapters/litedb_adapter"
) if ActiveRecord::ConnectionAdapters.respond_to?(:register)


if ActiveRecord::ConnectionAdapters.respond_to?(:register)
ActiveRecord::ConnectionAdapters.register(
"litedb", "ActiveRecord::ConnectionAdapters::LitedbAdapter",
"active_record/connection_adapters/litedb_adapter"
)
end
1 change: 0 additions & 1 deletion lib/active_support/cache/litecache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

module ActiveSupport
module Cache

self.format_version = 7.0

class Litecache < Store
Expand Down
7 changes: 3 additions & 4 deletions lib/litestack/liteboard/liteboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Liteboard
@@res_mapping = {"hour" => "minute", "day" => "hour", "week" => "day", "year" => "week"}
@@templates = {}
@@app = proc do |env|
case path = env["PATH_INFO"]
case env["PATH_INFO"]
when "/"
Liteboard.new(env).call(:index)
when "/topics/Litejob"
Expand All @@ -24,13 +24,12 @@ class Liteboard
when "/topics/Litecable"
Liteboard.new(env).call(:litecable)
end

end

def initialize(env)
@env = env
@req = Rack::Request.new(@env)
@params = @req.params
@params = @req.params
@running = true
@lm = Litemetric.instance
end
Expand Down Expand Up @@ -224,7 +223,7 @@ def litejob
@events.each do |event|
data_points = @lm.event_data_points(@step, @count, @resolution, @topic, event[:name])
event["counts"] = data_points.collect { |r| [r["rtime"], r["rcount"] || 0] }
event["values"] = data_points.collect { |r| [r["rtime"], (r["rtotal"] || 0.0)] }
event["values"] = data_points.collect { |r| [r["rtime"], r["rtotal"] || 0.0] }
end
@snapshot = read_snapshot(@topic)
@size = begin
Expand Down
4 changes: 2 additions & 2 deletions lib/litestack/litecache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def set_unless_exists(key, value, expires_in = nil)
# if the key doesn't exist or it is expired then null will be returned
def get(key)
key = key.to_s
if (record = run_stmt(:getter, key)[0])
if (record = run_stmt(:getter, key)[0])
capture(:get, key, 1)
return record[1]
end
Expand Down Expand Up @@ -223,7 +223,7 @@ def snapshot
}
}
end

private

def setup
Expand Down
20 changes: 8 additions & 12 deletions lib/litestack/liteconnection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def configure(options = {})
end
@options = defaults.merge(options)
config = begin
YAML.load(ERB.new(File.read(@options[:config_path])).result)
YAML.safe_load(ERB.new(File.read(@options[:config_path])).result)
rescue
{}
end # an empty hash won't hurt
Expand Down Expand Up @@ -122,24 +122,20 @@ def acquire_connection
if @checked_out_conn
yield @checked_out_conn
else
@conn.acquire{ |conn| yield conn }
@conn.acquire { |conn| yield conn }
end
end

# this will force the other run_* methods to use the
# this will force the other run_* methods to use the
# checked out connection if one exists
def with_connection
@conn.acquire do |conn|
begin
@checked_out_conn = conn
yield conn
ensure
@checked_out_conn = nil
end
@conn.acquire do |conn|
@checked_out_conn = conn
yield conn
ensure
@checked_out_conn = nil
end
end



def create_pooled_connection(count = 1)
count = 1 unless count&.is_a?(Integer)
Expand Down
10 changes: 5 additions & 5 deletions lib/litestack/litejobqueue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ def stop
# @@queue = nil
close
end

private

def prepare_search_options(opts)
sql_opts = super(opts)
sql_opts[:klass] = opts[:klass]
sql_opts[:params] = opts[:params]
sql_opts
sql_opts
end

def exit_callback
Expand Down Expand Up @@ -189,15 +189,15 @@ def schedule(spawn = false, &block)
# create a worker according to environment
def create_worker
# temporarily stop this feature until a better solution is implemented
#return if defined?(Rails) && !defined?(Rails::Server)
# return if defined?(Rails) && !defined?(Rails::Server)
Litescheduler.spawn do
worker_sleep_index = 0
while @running
processed = 0
@queues.each do |priority, queues| # iterate through the levels
queues.each do |queue, spawns| # iterate through the queues in the level
batched = 0

while (batched < priority) && (payload = pop(queue, 1)) # fearlessly use the same queue object
capture(:dequeue, queue)
processed += 1
Expand Down
28 changes: 22 additions & 6 deletions lib/litestack/litequeue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def snapshot
queues: queues
}
end

def find(opts = {})
run_stmt(:search, prepare_search_options(opts))
end
Expand All @@ -120,12 +120,28 @@ def find(opts = {})

def prepare_search_options(opts)
sql_opts = {}
sql_opts[:fire_at_from] = opts[:fire_at][0] rescue nil
sql_opts[:fire_at_to] = opts[:fire_at][1] rescue nil
sql_opts[:created_at_from] = opts[:created_at][0] rescue nil
sql_opts[:created_at_to] = opts[:created_at][1] rescue nil
sql_opts[:fire_at_from] = begin
opts[:fire_at][0]
rescue
nil
end
sql_opts[:fire_at_to] = begin
opts[:fire_at][1]
rescue
nil
end
sql_opts[:created_at_from] = begin
opts[:created_at][0]
rescue
nil
end
sql_opts[:created_at_to] = begin
opts[:created_at][1]
rescue
nil
end
sql_opts[:name] = opts[:queue]
sql_opts[:dir] = opts[:dir] == :desc ? -1 : 1
sql_opts[:dir] = (opts[:dir] == :desc) ? -1 : 1
sql_opts
end

Expand Down
3 changes: 1 addition & 2 deletions lib/litestack/litescheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def synchronize(&block)
end
end
end

module ForkListener
def self.listeners
@listeners ||= []
Expand All @@ -99,7 +99,6 @@ def _fork(*args)
result
end
end

end

Process.singleton_class.prepend(Litescheduler::Forkable)
29 changes: 15 additions & 14 deletions lib/litestack/litesearch/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ def similar(limit = 10)
end

module ClassMethods

def litesearch
# it is possible that this code is running when there is no table created yet
if !defined?(ActiveRecord::Base).nil? && ancestors.include?(ActiveRecord::Base)
unless table_exists?
# capture the schema block
@schema = ::Litesearch::Schema.new
@schema.model_class = self if @schema.respond_to? :model_class
@schema.type :backed
@schema.type :backed
@schema.table table_name.to_sym
yield @schema
@schema.post_init
Expand Down Expand Up @@ -137,29 +136,31 @@ def create_instance(row)
end

module ActiveRecordSchemaMethods

attr_accessor :model_class

def field(name, attributes = {})
keys = attributes.keys
if keys.include?(:action_text) || keys.include?(:rich_text)
attributes[:source] = "#{ActionText::RichText.table_name}.body" rescue "action_text_rich_texts.body"
attributes[:reference] = :record_id
attributes[:conditions] = { record_type: model_class.name }
attributes[:source] = begin
"#{ActionText::RichText.table_name}.body"
rescue
"action_text_rich_texts.body"
end
attributes[:reference] = :record_id
attributes[:conditions] = {record_type: model_class.name}
attributes[:target] = nil
elsif keys.include? :as
attributes[:source] = attributes[:target] unless attributes[:source]
attributes[:source] = attributes[:target] unless attributes[:source]
attributes[:reference] = "#{attributes[:as]}_id"
attributes[:conditions] = {"#{attributes[:as]}_type".to_sym => model_class.name }
attributes[:conditions] = {"#{attributes[:as]}_type": model_class.name}
attributes[:target] = nil
end
super(name, attributes)
end
end

def allowed_attributes
super + [:polymorphic, :as, :action_text]
end

def allowed_attributes
super + [:polymorphic, :as, :action_text]
end
end

module ActiveRecordInstanceMethods; end
Expand Down
1 change: 0 additions & 1 deletion lib/litestack/litesearch/schema.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require_relative "schema_adapters"

class Litesearch::Schema

TOKENIZERS = {
porter: "porter unicode61 remove_diacritics 2",
unicode: "unicode61 remove_diacritics 2",
Expand Down
32 changes: 14 additions & 18 deletions lib/litestack/litesearch/schema_adapters/backed_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def drop_primary_triggers_sql

def create_primary_triggers_sql(active = false)
when_stmt = "TRUE"
cols = active_cols_names.select{|n| !n.nil?}
cols = active_cols_names.select { |n| !n.nil? }
if (filter = @schema[:filter_column])
when_stmt = "NEW.#{filter} = TRUE"
cols << filter
end
update_filter = String.new
update_filter = +""
if cols.length > 0
" OF #{cols.join(', ')} "
" OF #{cols.join(", ")} "
end

<<-SQL
Expand Down Expand Up @@ -68,7 +68,7 @@ def create_secondary_trigger_sql(target_table, target_col, col)
end

def create_secondary_trigger_poly_sql(target_table, target_col, col, conditions)
conditions_sql = conditions.collect{|k, v| "NEW.#{k} = '#{v}'"}.join(" AND ")
conditions_sql = conditions.collect { |k, v| "NEW.#{k} = '#{v}'" }.join(" AND ")
<<~SQL
CREATE TRIGGER IF NOT EXISTS #{target_table}_#{target_col}_#{name}_insert AFTER INSERT ON #{target_table} WHEN #{conditions_sql} BEGIN
#{rebuild_sql};
Expand All @@ -79,10 +79,8 @@ def create_secondary_trigger_poly_sql(target_table, target_col, col, conditions)
SQL
end



def drop_secondary_triggers_sql
sql = String.new
sql = +""
@schema[:fields].each do |name, field|
if field[:trigger_sql]
if field[:col]
Expand All @@ -96,7 +94,7 @@ def drop_secondary_triggers_sql
end

def create_secondary_triggers_sql
sql = String.new
sql = +""
@schema[:fields].each do |name, field|
if field[:trigger_sql]
if field[:col]
Expand Down Expand Up @@ -129,11 +127,11 @@ def enrich_schema
target_table, target_col = field[:source].split(".")
field[:target_table] = target_table.to_sym
field[:target_col] = target_col.to_sym
field[:conditions_sql] = field[:conditions].collect{|k, v| "#{k} = '#{v}'"}.join(" AND ") if field[:conditions]
field[:conditions_sql] = field[:conditions].collect { |k, v| "#{k} = '#{v}'" }.join(" AND ") if field[:conditions]
field[:sql] = "SELECT #{field[:target_col]} FROM #{field[:target_table]} WHERE #{field[:reference]} = NEW.id"
field[:sql] += " AND #{field[:conditions_sql]}" if field[:conditions_sql]
field[:sql] += " AND #{field[:conditions_sql]}" if field[:conditions_sql]
field[:sql] = "(#{field[:sql]})"
field[:trigger_sql] = true
field[:trigger_sql] = true
field[:target_table_alias] = "#{field[:target_table]}_#{name}"
else
field[:col] = name unless field[:col]
Expand All @@ -145,7 +143,7 @@ def enrich_schema
end

def filter_sql
sql = String.new
sql = +""
sql << " WHERE #{@schema[:filter_column]} = TRUE " if @schema[:filter_column]
sql
end
Expand All @@ -161,27 +159,25 @@ def select_cols_sql
(!field[:trigger_sql].nil?) ? "#{field[:target_table_alias]}.#{field[:target_col]}" : field[:target]
end.join(", ")
end

def joins_sql
joins = [@schema[:table]]
active_fields.each do |name, field|
if field[:trigger_sql]
join_table = String.new
join_table = +""
join_table << "#{field[:target_table]} AS #{field[:target_table_alias]} ON "
if field[:col]
join_table << "#{field[:target_table_alias]}.id = #{@schema[:table]}.#{field[:col]}" if field[:col]
elsif field[:source]
join_table << "#{field[:target_table_alias]}.#{field[:reference]} = #{@schema[:table]}.id"
if field[:conditions]
join_table << " AND "
join_table << field[:conditions].collect{|k, v| "#{field[:target_table_alias]}.#{k} = '#{v}'"}.join(" AND ")
join_table << field[:conditions].collect { |k, v| "#{field[:target_table_alias]}.#{k} = '#{v}'" }.join(" AND ")
end
end
joins << join_table
joins << join_table
end
end
joins.join(" LEFT JOIN ")
end


end
Loading

0 comments on commit 76a3bcd

Please sign in to comment.