kit 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ module KitDBSupport
2
+
3
+ # Create a database.
4
+ # @raise RuntimeError This will raise an exception if databse exists.
5
+ # @param [Hash] config ActiveRecord::Base database configuration
6
+ def self.create! config
7
+ case config[:adapter]
8
+ when 'sqlite3'
9
+ db_file = config[:database]
10
+ raise RuntimeError, "Database file #{db_file} exists." if File.exists? db_file
11
+ SQLite3::Database.new config[:database]
12
+ else
13
+ raise RuntimeError, "Creating database with adapter #{config[:adapter]} not supported."
14
+ end
15
+ end
16
+
17
+ # (see #create!)
18
+ def self.create *args
19
+ begin
20
+ create! *args
21
+ rescue RuntimeError, /^Database file .+ exists.$/
22
+ end
23
+ end
24
+
25
+ # Destory a database.
26
+ # @raise RuntimeError This will raise an exception if databse does not exist.
27
+ # @param (see create!)
28
+ def self.destroy! config
29
+ case config[:adapter]
30
+ when 'sqlite3'
31
+ db_file = config[:database]
32
+ raise RuntimeError, "Database file #{db_file} does not exist." unless File.exists? db_file
33
+ File.unlink db_file
34
+ else
35
+ raise RuntimeError, "Destroying database with adapter #{config[:adapter]} not supported."
36
+ end
37
+ end
38
+
39
+ # (see #destroy)
40
+ def self.destroy *args
41
+ begin
42
+ destroy! *args
43
+ rescue RuntimeError, /^Database file .+ does not exist.$/
44
+ end
45
+ end
46
+
47
+ # Establishes an ActiveRecord::Base connection.
48
+ # @param [Hash] config settings for ActiveRecord::Base
49
+ def self.connect config
50
+ ActiveRecord::Base.establish_connection config
51
+ end
52
+
53
+ # Migrate up or down a given number of migrations.
54
+ # @param [String] path location of migration files
55
+ # @param [Symbol] direction
56
+ # @param [Integer] steps
57
+ def self.migrate path, direction = nil, steps = 1
58
+ if direction.nil?
59
+ ActiveRecord::Migrator.migrate(path, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
60
+ else
61
+ ActiveRecord::Migrator.send direction, path, steps
62
+ end
63
+ end
64
+
65
+ # Migrate to a specfic migration.
66
+ # @param path (see #migrate)
67
+ # @param [Integer] version the migration number
68
+ def self.migrate_to path, version
69
+ ActiveRecord::Migrator.migrate(path, version)
70
+ end
71
+ end
@@ -0,0 +1,34 @@
1
+ class Kit::Bit < ActiveRecord::Base
2
+
3
+ belongs_to :group
4
+ has_many :permissions
5
+ has_many :users, :through => :permissions
6
+
7
+ after_initialize do
8
+ if self.group.nil?
9
+ self.extend KitActionsDefault
10
+ else
11
+ mod = "KitActions#{self.group.name.gsub(' ', '_').camelize}"
12
+ self.extend Kernel.const_get(mod) if Kernel.const_defined? mod
13
+ end
14
+ end
15
+
16
+ class Job
17
+
18
+ def initialize *args
19
+ if args[0].is_a? Hash
20
+ hash = args[0]
21
+ args = [ hash[:config_file], hash[:bit_id], hash[:action], *hash[:args] ]
22
+ end
23
+ @config_file = args[0]
24
+ @bit_id = args[1]
25
+ @action = args[2]
26
+ @args = *args[3..-1]
27
+ end
28
+
29
+ def perform
30
+ Kit.open @config_file
31
+ Kit::Bit.find(@bit_id).send(@action, *@args)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ class Kit::Group < ActiveRecord::Base
2
+
3
+ has_many :bits
4
+
5
+ end
@@ -0,0 +1,6 @@
1
+ class Kit::Permission < ActiveRecord::Base
2
+
3
+ belongs_to :user
4
+ belongs_to :bit
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ class Kit::User < ActiveRecord::Base
2
+
3
+ has_many :permissions
4
+ has_many :bits, :through => :permissions
5
+
6
+ end
@@ -0,0 +1,3 @@
1
+ require 'kit/rake/admin/database'
2
+ require 'kit/rake/admin/manage'
3
+ require 'kit/rake/admin/make'
@@ -0,0 +1,28 @@
1
+ namespace 'db' do
2
+
3
+ kit = Kit.new 'config.yml'
4
+
5
+ task :create do
6
+ kit.db_create
7
+ end
8
+
9
+ task :destroy do
10
+ kit.db_destroy
11
+ end
12
+
13
+ task :reset => [:destroy, :create, :migrate] do
14
+ end
15
+
16
+ task :environment do
17
+ kit.db_connect
18
+ end
19
+
20
+ task :migrate, [:direction, :steps] => [:create, :environment] do |_, args|
21
+ args.with_defaults(direction: nil, steps: 1)
22
+ if args[:direction].nil?
23
+ kit.db_migrate
24
+ else
25
+ kit.db_migrate args[:direction], args[:steps]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ namespace 'mk' do
2
+
3
+ def make_class_template file_name, class_line
4
+ unless File.exists? file_name
5
+ f = File.new file_name, 'w+'
6
+ f << class_line
7
+ f << "\nend"
8
+ f.close
9
+ end
10
+ end
11
+
12
+ task :migration, :name do |_, args|
13
+ make_class_template "migrations/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_#{args[:name]}.rb", "class #{args[:name].camelize} < ActiveRecord::Migration"
14
+ end
15
+
16
+ task :model, :name do |_, args|
17
+ make_class_template "models/#{args[:name]}.rb", "class Kit::#{args[:name].camelize} < ActiveRecord::Base"
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ namespace 'add' do
2
+
3
+ task :group, :name do |_, args|
4
+ kit = Kit.open 'config.yml'
5
+ Kit::Group.create :name => args[:name].gsub('_', ' ')
6
+ make_class_template "actions/#{args[:name]}.rb", "module KitActions#{args[:name].camelize}"
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ class Kit
2
+ # Version number.
3
+ VERSION = '1.0.0'
4
+ end
@@ -0,0 +1,57 @@
1
+ require 'kit'
2
+
3
+ describe Kit::Bit do
4
+
5
+ before :all do
6
+ @config_file = File.expand_path '../../test_kit/config.yml', __FILE__
7
+ Kit.new(@config_file).db_create.db_connect.db_migrate
8
+ end
9
+
10
+ after :all do
11
+ Kit.new(@config_file).db_destroy
12
+ end
13
+
14
+ before :each do
15
+ Kit.open @config_file
16
+ end
17
+
18
+ describe Kit::Bit::Job do
19
+
20
+ [:args, :hash].each do |type|
21
+
22
+ describe ".perform" do
23
+
24
+ context "given #{type}" do
25
+
26
+ config = File.expand_path '../../test_kit/config.yml', __FILE__
27
+ if type == :args
28
+ subject { Kit::Bit::Job.new config, 1, :the_action, :arg_1, :arg_2 }
29
+ else
30
+ hash = { config_file: config, bit_id: 1, action: :the_action, args: [:arg_1, :arg_2] }
31
+ subject { Kit::Bit::Job.new hash }
32
+ end
33
+
34
+ before :each do
35
+ @bit = mock('Kit::Bit', :id => 1, :the_action => nil)
36
+ Kit::Bit.stub(:find).and_return(@bit)
37
+ end
38
+
39
+ it "opens the kit" do
40
+ Kit.should_receive(:open).with(@config_file)
41
+ subject.perform
42
+ end
43
+
44
+ it "looks for the bit" do
45
+ Kit::Bit.should_receive(:find).with(1)
46
+ subject.perform
47
+ end
48
+
49
+ it "runs the action on the bit" do
50
+ @bit.should_receive(:the_action).with(:arg_1, :arg_2)
51
+ subject.perform
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,110 @@
1
+ require 'kit'
2
+
3
+ describe KitDBSupport do
4
+
5
+ before :all do
6
+ @config = {}
7
+ @config[:sqlite3] = { adapter: 'sqlite3', database: 'spec_db.sqlite3' }
8
+ @sqlite3 = @config[:sqlite3]
9
+ end
10
+
11
+ describe "create!" do
12
+
13
+ it "raises error if adapter not supported" do
14
+ expect { KitDBSupport::create!(:adapter => 'bad_adapter') }.should raise_error RuntimeError, /not supported/
15
+ end
16
+
17
+ context "adapter is sqlite3" do
18
+
19
+ it "creates the sqlite3 database file" do
20
+ File.stub(:exists?).with(@sqlite3[:database]).and_return(false)
21
+ SQLite3::Database.should_receive(:new).with(@sqlite3[:database])
22
+ KitDBSupport::create! @config[:sqlite3]
23
+ end
24
+
25
+ it "raises error if sqlite3 database file exists" do
26
+ File.stub(:exists?).with(@sqlite3[:database]).and_return(true)
27
+ expect { KitDBSupport::create! @config[:sqlite3] }.should raise_error RuntimeError, /exists/
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "create" do
33
+ it "calls create!" do
34
+ KitDBSupport.should_receive(:create!)
35
+ KitDBSupport::create
36
+ end
37
+
38
+ it "does not raise error if sqlite3 database file exists" do
39
+ File.stub(:exists?).with(@sqlite3[:database]).and_return(true)
40
+ expect { KitDBSupport::create @config[:sqlite3] }.should_not raise_error RuntimeError, /exists/
41
+ end
42
+ end
43
+
44
+ describe "destroy!" do
45
+
46
+ it "raises error if adapter not supported" do
47
+ expect { KitDBSupport::destroy!(:adapter => 'bad_adapter') }.should raise_error RuntimeError, /not supported/
48
+ end
49
+
50
+ context "adapter is sqlite3" do
51
+
52
+ it "unlinks the sqlite3 databasefile " do
53
+ File.stub(:exists?).with(@sqlite3[:database]).and_return(true)
54
+ File.should_receive(:unlink).with( @sqlite3[:database] )
55
+ KitDBSupport::destroy! @config[:sqlite3]
56
+ end
57
+
58
+ it "raises error if sqlite3 database file does not exist" do
59
+ File.stub(:exists?).with(@sqlite3[:database]).and_return(false)
60
+ expect { KitDBSupport::destroy! @config[:sqlite3] }.should raise_error RuntimeError, /does not exist/
61
+ end
62
+ end
63
+ end
64
+
65
+ describe "destroy" do
66
+ it "calls destroy!" do
67
+ KitDBSupport.should_receive(:destroy!)
68
+ KitDBSupport::destroy
69
+ end
70
+
71
+ it "does not raise error if sqlite3 database file does not exist" do
72
+ File.stub(:exists?).with(@sqlite3[:database]).and_return(false)
73
+ expect { KitDBSupport::destroy @config[:sqlite3] }.should_not raise_error RuntimeError, /does not exist/
74
+ end
75
+ end
76
+
77
+ describe "connect" do
78
+
79
+ it "makes active record establish a connection" do
80
+ ActiveRecord::Base.should_receive(:establish_connection).with(@config[:sqlite3])
81
+ KitDBSupport::connect @config[:sqlite3]
82
+ end
83
+ end
84
+
85
+ describe "migrate" do
86
+
87
+ it "migrates to latest version when called with no arguments" do
88
+ ActiveRecord::Migrator.should_receive(:migrate)
89
+ KitDBSupport::migrate 'migrations'
90
+ end
91
+
92
+ it "migrates one step in a given direction" do
93
+ ActiveRecord::Migrator.should_receive(:up).with('migrations', 1)
94
+ KitDBSupport::migrate 'migrations', :up
95
+ end
96
+
97
+ it "migrates many steps in a given direction" do
98
+ ActiveRecord::Migrator.should_receive(:up).with('migrations', 3)
99
+ KitDBSupport::migrate 'migrations', :up, 3
100
+ end
101
+ end
102
+
103
+ describe "migrate_to" do
104
+
105
+ it "migrates to a specific migration" do
106
+ ActiveRecord::Migrator.should_receive(:migrate).with('migrations', 001)
107
+ KitDBSupport::migrate_to 'migrations', 001
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,56 @@
1
+ require 'kit'
2
+
3
+ describe Kit do
4
+
5
+ before :all do
6
+ @kit_path = File.expand_path '../../test_kit', __FILE__
7
+ @config_file = File.expand_path '../../test_kit/config.yml', __FILE__
8
+ @config = YAML.load File.read("#{@kit_path}/config.yml")
9
+ end
10
+
11
+ subject { Kit.new @config_file }
12
+
13
+ describe ".path" do
14
+
15
+ it "determines the correct path to the kit" do
16
+ subject.path.should == @kit_path
17
+ end
18
+ end
19
+
20
+ describe ".config" do
21
+
22
+ it "loads the config file" do
23
+ subject.config.should == @config
24
+ end
25
+ end
26
+
27
+ describe ".open" do
28
+
29
+ it "creates a new kit and opens a database connections" do
30
+ Kit.any_instance.should_receive(:db_connect)
31
+ Kit.open @config_file
32
+ end
33
+ end
34
+
35
+ db_actions = {}
36
+ db_actions[:destroy] = [Hash.new]
37
+ db_actions[:create] = [Hash.new]
38
+ db_actions[:migrate] = [String.new, Hash.new, 0]
39
+ db_actions[:migrate_to] = [String.new, 0]
40
+
41
+ db_actions.each do |action, args|
42
+
43
+ describe ".db_#{action}" do
44
+
45
+ it "calls KitDBSupport::#{action}" do
46
+ KitDBSupport.should_receive(action).with( *( args.map { |x| kind_of(x.class) } ) )
47
+ subject.send "db_#{action}", *args[1..-1]
48
+ end
49
+
50
+ it "returns the instance of Kit" do
51
+ KitDBSupport.stub action
52
+ subject.send("db_#{action}", *args[1..-1]).should equal subject
53
+ end
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,32 +9,119 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-25 00:00:00.000000000 Z
12
+ date: 2012-05-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: sqlite3
16
- requirement: &75741220 !ruby/object:Gem::Requirement
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.3.3
21
+ version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *75741220
25
- description: ! "\t\tKit is a framework for making simple management tools called kits.\n"
26
- email:
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: git
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Extendable tool to manage site development and more.
95
+ email:
96
+ - razorx@evansosenko.com
27
97
  executables: []
28
98
  extensions: []
29
99
  extra_rdoc_files: []
30
100
  files:
31
- - lib/kit/db_sqlite3.rb
32
- - lib/kit/bit.rb
33
- - lib/kit.rb
101
+ - .gitignore
102
+ - .rspec
103
+ - .yardopts
34
104
  - CHANGELOG.rdoc
105
+ - Gemfile
106
+ - LICENSE.txt
35
107
  - README.rdoc
36
- - LICENCE.txt
37
- homepage:
108
+ - Rakefile
109
+ - kit.gemspec
110
+ - lib/kit.rb
111
+ - lib/kit/db_support.rb
112
+ - lib/kit/models/bit.rb
113
+ - lib/kit/models/group.rb
114
+ - lib/kit/models/permission.rb
115
+ - lib/kit/models/user.rb
116
+ - lib/kit/rake/admin.rb
117
+ - lib/kit/rake/admin/database.rb
118
+ - lib/kit/rake/admin/make.rb
119
+ - lib/kit/rake/admin/manage.rb
120
+ - lib/kit/version.rb
121
+ - spec/bit_spec.rb
122
+ - spec/kit_db_support_spec.rb
123
+ - spec/kit_spec.rb
124
+ homepage: http://evansosenko.com
38
125
  licenses:
39
126
  - MIT
40
127
  post_install_message:
@@ -46,19 +133,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
133
  requirements:
47
134
  - - ! '>='
48
135
  - !ruby/object:Gem::Version
49
- version: 1.9.2
136
+ version: '0'
50
137
  required_rubygems_version: !ruby/object:Gem::Requirement
51
138
  none: false
52
139
  requirements:
53
140
  - - ! '>='
54
141
  - !ruby/object:Gem::Version
55
142
  version: '0'
56
- requirements:
57
- - SQLite3
143
+ requirements: []
58
144
  rubyforge_project:
59
- rubygems_version: 1.8.11
145
+ rubygems_version: 1.8.23
60
146
  signing_key:
61
147
  specification_version: 3
62
- summary: Extendable tool to manage site development and more.
63
- test_files: []
148
+ summary: Kit is a framework for making simple management tools called kits.
149
+ test_files:
150
+ - spec/bit_spec.rb
151
+ - spec/kit_db_support_spec.rb
152
+ - spec/kit_spec.rb
64
153
  has_rdoc: