Skip to content

Commit

Permalink
Use Hash#fetch instead of ternary operator in multiple places to simp…
Browse files Browse the repository at this point in the history
…lify code
  • Loading branch information
jeremyevans committed Apr 8, 2010
1 parent c3b85ec commit 4d86861
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/sequel/adapters/firebird.rb
Expand Up @@ -156,7 +156,7 @@ def create_trigger_sql(table, name, definition, opts={})
events = opts[:events] ? Array(opts[:events]) : [:insert, :update, :delete]
whence = opts[:after] ? 'AFTER' : 'BEFORE'
inactive = opts[:inactive] ? 'INACTIVE' : 'ACTIVE'
position = opts[:position] ? opts[:position] : 0
position = opts.fetch(:position, 0)
sql = <<-end_sql
CREATE TRIGGER #{quote_identifier(name)} for #{quote_identifier(table)}
#{inactive} #{whence} #{events.map{|e| e.to_s.upcase}.join(' OR ')} position #{position}
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/adapters/jdbc.rb
Expand Up @@ -113,7 +113,7 @@ class Database < Sequel::Database
# uri, since JDBC requires one.
def initialize(opts)
super
@convert_types = @opts.include?(:convert_types) ? typecast_value_boolean(@opts[:convert_types]) : true
@convert_types = typecast_value_boolean(@opts.fetch(:convert_types, true))
raise(Error, "No connection string specified") unless uri

resolved_uri = jndi? ? get_uri_from_jndi : uri
Expand Down
6 changes: 3 additions & 3 deletions lib/sequel/adapters/shared/mysql.rb
Expand Up @@ -131,9 +131,9 @@ def column_definition_sql(column)

# Use MySQL specific syntax for engine type and character encoding
def create_table_sql(name, generator, options = {})
engine = options.include?(:engine) ? options[:engine] : Sequel::MySQL.default_engine
charset = options.include?(:charset) ? options[:charset] : Sequel::MySQL.default_charset
collate = options.include?(:collate) ? options[:collate] : Sequel::MySQL.default_collate
engine = options.fetch(:engine, Sequel::MySQL.default_engine)
charset = options.fetch(:charset, Sequel::MySQL.default_charset)
collate = options.fetch(:collate, Sequel::MySQL.default_collate)
generator.columns.each do |c|
if t = c.delete(:table)
generator.foreign_key([c[:name]], t, c.merge(:name=>nil, :type=>:foreign_key))
Expand Down
10 changes: 5 additions & 5 deletions lib/sequel/database.rb
Expand Up @@ -91,9 +91,9 @@ def initialize(opts = {}, &block)
block ||= proc{|server| connect(server)}
@opts[:servers] = {} if @opts[:servers].is_a?(String)

@opts[:single_threaded] = @single_threaded = @opts.include?(:single_threaded) ? typecast_value_boolean(@opts[:single_threaded]) : @@single_threaded
@opts[:single_threaded] = @single_threaded = typecast_value_boolean(@opts.fetch(:single_threaded, @@single_threaded))
@schemas = {}
@default_schema = opts.include?(:default_schema) ? @opts[:default_schema] : default_schema_default
@default_schema = @opts.fetch(:default_schema, default_schema_default)
@prepared_statements = {}
@transactions = []
@identifier_input_method = nil
Expand Down Expand Up @@ -393,7 +393,7 @@ def get(*args, &block)
def identifier_input_method
case @identifier_input_method
when nil
@identifier_input_method = @opts.include?(:identifier_input_method) ? @opts[:identifier_input_method] : (@@identifier_input_method.nil? ? identifier_input_method_default : @@identifier_input_method)
@identifier_input_method = @opts.fetch(:identifier_input_method, (@@identifier_input_method.nil? ? identifier_input_method_default : @@identifier_input_method))
@identifier_input_method == "" ? nil : @identifier_input_method
when ""
nil
Expand All @@ -412,7 +412,7 @@ def identifier_input_method=(v)
def identifier_output_method
case @identifier_output_method
when nil
@identifier_output_method = @opts.include?(:identifier_output_method) ? @opts[:identifier_output_method] : (@@identifier_output_method.nil? ? identifier_output_method_default : @@identifier_output_method)
@identifier_output_method = @opts.fetch(:identifier_output_method, (@@identifier_output_method.nil? ? identifier_output_method_default : @@identifier_output_method))
@identifier_output_method == "" ? nil : @identifier_output_method
when ""
nil
Expand Down Expand Up @@ -474,7 +474,7 @@ def quote_identifiers=(v)
# Returns true if the database quotes identifiers.
def quote_identifiers?
return @quote_identifiers unless @quote_identifiers.nil?
@quote_identifiers = @opts.include?(:quote_identifiers) ? @opts[:quote_identifiers] : (@@quote_identifiers.nil? ? quote_identifiers_default : @@quote_identifiers)
@quote_identifiers = @opts.fetch(:quote_identifiers, (@@quote_identifiers.nil? ? quote_identifiers_default : @@quote_identifiers))
end

# Dynamically remove existing servers from the connection pool. Intended for
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/database/schema_sql.rb
Expand Up @@ -69,7 +69,7 @@ def auto_increment_sql
def column_definition_sql(column)
sql = "#{quote_identifier(column[:name])} #{type_literal(column)}"
sql << UNIQUE if column[:unique]
null = column.include?(:null) ? column[:null] : column[:allow_null]
null = column.fetch(:null, column[:allow_null])
sql << NOT_NULL if null == false
sql << NULL if null == true
sql << " DEFAULT #{literal(column[:default])}" if column.include?(:default)
Expand Down
4 changes: 2 additions & 2 deletions lib/sequel/model/associations.rb
Expand Up @@ -618,7 +618,7 @@ def associate(type, name, opts = {}, &block)
opts[:order_eager_graph] = true unless opts.include?(:order_eager_graph)
conds = opts[:conditions]
opts[:graph_conditions] = conds if !opts.include?(:graph_conditions) and Sequel.condition_specifier?(conds)
opts[:graph_conditions] = opts[:graph_conditions] ? opts[:graph_conditions].to_a : []
opts[:graph_conditions] = opts.fetch(:graph_conditions, []).to_a
opts[:graph_select] = Array(opts[:graph_select]) if opts[:graph_select]
[:before_add, :before_remove, :after_add, :after_remove, :after_load, :before_set, :after_set, :extend].each do |cb_type|
opts[cb_type] = Array(opts[cb_type])
Expand Down Expand Up @@ -747,7 +747,7 @@ def def_many_to_many(opts)
opts[:cartesian_product_number] ||= 1
join_table = (opts[:join_table] ||= opts.default_join_table)
left_key_alias = opts[:left_key_alias] ||= opts.default_associated_key_alias
graph_jt_conds = opts[:graph_join_table_conditions] = opts[:graph_join_table_conditions] ? opts[:graph_join_table_conditions].to_a : []
graph_jt_conds = opts[:graph_join_table_conditions] = opts.fetch(:graph_join_table_conditions, []).to_a
opts[:graph_join_table_join_type] ||= opts[:graph_join_type]
opts[:after_load].unshift(:array_uniq!) if opts[:uniq]
opts[:dataset] ||= proc{opts.associated_class.inner_join(join_table, rcks.zip(opts.right_primary_keys) + lcks.zip(lcpks.map{|k| send(k)}))}
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/model/base.rb
Expand Up @@ -1006,7 +1006,7 @@ def update_restricted(hash, only, except)
# option is present in the hash, use that, otherwise, fallback to the
# object's default (if set), or class's default (if not).
def use_transaction?(opts = {})
opts.include?(:transaction) ? opts[:transaction] : use_transactions
opts.fetch(:transaction, use_transactions)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/plugins/many_through_many.rb
Expand Up @@ -193,7 +193,7 @@ def def_many_through_many(opts)
opts[:eager_grapher] ||= proc do |ds, assoc_alias, table_alias|
iq = table_alias
opts.edges.each do |t|
ds = ds.graph(t[:table], t.include?(:only_conditions) ? t[:only_conditions] : (Array(t[:right]).zip(Array(t[:left])) + t[:conditions]), :select=>false, :table_alias=>ds.unused_table_alias(t[:table]), :join_type=>t[:join_type], :implicit_qualifier=>iq, &t[:block])
ds = ds.graph(t[:table], t.fetch(:only_conditions, (Array(t[:right]).zip(Array(t[:left])) + t[:conditions])), :select=>false, :table_alias=>ds.unused_table_alias(t[:table]), :join_type=>t[:join_type], :implicit_qualifier=>iq, &t[:block])
iq = nil
end
fe = opts[:final_edge]
Expand Down

0 comments on commit 4d86861

Please sign in to comment.