model-builder 1.0.2
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.ruby-version +1 -0
- data/Gemfile +3 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +51 -0
- data/Rakefile +40 -0
- data/lib/model_builder/class_builder.rb +47 -0
- data/lib/model_builder/version.rb +3 -0
- data/lib/model_builder.rb +75 -0
- data/model_builder.gemspec +26 -0
- data/spec/lib/model_builder/class_builder_spec.rb +29 -0
- data/spec/lib/model_builder_spec.rb +68 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/database_connection.rb +15 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: badad59df7fa705f88cd6c2a46528b6e2c7ba99a
|
4
|
+
data.tar.gz: 3854a69631e6c472fbe0785ab8e7b42eacff7d5d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 075df025384ee090201b909a2c36104df271279362df11ab132ab65215acf2063a65759ebe6c30a2320a30b1b7eb6a80d502532b4341b5e8be5ab6e4ca3b4599
|
7
|
+
data.tar.gz: f07b7c319e6b5af8e52519f6f247779266b113f06ecdafc2900a40cd273b1841c362b0c5ab47c6d1644c970f805b785315f32d8cdbd03bc418de7d0abd9eb17b
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.2.3@model-builder
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= Model Builder
|
2
|
+
|
3
|
+
Build active record models on the fly.
|
4
|
+
Handy during tests creation, when the development is not related to business intelligence (and should not reference it).
|
5
|
+
|
6
|
+
Something like this:
|
7
|
+
|
8
|
+
# migration
|
9
|
+
|
10
|
+
class CreatePlayers < ActiveRecord::Migration
|
11
|
+
def change
|
12
|
+
create_table :players do |t|
|
13
|
+
t.string :name
|
14
|
+
t.integer :age, default: 18
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# model
|
20
|
+
|
21
|
+
class Player < ActiveRecord::Base
|
22
|
+
validates :name, :age, presence: true
|
23
|
+
validates :age, numericality: true
|
24
|
+
end
|
25
|
+
|
26
|
+
Can be done at runtime:
|
27
|
+
|
28
|
+
options = {
|
29
|
+
attributes: {
|
30
|
+
name: :string,
|
31
|
+
age: { type: :integer, default: 18 }
|
32
|
+
},
|
33
|
+
validates: [
|
34
|
+
[:name, :age, presence: true],
|
35
|
+
[:age, numericality: true]
|
36
|
+
]
|
37
|
+
}
|
38
|
+
|
39
|
+
ModelBuilder.build 'Player', options
|
40
|
+
|
41
|
+
After all, you can drop tables with:
|
42
|
+
|
43
|
+
ModelBuilder.clean
|
44
|
+
|
45
|
+
Simple classes are welcome too:
|
46
|
+
|
47
|
+
ModelBuilder::ClassBuilder.build 'Duck', superclass: 'Animal', accessors: %w(age size)
|
48
|
+
|
49
|
+
If you want your object to quack like active record, see this:
|
50
|
+
|
51
|
+
* https://github.com/makandra/active_type
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'ModelBuilder'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
Bundler::GemHelper.install_tasks
|
24
|
+
|
25
|
+
require 'rake/testtask'
|
26
|
+
|
27
|
+
Rake::TestTask.new(:test) do |t|
|
28
|
+
t.libs << 'lib'
|
29
|
+
t.libs << 'test'
|
30
|
+
t.pattern = 'test/**/*_test.rb'
|
31
|
+
t.verbose = false
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'rspec/core/rake_task'
|
35
|
+
|
36
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
37
|
+
t.rspec_opts = %w(--color)
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ModelBuilder
|
2
|
+
module ClassBuilder
|
3
|
+
|
4
|
+
@@dynamic_classes ||= []
|
5
|
+
|
6
|
+
def self.build(name, opts={})
|
7
|
+
return name.constantize if Object.const_defined? name
|
8
|
+
|
9
|
+
klass = get_class_from_options opts
|
10
|
+
Object.const_set name, klass
|
11
|
+
|
12
|
+
add_class klass
|
13
|
+
create_accessors klass, opts[:accessors]
|
14
|
+
|
15
|
+
klass
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.get_class_from_options(opts)
|
19
|
+
superclass = get_superclass_from_options opts
|
20
|
+
superclass.nil? ? Class.new : Class.new(superclass)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.get_superclass_from_options(opts)
|
24
|
+
superclass = opts[:superclass]
|
25
|
+
superclass.is_a?(String) ? superclass.constantize : superclass
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.list
|
29
|
+
@@dynamic_classes
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.add_class(klass)
|
33
|
+
@@dynamic_classes << klass
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.create_accessors(klass, accessors=[])
|
37
|
+
return if accessors.nil?
|
38
|
+
accessors = [accessors] unless accessors.is_a? Array
|
39
|
+
accessors.each { |accessor| create_accessor(klass, accessor) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.create_accessor(klass, accessor)
|
43
|
+
klass.send 'attr_accessor', accessor unless accessor.nil?
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
Dir["#{File.dirname(__FILE__)}/**/*.rb"].each {|file| require file }
|
2
|
+
|
3
|
+
module ModelBuilder
|
4
|
+
|
5
|
+
@@dynamic_models ||= []
|
6
|
+
|
7
|
+
def self.build(name, opts={})
|
8
|
+
opts.reverse_merge! get_default_options
|
9
|
+
|
10
|
+
already_exists = Object.const_defined? name
|
11
|
+
klass = ClassBuilder.build name, opts
|
12
|
+
|
13
|
+
unless already_exists
|
14
|
+
create_table klass, opts
|
15
|
+
define_validations klass, opts[:validates]
|
16
|
+
end
|
17
|
+
|
18
|
+
klass
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.get_default_options
|
22
|
+
{
|
23
|
+
superclass: ::ActiveRecord::Base,
|
24
|
+
attributes: {}
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.create_table(klass, opts)
|
29
|
+
ActiveRecord::Migration.create_table(table_name_for(klass)) do |migration|
|
30
|
+
create_attributes(migration, opts[:attributes])
|
31
|
+
end
|
32
|
+
|
33
|
+
@@dynamic_models << klass
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.table_name_for(klass)
|
37
|
+
klass.to_s.tableize
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.create_attributes(migration, attributes={})
|
41
|
+
attributes.each_pair do |key, value|
|
42
|
+
create_attribute(migration, key, value)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.create_attribute(migration, key, opts)
|
47
|
+
if opts.is_a?(Hash)
|
48
|
+
opts = opts.clone
|
49
|
+
type = opts.delete :type
|
50
|
+
else
|
51
|
+
type = opts
|
52
|
+
opts = {}
|
53
|
+
end
|
54
|
+
migration.send(type, key, opts)
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.define_validations(klass, validations)
|
58
|
+
return if validations.nil? or !validations.kind_of?(Array) or validations.empty?
|
59
|
+
validations = [validations] unless validations.first.kind_of? Array
|
60
|
+
validations.each {|v| klass.validates *v}
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.clean
|
64
|
+
list.map {|c| drop_table c }
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.list
|
68
|
+
@@dynamic_models
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.drop_table(klass)
|
72
|
+
ActiveRecord::Migration.drop_table(table_name_for(klass))
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'model_builder/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'model-builder'
|
7
|
+
s.version = ModelBuilder::VERSION
|
8
|
+
s.authors = ['r4z3c']
|
9
|
+
s.email = ['r4z3c.43@gmail.com']
|
10
|
+
s.homepage = 'https://github.com/r4z3c/model-builder.git'
|
11
|
+
s.summary = 'Active record runtime model builder'
|
12
|
+
s.description = 'Create active record models on the fly'
|
13
|
+
s.licenses = %w(MIT)
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
17
|
+
|
18
|
+
s.require_paths = %w(lib)
|
19
|
+
|
20
|
+
s.add_dependency 'bundler', '~>1'
|
21
|
+
s.add_dependency 'activerecord', '~>4'
|
22
|
+
|
23
|
+
s.add_development_dependency 'sqlite3', '~>1'
|
24
|
+
s.add_development_dependency 'rspec', '~>3'
|
25
|
+
s.add_development_dependency 'simplecov', '~>0'
|
26
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ModelBuilder::ClassBuilder do
|
4
|
+
|
5
|
+
let(:builder) { ModelBuilder::ClassBuilder }
|
6
|
+
let(:name) { 'ClassBuilderTest' }
|
7
|
+
let(:options) { { superclass: Array, accessors: %w(a1 a2) } }
|
8
|
+
let(:constant) { name.constantize }
|
9
|
+
|
10
|
+
subject { builder.build name, options }
|
11
|
+
|
12
|
+
it { is_expected.to eq constant }
|
13
|
+
it { expect(builder.list).to include constant }
|
14
|
+
|
15
|
+
context 'accessors validations' do
|
16
|
+
|
17
|
+
before { subject }
|
18
|
+
|
19
|
+
it { expect(constant.new).to respond_to :a1 }
|
20
|
+
it { expect(constant.new).to respond_to :a2 }
|
21
|
+
it { expect(constant.new).to_not respond_to :a3 }
|
22
|
+
|
23
|
+
it { expect(constant.new).to respond_to :a1= }
|
24
|
+
it { expect(constant.new).to respond_to :a2= }
|
25
|
+
it { expect(constant.new).to_not respond_to :a3= }
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/database_connection'
|
3
|
+
|
4
|
+
Spec::Support::DatabaseConnection.establish_sqlite_connection
|
5
|
+
|
6
|
+
describe ModelBuilder do
|
7
|
+
|
8
|
+
let(:builder) { ModelBuilder }
|
9
|
+
let(:name) { 'TestModel' }
|
10
|
+
let(:constant) { name.constantize }
|
11
|
+
let(:default_age) { 17 }
|
12
|
+
let(:options) do
|
13
|
+
{
|
14
|
+
attributes: {
|
15
|
+
name: :string,
|
16
|
+
age: {
|
17
|
+
type: :integer,
|
18
|
+
default: default_age
|
19
|
+
}
|
20
|
+
},
|
21
|
+
validates: [
|
22
|
+
[:name, :age, presence: true],
|
23
|
+
[:age, numericality: true]
|
24
|
+
]
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
subject { builder.build name, options }
|
29
|
+
|
30
|
+
it { is_expected.to eq constant }
|
31
|
+
it { expect(builder.list).to include constant }
|
32
|
+
|
33
|
+
context 'options validations' do
|
34
|
+
|
35
|
+
before { subject }
|
36
|
+
|
37
|
+
subject(:instance) { constant.new }
|
38
|
+
|
39
|
+
context 'attributes validations' do
|
40
|
+
|
41
|
+
it { is_expected.to respond_to :name }
|
42
|
+
it { is_expected.to respond_to :age }
|
43
|
+
|
44
|
+
it { is_expected.to respond_to :name= }
|
45
|
+
it { is_expected.to respond_to :age= }
|
46
|
+
|
47
|
+
it { expect(instance.age).to eq default_age }
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'validations validations' do
|
52
|
+
|
53
|
+
before do
|
54
|
+
instance.age = 'noop'
|
55
|
+
instance.valid?
|
56
|
+
end
|
57
|
+
|
58
|
+
it { expect(instance.valid?).to be false }
|
59
|
+
it { expect(instance.errors.messages.keys).to include :name }
|
60
|
+
it { expect(instance.errors.messages.keys).to include :age }
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
after(:all) { ModelBuilder.clean }
|
67
|
+
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'model_builder'
|
2
|
+
|
3
|
+
module Spec
|
4
|
+
module Support
|
5
|
+
module DatabaseConnection
|
6
|
+
|
7
|
+
def self.establish_sqlite_connection
|
8
|
+
database = "#{File.dirname(__FILE__)}/../../tmp/model_builder-test.sqlite3"
|
9
|
+
|
10
|
+
::ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: database
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: model-builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- r4z3c
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Create active record models on the fly
|
84
|
+
email:
|
85
|
+
- r4z3c.43@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".ruby-version"
|
92
|
+
- Gemfile
|
93
|
+
- MIT-LICENSE
|
94
|
+
- README.rdoc
|
95
|
+
- Rakefile
|
96
|
+
- lib/model_builder.rb
|
97
|
+
- lib/model_builder/class_builder.rb
|
98
|
+
- lib/model_builder/version.rb
|
99
|
+
- model_builder.gemspec
|
100
|
+
- spec/lib/model_builder/class_builder_spec.rb
|
101
|
+
- spec/lib/model_builder_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
- spec/support/database_connection.rb
|
104
|
+
homepage: https://github.com/r4z3c/model-builder.git
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.8
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Active record runtime model builder
|
128
|
+
test_files:
|
129
|
+
- spec/lib/model_builder/class_builder_spec.rb
|
130
|
+
- spec/lib/model_builder_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/support/database_connection.rb
|