talentbox-sequel-rails 0.2.0

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.
Files changed (41) hide show
  1. data/.document +5 -0
  2. data/.gitignore +29 -0
  3. data/CHANGELOG +21 -0
  4. data/Gemfile +3 -0
  5. data/Gemfile.lock +114 -0
  6. data/LICENSE +20 -0
  7. data/README.rdoc +86 -0
  8. data/Rakefile +9 -0
  9. data/autotest/discover.rb +1 -0
  10. data/lib/generators/sequel.rb +83 -0
  11. data/lib/generators/sequel/migration/migration_generator.rb +30 -0
  12. data/lib/generators/sequel/migration/templates/migration.rb +16 -0
  13. data/lib/generators/sequel/model/model_generator.rb +23 -0
  14. data/lib/generators/sequel/model/templates/model.rb +3 -0
  15. data/lib/generators/sequel/observer/observer_generator.rb +19 -0
  16. data/lib/generators/sequel/observer/templates/observer.rb +7 -0
  17. data/lib/sequel-rails.rb +7 -0
  18. data/lib/sequel-rails/configuration.rb +63 -0
  19. data/lib/sequel-rails/migrations.rb +30 -0
  20. data/lib/sequel-rails/railtie.rb +90 -0
  21. data/lib/sequel-rails/railties/benchmarking_mixin.rb +23 -0
  22. data/lib/sequel-rails/railties/controller_runtime.rb +43 -0
  23. data/lib/sequel-rails/railties/database.rake +167 -0
  24. data/lib/sequel-rails/railties/i18n_support.rb +12 -0
  25. data/lib/sequel-rails/railties/log_subscriber.rb +31 -0
  26. data/lib/sequel-rails/runtime.rb +14 -0
  27. data/lib/sequel-rails/session_store.rb +82 -0
  28. data/lib/sequel-rails/setup.rb +17 -0
  29. data/lib/sequel-rails/storage.rb +221 -0
  30. data/lib/sequel-rails/version.rb +5 -0
  31. data/sequel-rails.gemspec +29 -0
  32. data/spec/rcov.opts +6 -0
  33. data/spec/spec.opts +4 -0
  34. data/spec/spec_helper.rb +7 -0
  35. data/tasks/ci.rake +1 -0
  36. data/tasks/clean.rake +6 -0
  37. data/tasks/metrics.rake +37 -0
  38. data/tasks/spec.rake +30 -0
  39. data/tasks/yard.rake +9 -0
  40. data/tasks/yardstick.rake +20 -0
  41. metadata +177 -0
@@ -0,0 +1,12 @@
1
+ module Rails
2
+ module Sequel
3
+
4
+ module I18nSupport
5
+ # Set the i18n scope to overwrite ActiveModel.
6
+ def i18n_scope #:nodoc:
7
+ :sequel
8
+ end
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ module Sequel
2
+ module Railties
3
+
4
+ class LogSubscriber < ActiveSupport::LogSubscriber
5
+
6
+ def sql(event)
7
+ name = '%s (%.1fms)' % [event.payload[:name], event.duration]
8
+ sql = event.payload[:sql].squeeze(' ')
9
+
10
+ if odd?
11
+ name = color(name, :cyan, true)
12
+ sql = color(sql, nil, true)
13
+ else
14
+ name = color(name, :magenta, true)
15
+ end
16
+
17
+ debug " #{name} #{sql}"
18
+ end
19
+
20
+ def odd?
21
+ @odd_or_even = !@odd_or_even
22
+ end
23
+
24
+ def logger
25
+ ::Rails::Sequel.configuration.logger
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ module Rails
2
+ module Sequel
3
+
4
+ class << self
5
+ def reset_runtime
6
+ @runtime ||= 0
7
+
8
+ rt, @runtime = @runtime, 0
9
+ rt
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,82 @@
1
+ require 'sequel'
2
+
3
+ # Implements Sequel-specific session store.
4
+
5
+ module Rails
6
+ module Sequel
7
+
8
+ class SessionStore < ActionDispatch::Session::AbstractStore
9
+
10
+ class Session < ::Sequel::Model
11
+
12
+ # property :id, Serial
13
+ # property :session_id, String, :required => true, :unique => true, :unique_index => true
14
+ # property :data, Object, :required => true, :default => ActiveSupport::Base64.encode64(Marshal.dump({}))
15
+ # property :updated_at, DateTime, :required => false, :index => true
16
+
17
+ class << self
18
+
19
+ def auto_migrate!
20
+ self.db.create_table :sessions do
21
+ primary_key :id
22
+ column :session_id, String,
23
+ :null => false,
24
+ :unique => true,
25
+ :index => true
26
+
27
+ column :data, :text,
28
+ :null => false
29
+
30
+ column :updated_at, DateTime,
31
+ :null => true,
32
+ :index => true
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ def self.name
39
+ 'session'
40
+ end
41
+
42
+ end
43
+
44
+ SESSION_RECORD_KEY = 'rack.session.record'.freeze
45
+
46
+ cattr_accessor :session_class
47
+ self.session_class = Session
48
+
49
+ private
50
+
51
+ def get_session(env, sid)
52
+ sid ||= generate_sid
53
+ session = find_session(sid)
54
+ env[SESSION_RECORD_KEY] = session
55
+ [ sid, session.data ]
56
+ end
57
+
58
+ def set_session(env, sid, session_data)
59
+ session = get_session_resource(env, sid)
60
+ session.data = session_data
61
+ session.updated_at = Time.now if session.dirty?
62
+ session.save
63
+ end
64
+
65
+ def get_session_resource(env, sid)
66
+ if env[ENV_SESSION_OPTIONS_KEY][:id].nil?
67
+ env[SESSION_RECORD_KEY] = find_session(sid)
68
+ else
69
+ env[SESSION_RECORD_KEY] ||= find_session(sid)
70
+ end
71
+ end
72
+
73
+ def find_session(sid)
74
+ klass = self.class.session_class
75
+
76
+ klass.where(:session_id => sid).first || klass.new(:session_id => sid)
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_support/core_ext/hash/except'
2
+
3
+ require 'sequel/extensions/migration'
4
+
5
+ require 'sequel-rails/configuration'
6
+ require 'sequel-rails/runtime'
7
+ require 'sequel-rails/railties/benchmarking_mixin'
8
+
9
+ module Rails
10
+ module Sequel
11
+
12
+ def self.setup(environment)
13
+ ::Sequel.connect({:logger => configuration.logger}.merge(::Rails::Sequel.configuration.environment_for(environment.to_s)))
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,221 @@
1
+ module Rails
2
+ module Sequel
3
+
4
+ def self.storage
5
+ Storage
6
+ end
7
+
8
+ class Storage
9
+ attr_reader :config
10
+
11
+ def self.create_all
12
+ with_local_repositories { |config| create_environment(config) }
13
+ end
14
+
15
+ def self.drop_all
16
+ with_local_repositories { |config| drop_environment(config) }
17
+ end
18
+
19
+ def self.create_environment(config)
20
+ new(config).create
21
+ end
22
+
23
+ def self.drop_environment(config)
24
+ new(config).drop
25
+ end
26
+
27
+ def self.new(config)
28
+ config = Rails::Sequel.configuration.environments[config.to_s] unless config.kind_of?(Hash)
29
+
30
+ klass = lookup_class(config['adapter'])
31
+ if klass.equal?(self)
32
+ super(config)
33
+ else
34
+ klass.new(config)
35
+ end
36
+ end
37
+
38
+ class << self
39
+ private
40
+
41
+ def with_local_repositories
42
+ Rails::Sequel.configuration.environments.each_value do |config|
43
+ if config['host'].blank? || %w[ 127.0.0.1 localhost ].include?(config['host'])
44
+ yield(config)
45
+ else
46
+ puts "This task only modifies local databases. #{config['database']} is on a remote host."
47
+ end
48
+ end
49
+ end
50
+
51
+ def lookup_class(adapter)
52
+ klass_name = adapter.camelize.to_sym
53
+
54
+ unless Storage.const_defined?(klass_name)
55
+ raise "Adapter #{adapter} not supported (#{klass_name.inspect})"
56
+ end
57
+
58
+ const_get(klass_name)
59
+ end
60
+
61
+ end
62
+
63
+ def initialize(config)
64
+ @config = config
65
+ end
66
+
67
+ def create
68
+ _create
69
+ puts "[sequel] Created database '#{database}'"
70
+ end
71
+
72
+ def drop
73
+ _drop
74
+ puts "[sequel] Dropped database '#{database}'"
75
+ end
76
+
77
+ def database
78
+ @database ||= config['database'] || config['path']
79
+ end
80
+
81
+ def username
82
+ @username ||= config['username'] || ''
83
+ end
84
+
85
+ def password
86
+ @password ||= config['password'] || ''
87
+ end
88
+
89
+ def host
90
+ @host ||= config['host'] || ''
91
+ end
92
+
93
+ def port
94
+ @port ||= config['port'] || ''
95
+ end
96
+
97
+ def owner
98
+ @owner ||= config['owner'] || ''
99
+ end
100
+
101
+ def charset
102
+ @charset ||= config['charset'] || ENV['CHARSET'] || 'utf8'
103
+ end
104
+
105
+ class Sqlite < Storage
106
+ def _create
107
+ return if in_memory?
108
+ ::Sequel.connect(config.merge('database' => path))
109
+ end
110
+
111
+ def _drop
112
+ return if in_memory?
113
+ path.unlink if path.file?
114
+ end
115
+
116
+ private
117
+
118
+ def in_memory?
119
+ database == ':memory:'
120
+ end
121
+
122
+ def path
123
+ @path ||= Pathname(File.expand_path(database, Rails.root))
124
+ end
125
+
126
+ end
127
+
128
+ class Mysql < Storage
129
+ def _create
130
+ execute("CREATE DATABASE IF NOT EXISTS `#{database}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}")
131
+ end
132
+
133
+ def _drop
134
+ execute("DROP DATABASE IF EXISTS `#{database}`")
135
+ end
136
+
137
+ private
138
+
139
+ def execute(statement)
140
+ commands = 'mysql '
141
+ commands << "--user=#{username} " unless username.blank?
142
+ commands << "--password=#{password} " unless password.blank?
143
+ commands << "--host=#{host} " unless host.blank?
144
+ commands << '-e '
145
+ commands << statement
146
+ system(commands)
147
+ end
148
+
149
+ def collation
150
+ @collation ||= config['collation'] || ENV['COLLATION'] || 'utf8_unicode_ci'
151
+ end
152
+
153
+ end
154
+
155
+ class Postgres < Storage
156
+ def _create
157
+ commands = "createdb --encoding=#{charset} "
158
+ commands << "--username=#{username} " unless username.blank?
159
+ commands << "--owner=#{owner} " unless owner.blank?
160
+ commands << "--port=#{port} " unless port.blank?
161
+ commands << "--host=#{host} " unless host.blank?
162
+ commands << database
163
+ system(commands)
164
+ end
165
+
166
+ def _drop
167
+ system(
168
+ 'dropdb',
169
+ '-U',
170
+ username,
171
+ database
172
+ )
173
+ end
174
+ end
175
+
176
+ class Jdbc < Storage
177
+
178
+ def _is_mysql?
179
+ database.match(/^jdbc:mysql/)
180
+ end
181
+
182
+ def _root_url
183
+ database.scan /^jdbc:mysql:\/\/\w*:?\d*/
184
+ end
185
+
186
+ def db_name
187
+ database.scan(/^jdbc:mysql:\/\/\w+:?\d*\/(\w+)/).flatten.first
188
+ end
189
+
190
+ def _params
191
+ database.scan /\?.*$/
192
+ end
193
+
194
+ def _create
195
+ if _is_mysql?
196
+ ::Sequel.connect("#{_root_url}#{_params}") do |db|
197
+ db.execute("CREATE DATABASE IF NOT EXISTS `#{db_name}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}")
198
+ end
199
+ end
200
+ end
201
+
202
+ def _drop
203
+ if _is_mysql?
204
+ ::Sequel.connect("#{_root_url}#{_params}") do |db|
205
+ db.execute("DROP DATABASE IF EXISTS `#{db_name}`")
206
+ end
207
+ end
208
+ end
209
+
210
+ private
211
+
212
+ def collation
213
+ @collation ||= config['collation'] || ENV['COLLATION'] || 'utf8_unicode_ci'
214
+ end
215
+
216
+
217
+ end
218
+
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,5 @@
1
+ module Rails
2
+ module Sequel
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require "sequel-rails/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "talentbox-sequel-rails"
8
+ s.version = Rails::Sequel::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Brasten Sager (brasten)", "Jonathan TRON"]
11
+ s.email = ["brasten@gmail.com", "jonathan.tron@thetalentbox.com"]
12
+ s.homepage = "https://github.com/TalentBox/sequel-rails"
13
+ s.description = "Integrate Sequel with Rails 3"
14
+ s.summary = "Use Sequel with Rails 3"
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ s.extra_rdoc_files = ["LICENSE", "README.rdoc"]
20
+ s.rdoc_options = ["--charset=UTF-8"]
21
+ s.add_runtime_dependency("sequel", ["~> 3.28.0"])
22
+ s.add_runtime_dependency("rails", ["~> 3.1.1"])
23
+
24
+ s.add_development_dependency("rake", ["~> 0.8.7"])
25
+ s.add_development_dependency("yard", ["~> 0.5"])
26
+ s.add_development_dependency("rspec", ["~> 2.7.0"])
27
+ s.add_development_dependency("autotest", ["~> 4.4.6"])
28
+ s.add_development_dependency("rcov", ["~> 0.9.11"])
29
+ end
@@ -0,0 +1,6 @@
1
+ --exclude-only "spec\/,^\/"
2
+ --sort coverage
3
+ --callsites
4
+ --xrefs
5
+ --profile
6
+ --text-summary
@@ -0,0 +1,4 @@
1
+ --color
2
+ --loadby random
3
+ --format profile
4
+ --backtrace
@@ -0,0 +1,7 @@
1
+ require "sequel-rails"
2
+
3
+ # Load support files
4
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
5
+
6
+ RSpec.configure do |config|
7
+ end
@@ -0,0 +1 @@
1
+ task :ci => [ 'metrics:all' ]
@@ -0,0 +1,6 @@
1
+ require 'rake/clean'
2
+
3
+ File.foreach('.gitignore') do |line|
4
+ line.strip!
5
+ CLOBBER << line unless line.empty? || line[0, 1] == '#'
6
+ end