active_finite 0.1.4 → 0.1.5
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.
- data/README.rdoc +2 -2
- data/VERSION +1 -1
- data/lib/active_finite.rb +14 -54
- data/lib/get_table.rb +13 -0
- data/lib/master_table.rb +49 -0
- data/spec/active_finite_spec.rb +44 -10
- data/spec/spec_helper.rb +1 -1
- metadata +5 -3
data/README.rdoc
CHANGED
@@ -24,7 +24,7 @@ Let's start by defining a simple migration:
|
|
24
24
|
end
|
25
25
|
|
26
26
|
Now that we some hellos in our Hello model, we can access them using:
|
27
|
-
hellos =
|
27
|
+
hellos = get_table :hellos
|
28
28
|
|
29
29
|
The variable hellos now holds a class derived from ActiveRecord named
|
30
30
|
Hello, so all of the normal ActiveRecord methods can be used.
|
@@ -33,7 +33,7 @@ We can also a collection of all of the active record classes defined by
|
|
33
33
|
active_finite by using:
|
34
34
|
all_finite_tables
|
35
35
|
|
36
|
-
Using
|
36
|
+
Using get_table and all_finite_tables implicitly bring the associated
|
37
37
|
classes into scope, so a single call to all_finite_tables is sufficent to
|
38
38
|
define all of the models.
|
39
39
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/lib/active_finite.rb
CHANGED
@@ -3,59 +3,13 @@ require 'find'
|
|
3
3
|
require 'active_support/inflector'
|
4
4
|
require 'active_record'
|
5
5
|
require 'json'
|
6
|
-
|
7
|
-
|
8
|
-
class_name = as_class_name sym
|
9
|
-
class_not_defined = not(Object.const_defined? class_name)
|
10
|
-
should_force = args.any? {|x| x == :force}
|
11
|
-
if class_not_defined or should_force
|
12
|
-
anon = Class.new ActiveRecord::Base
|
13
|
-
Object.const_set class_name, anon
|
14
|
-
else
|
15
|
-
Object.const_get class_name
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def as_class_name table_name
|
20
|
-
table_name.to_s.singularize.capitalize.to_sym
|
21
|
-
end
|
6
|
+
require_relative 'master_table.rb'
|
7
|
+
require_relative 'get_table.rb'
|
22
8
|
|
23
9
|
def all_finite_tables
|
24
|
-
|
25
|
-
if master.table_exists?
|
26
|
-
master
|
27
|
-
.where("#{default_column_name} != ?", master_table_name)
|
28
|
-
.collect {|x| x.send default_column_name.to_s}
|
29
|
-
.collect {|x| get_finite_table x}
|
30
|
-
else
|
31
|
-
[]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def master_table_name
|
36
|
-
:active_finites
|
37
|
-
end
|
38
|
-
|
39
|
-
def add_to_master table_name
|
40
|
-
add_finites in_table: master_table_name, values: [table_name]
|
41
|
-
end
|
42
|
-
|
43
|
-
def delete_from_master table_name
|
44
|
-
delete_finites in_table: master_table_name, values: [table_name]
|
45
|
-
end
|
46
|
-
|
47
|
-
def is_in_master_table? table_name
|
48
|
-
if table_name.eql? master_table_name
|
49
|
-
true
|
50
|
-
else
|
51
|
-
get_finite_table(master_table_name)
|
52
|
-
.where("#{default_column_name} != ?", master_table_name)
|
53
|
-
.where("#{default_column_name} == ?", table_name)
|
54
|
-
.any?
|
55
|
-
end
|
10
|
+
MasterTable.all
|
56
11
|
end
|
57
12
|
|
58
|
-
|
59
13
|
def add_finites args
|
60
14
|
modify_finite args do |vs, klass, column_name|
|
61
15
|
if not klass.table_exists?
|
@@ -63,9 +17,10 @@ def add_finites args
|
|
63
17
|
create_table klass.table_name do |t|
|
64
18
|
t.string column_name, :null => false
|
65
19
|
end
|
20
|
+
add_index klass.table_name, column_name, :unique => true
|
66
21
|
end
|
67
|
-
add_to_master klass.table_name
|
68
22
|
end
|
23
|
+
MasterTable.add klass.table_name
|
69
24
|
vs.each do |v|
|
70
25
|
obj = klass.new
|
71
26
|
obj.send column_name.to_s + '=', v
|
@@ -74,12 +29,13 @@ def add_finites args
|
|
74
29
|
end
|
75
30
|
end
|
76
31
|
|
77
|
-
def delete_finites args
|
32
|
+
def delete_finites args
|
78
33
|
delete_all = args[:values] == :all
|
79
34
|
if delete_all
|
80
35
|
args[:values] = []
|
81
36
|
end
|
82
37
|
modify_finite args do |vs, klass, column_name|
|
38
|
+
MasterTable.add klass.table_name
|
83
39
|
if delete_all
|
84
40
|
klass.all.each do |o|
|
85
41
|
klass.destroy o
|
@@ -96,7 +52,7 @@ def delete_finites args,
|
|
96
52
|
ActiveRecord::Schema.define do
|
97
53
|
drop_table klass.table_name
|
98
54
|
end
|
99
|
-
|
55
|
+
MasterTable.delete klass.table_name
|
100
56
|
Object.send 'remove_const', klass.to_s.to_sym
|
101
57
|
end
|
102
58
|
end
|
@@ -105,7 +61,7 @@ end
|
|
105
61
|
def modify_finite args
|
106
62
|
table_name = args[:in_table]
|
107
63
|
if table_name.nil?
|
108
|
-
raise 'A table name must be specified for :in_table
|
64
|
+
raise 'A table name must be specified for :in_table.'
|
109
65
|
end
|
110
66
|
file_name = args[:from_file]
|
111
67
|
values = args[:values]
|
@@ -123,7 +79,7 @@ def modify_finite args
|
|
123
79
|
to_be_modified = to_be_modified.concat JSON.load open file_name
|
124
80
|
end
|
125
81
|
|
126
|
-
klass =
|
82
|
+
klass = get_table table_name
|
127
83
|
ActiveRecord::Base.transaction do
|
128
84
|
yield to_be_modified, klass, column_name
|
129
85
|
end
|
@@ -132,3 +88,7 @@ end
|
|
132
88
|
def default_column_name
|
133
89
|
:value
|
134
90
|
end
|
91
|
+
|
92
|
+
def as_class_name table_name
|
93
|
+
table_name.to_s.singularize.capitalize.to_sym
|
94
|
+
end
|
data/lib/get_table.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
def get_table sym, *args
|
4
|
+
class_name = as_class_name sym
|
5
|
+
class_not_defined = not(Object.const_defined? class_name)
|
6
|
+
should_force = args.any? {|x| x == :force}
|
7
|
+
if class_not_defined or should_force
|
8
|
+
anon = Class.new ActiveRecord::Base
|
9
|
+
Object.const_set class_name, anon
|
10
|
+
else
|
11
|
+
Object.const_get class_name
|
12
|
+
end
|
13
|
+
end
|
data/lib/master_table.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require_relative 'get_table.rb'
|
2
|
+
|
3
|
+
module MasterTable
|
4
|
+
def self.table_name
|
5
|
+
:active_finites
|
6
|
+
end
|
7
|
+
def self.column_name
|
8
|
+
:value
|
9
|
+
end
|
10
|
+
def self.table
|
11
|
+
get_table self.table_name
|
12
|
+
end
|
13
|
+
def self.all
|
14
|
+
if self.table.table_exists?
|
15
|
+
self.table
|
16
|
+
.all
|
17
|
+
.collect {|x| x.send default_column_name.to_s}
|
18
|
+
.collect {|x| get_table x}
|
19
|
+
else
|
20
|
+
[]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
def self.add table_name
|
24
|
+
if not self.table.table_exists?
|
25
|
+
ActiveRecord::Schema.define do
|
26
|
+
create_table MasterTable.table_name do |t|
|
27
|
+
t.string MasterTable.column_name, :null => false
|
28
|
+
end
|
29
|
+
add_index MasterTable.table_name, MasterTable.column_name, :unique => true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
if self.table.where(self.column_name => table_name).empty?
|
33
|
+
new_finite = self.table.new
|
34
|
+
new_finite.send self.column_name.to_s + '=', table_name
|
35
|
+
new_finite.save
|
36
|
+
end
|
37
|
+
end
|
38
|
+
def self.delete table_name
|
39
|
+
matches = self.table.where self.column_name => table_name
|
40
|
+
matches.each do |m|
|
41
|
+
self.table.destroy m
|
42
|
+
end
|
43
|
+
if self.table.count.eql? 0
|
44
|
+
ActiveRecord::Schema.define do
|
45
|
+
drop_table MasterTable.table_name
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/active_finite_spec.rb
CHANGED
@@ -8,25 +8,25 @@ def reconnect
|
|
8
8
|
:timeout => 5000)
|
9
9
|
end
|
10
10
|
|
11
|
-
describe '
|
11
|
+
describe 'get_table' do
|
12
12
|
before :each do
|
13
13
|
reconnect
|
14
14
|
end
|
15
15
|
it 'returns a class that is a child of active record base' do
|
16
|
-
|
16
|
+
get_table(:test).superclass.should eql ActiveRecord::Base
|
17
17
|
end
|
18
18
|
it 'brings a class into scope' do
|
19
|
-
|
19
|
+
get_table :spaces
|
20
20
|
Object.const_defined?(:Space).should be true
|
21
21
|
end
|
22
22
|
it 'will not redefine a previously defined constant' do
|
23
23
|
Define = true
|
24
|
-
|
24
|
+
get_table :defines
|
25
25
|
Define.should == true
|
26
26
|
end
|
27
27
|
it 'will redefine a previously defined constant with the force option' do
|
28
28
|
Anyway = true
|
29
|
-
|
29
|
+
get_table :anyways, :force
|
30
30
|
Anyway.should_not == true
|
31
31
|
end
|
32
32
|
end
|
@@ -45,12 +45,41 @@ describe 'all_finite_tables' do
|
|
45
45
|
delete_finites in_table: :things, values: ['1']
|
46
46
|
all_finite_tables.should == []
|
47
47
|
end
|
48
|
+
it 'returns a table that was used with add_finites' do
|
49
|
+
ActiveRecord::Schema.define do
|
50
|
+
create_table :things do |t|
|
51
|
+
t.string :column, :null => false
|
52
|
+
end
|
53
|
+
end
|
54
|
+
add_finites in_table: :things,
|
55
|
+
column_name: :column,
|
56
|
+
values: ['1']
|
57
|
+
|
58
|
+
all_finite_tables.should == [Thing]
|
59
|
+
end
|
60
|
+
it 'returns a table that was used with delete_finites' do
|
61
|
+
ActiveRecord::Schema.define do
|
62
|
+
create_table :things do |t|
|
63
|
+
t.string :column, :null => false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
t = Thing.new
|
67
|
+
t.column = "hi"
|
68
|
+
t.save
|
69
|
+
|
70
|
+
u = Thing.new
|
71
|
+
u.column = "bye"
|
72
|
+
u.save
|
73
|
+
|
74
|
+
delete_finites in_table: :things,
|
75
|
+
column_name: :column,
|
76
|
+
values: ["bye"]
|
77
|
+
|
78
|
+
all_finite_tables.should == [Thing]
|
79
|
+
end
|
48
80
|
end
|
49
81
|
|
50
82
|
describe 'as_class_name' do
|
51
|
-
before :each do
|
52
|
-
reconnect
|
53
|
-
end
|
54
83
|
it 'capitalizes its input' do
|
55
84
|
as_class_name(:as).should eql :A
|
56
85
|
end
|
@@ -105,13 +134,18 @@ describe 'add_finites' do
|
|
105
134
|
add_finites in_table: :wu_members, values: ['rza', 'gza', nil]
|
106
135
|
rescue
|
107
136
|
['rza', 'gza'].each do |w|
|
108
|
-
|
137
|
+
get_table(:wu_members).where(default_column_name => w).should nil
|
109
138
|
end
|
110
139
|
end
|
111
140
|
end
|
141
|
+
it 'will not add a duplicate value' do
|
142
|
+
lambda do
|
143
|
+
add_finites(in_table: :numbers, values: ['1','1'])
|
144
|
+
end.should raise_error ActiveRecord::RecordNotUnique
|
145
|
+
end
|
112
146
|
end
|
113
147
|
|
114
|
-
describe '
|
148
|
+
describe 'delete_finites' do
|
115
149
|
before :each do
|
116
150
|
reconnect
|
117
151
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,7 +2,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
2
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
3
|
require 'rspec'
|
4
4
|
require 'active_finite'
|
5
|
-
require '
|
5
|
+
require 'active_record'
|
6
6
|
|
7
7
|
# Requires supporting files with custom matchers and macros, etc,
|
8
8
|
# in ./support/ and its subdirectories.
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: active_finite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- me
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-23 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -96,6 +96,8 @@ files:
|
|
96
96
|
- Rakefile
|
97
97
|
- VERSION
|
98
98
|
- lib/active_finite.rb
|
99
|
+
- lib/get_table.rb
|
100
|
+
- lib/master_table.rb
|
99
101
|
- spec/active_finite_spec.rb
|
100
102
|
- spec/spec_helper.rb
|
101
103
|
- spec/villans.json
|
@@ -113,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
113
115
|
requirements:
|
114
116
|
- - ">="
|
115
117
|
- !ruby/object:Gem::Version
|
116
|
-
hash:
|
118
|
+
hash: 611629043
|
117
119
|
segments:
|
118
120
|
- 0
|
119
121
|
version: "0"
|