enum_table 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.2.0 2013-05-14
2
+
3
+ * Memoize enum_table list. Your logs are now less noisy.
4
+ * #enum no longer requires the table to exist when migrating or loading the
5
+ schema, so migrations have a chance to create the enum tables.
6
+
1
7
  == 0.1.0 2012-11-30
2
8
 
3
9
  * DB Nazi compliance.
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
  gemspec
@@ -4,6 +4,37 @@ module EnumTable
4
4
  autoload :Reflection, 'enum_table/reflection'
5
5
  autoload :SchemaDumper, 'enum_table/schema_dumper'
6
6
  autoload :SchemaStatements, 'enum_table/schema_statements'
7
+
8
+ # Allow enum tables to be missing until missing_tables_disallowed is
9
+ # called. This is invoked from rake tasks that create the enum tables, such as
10
+ # Rails migration tasks.
11
+ #
12
+ # Missing table allowance is implemented as a thread-local stack to handle
13
+ # nested invocations in multi-threaded programs.
14
+ class << self
15
+ def missing_tables_allowed
16
+ missing_tables_allowances.push true
17
+ end
18
+
19
+ def missing_tables_disallowed
20
+ missing_tables_allowances.pop
21
+ end
22
+
23
+ def missing_tables_allowed?
24
+ !missing_tables_allowances.empty?
25
+ end
26
+
27
+ # Reset our state. Intended for testing Enum Table.
28
+ def reset
29
+ Thread.current[:enum_table_missing_tables_allowed] = nil
30
+ end
31
+
32
+ private
33
+
34
+ def missing_tables_allowances
35
+ Thread.current[:enum_table_missing_tables_allowed] ||= []
36
+ end
37
+ end
7
38
  end
8
39
 
9
40
  require 'enum_table/railtie' if defined?(Rails)
@@ -5,9 +5,19 @@ module EnumTable
5
5
  task :load_schema_dumper do
6
6
  require 'enum_table/schema_dumper'
7
7
  end
8
+
9
+ task :allow_missing_tables do
10
+ EnumTable.missing_tables_allowed
11
+ end
8
12
  end
9
13
 
10
14
  Rake::Task['db:schema:dump'].prerequisites << 'enum_table:load_schema_dumper'
15
+
16
+ %w'db:schema:load db:migrate db:migrate:up'.each do |task|
17
+ task = Rake::Task[task]
18
+ task.prerequisites.insert 0, 'enum_table:allow_missing_tables'
19
+ task.enhance { EnumTable.missing_tables_disallowed }
20
+ end
11
21
  end
12
22
  end
13
23
  end
@@ -20,7 +20,7 @@ module EnumTable
20
20
  end
21
21
  self.enums = enums.merge(name => reflection, name.to_s => reflection)
22
22
 
23
- class_eval <<-EOS
23
+ class_eval <<-EOS, __FILE__, __LINE__ + 1
24
24
  def #{name}
25
25
  read_enum(:#{name})
26
26
  end
@@ -60,6 +60,7 @@ module EnumTable
60
60
  when String, Symbol, nil
61
61
  map = {}
62
62
  table_name = table || "#{self.table_name.singularize}_#{name.to_s.pluralize}"
63
+ return {} if EnumTable.missing_tables_allowed? && !connection.tables.include?(table_name)
63
64
  connection.execute("SELECT id, value FROM #{table_name}").each do |row|
64
65
  map[row[1]] = row[0]
65
66
  end
@@ -4,6 +4,7 @@ module EnumTable
4
4
  table = NewTable.new(self, table_name, options)
5
5
  yield table if block_given?
6
6
  table._create
7
+ enum_tables_updated
7
8
  end
8
9
 
9
10
  def change_enum_table(table_name)
@@ -13,13 +14,17 @@ module EnumTable
13
14
  def drop_enum_table(table_name)
14
15
  drop_table table_name
15
16
  execute "DELETE FROM enum_tables WHERE table_name = '#{table_name}'"
17
+ enum_tables_updated
16
18
  end
17
19
 
18
20
  def enum_tables
19
21
  return [] if !table_exists?('enum_tables')
20
- execute("SELECT table_name FROM enum_tables").map do |row|
21
- row[0]
22
- end.sort
22
+ @enum_tables ||= execute("SELECT table_name FROM enum_tables").
23
+ map { |row| row[0] }.sort
24
+ end
25
+
26
+ def enum_tables_updated
27
+ @enum_tables = nil
23
28
  end
24
29
 
25
30
  DEFAULT_VALUE_ATTRIBUTES = {type: :string, limit: 255, null: false}.freeze
@@ -1,5 +1,5 @@
1
1
  module EnumTable
2
- VERSION = [0, 1, 0]
2
+ VERSION = [0, 2, 0]
3
3
 
4
4
  class << VERSION
5
5
  include Comparable
@@ -3,17 +3,17 @@ sqlite3:
3
3
  mysql:
4
4
  username: root
5
5
  password:
6
- database: db_nazi_test
6
+ database: enum_table_test
7
7
  mysql2:
8
8
  username: root
9
9
  password:
10
- database: db_nazi_test
10
+ database: enum_table_test
11
11
  postgresql:
12
12
  username: postgres
13
13
  password:
14
- database: db_nazi_test
14
+ database: enum_table_test
15
15
  min_messages: warning
16
16
  jdbcmysql:
17
17
  username: root
18
18
  password:
19
- database: db_nazi_test
19
+ database: enum_table_test
@@ -65,6 +65,29 @@ describe EnumTable do
65
65
  enum.id_name.must_equal :gender_number
66
66
  end
67
67
 
68
+ describe "when missing tables are not allowed" do
69
+ it "raises an error if the underlying table does not exist" do
70
+ # must_raise does not do ancestor lookup in some versions of minitest
71
+ # (notably the version that ships with Ruby 1.9.3). Avoid it until we
72
+ # have a testrb that honors the Gemfile.
73
+ exception = nil
74
+ begin
75
+ User.enum(:status)
76
+ rescue => exception
77
+ end
78
+ exception.must_be_kind_of StandardError
79
+ end
80
+ end
81
+
82
+ describe "when missing tables are allowed" do
83
+ before { EnumTable.missing_tables_allowed }
84
+ after { EnumTable.reset }
85
+
86
+ it "does not raise an error if the underlying table does not exist" do
87
+ User.enum :status
88
+ end
89
+ end
90
+
68
91
  describe "on a subclass" do
69
92
  before do
70
93
  User.inheritance_column = :user_type
@@ -110,6 +110,21 @@ describe EnumTable::SchemaStatements do
110
110
  connection.drop_enum_table :genders
111
111
  connection.enum_tables.must_equal []
112
112
  end
113
+
114
+ it "clears memoized tables when an enum table is created" do
115
+ connection.create_enum_table :genders
116
+ connection.enum_tables.must_equal ['genders']
117
+ connection.create_enum_table :statuses
118
+ connection.enum_tables.must_equal ['genders', 'statuses']
119
+ end
120
+
121
+ it "clears memoized tables when an enum table is dropped" do
122
+ connection.create_enum_table :genders
123
+ connection.create_enum_table :statuses
124
+ connection.enum_tables.must_equal ['genders', 'statuses']
125
+ connection.drop_enum_table :genders
126
+ connection.enum_tables.must_equal ['statuses']
127
+ end
113
128
  end
114
129
 
115
130
  def read_table(name)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-30 00:00:00.000000000 Z
12
+ date: 2013-05-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord