Class: Firefly::Server

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/firefly/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, &blk) ⇒ Server

Returns a new instance of Server.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/firefly/server.rb', line 254

def initialize config = {}, &blk
  super
  @config = config.is_a?(Config) ? config : Firefly::Config.new(config)
  @config.instance_eval(&blk) if block_given?
  Firefly::CodeFactory.order = @config[:order]
  begin
    DataMapper.setup(:default, @config[:database])
    DataMapper.auto_upgrade!
    check_mysql_collation
    check_code_factory
  rescue
    puts "Error setting up database connection. Please check the `database` setting in config.ru"
    puts $!
    puts "-------"
    puts $!.backtrace
    exit(1)
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



28
29
30
# File 'lib/firefly/server.rb', line 28

def config
  @config
end

Instance Method Details

#check_code_factoryObject



273
274
275
# File 'lib/firefly/server.rb', line 273

def check_code_factory
  Firefly::CodeFactory.first || Firefly::CodeFactory.create(:count => 0)
end

#check_mysql_collation(first_try = true) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/firefly/server.rb', line 277

def check_mysql_collation(first_try = true)
  # Make sure the 'code' column is case-sensitive. This hack is for
  # MySQL only, other database systems don't have this problem.
  if DataMapper.repository(:default).adapter =~ "DataMapper::Adapters::MysqlAdapter"
    query     = "SHOW FULL COLUMNS FROM firefly_urls WHERE Field='code';"
    collation = DataMapper.repository(:default).adapter.select(query)[0][:collation]

    if collation != "utf8_bin"
      if first_try
        puts " ~ Your MySQL database is not using the 'utf8-bin' collation. Trying to fix..."
        DataMapper.repository(:default).adapter.execute("ALTER TABLE firefly_urls MODIFY `code` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
        return check_mysql_collation(false)
      else
        puts " ~ Failed to set the collation for `code` in `firefly_urls`. Please see http://wiki.github.com/ariejan/firefly/faq for details."
        return false
      end
    else
      if !first_try
        puts " ~ Successfully fixed your database."
      end
      return true
    end
  end
end