magic_models 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 223608e9416366b9e8013ff21a3f56040fa477c0
4
+ data.tar.gz: 799ada84ee5ff4bc85208bce614dbafc68af3c78
5
+ SHA512:
6
+ metadata.gz: 2943748c0e52a71e6a45ab3ae8fd580f20f437b89da1a374dc7e63410b158680a484d1730f88dfc195804a8639372a08119de7f004311c0dac30cc36767304e5
7
+ data.tar.gz: 2771b0e4b8d2bd5eb80bb64e1dfd401223a14296e75beac771b1aa0f4338d68e9481eab5f762d9e85548b3c642ea26c80fd25bf77b55f31bf0319c023ea51b95
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in magic_models.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
7
+ gem 'pg'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ray Zane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,94 @@
1
+ # MagicModels
2
+
3
+ Have an existing database? MagicModels can generate models on the fly using your existing database connection.
4
+
5
+ If you have a table called `people`, just call `MagicModels.define`. Now, you have a model called `Person`. Boom!
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'magic_models'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install magic_models
22
+
23
+ ## Usage
24
+
25
+ MagicModels has two main functions:
26
+
27
+ + **dump**: Generate files for your Active Record models.
28
+ + **define**: Declare models at runtime without creating any files.
29
+
30
+ First, you'll need to point your app at an existing database. To do that, just edit your `config/database.yml`.
31
+
32
+ To define your models at runtime, just put this line in an initializer:
33
+
34
+ ```ruby
35
+ MagicModels.define
36
+ ```
37
+
38
+ If you want to dump your models to files, run this shell command:
39
+
40
+ ```shell
41
+ $ rails runner 'MagicModels.dump'
42
+ ```
43
+
44
+ ## Configuration
45
+
46
+ MagicModels also offers some configuration settings. `MagicModels.define` and `MagicModels.dump` accept a block:
47
+
48
+ ```ruby
49
+ MagicModels.define do |config|
50
+ end
51
+
52
+ MagicModels.dump do |config|
53
+ end
54
+ ```
55
+
56
+ #### Shared settings
57
+
58
+ ```ruby
59
+ # Ignore certian tables (default: schema_migrations, ar_internal_metadata)
60
+ config.exclude 'some_table', 'some_other_table'
61
+
62
+ # Change the connection (default: ActiveRecord::Base.connection)
63
+ config.connection = SomeOtherModel.connection
64
+
65
+ # Declare a different class to inherit from (default: ActiveRecord::Base)
66
+ config.base_class = 'SomeOtherAncestor'
67
+ ```
68
+
69
+ #### Dump-specific settings
70
+
71
+ ```ruby
72
+ # Change the directory where the model files will be created (default: app/models)
73
+ config.destination = '/path/to/your/models/dir'
74
+ ```
75
+
76
+ #### Define-specific settings
77
+
78
+ ```ruby
79
+ # Change the namespace to create the models under. For example, if you had a table
80
+ # named 'foos', this configuration would create `SomeModule::Foo` instead of `Foo`.
81
+ config.namespace = SomeModule
82
+ ```
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rzane/magic_models.
87
+
88
+ ## Props
89
+
90
+ + [Dr. Nic's Magic Models](https://github.com/drnic/dr-nic-magic-models)
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'magic_models'
5
+ require 'pry'
6
+
7
+ require_relative '../spec/schema'
8
+
9
+ Pry.start
10
+
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ require 'magic_models/version'
2
+ require 'magic_models/schema'
3
+
4
+ module MagicModels
5
+ class << self
6
+ def define
7
+ schema = Schema::Define.new
8
+ yield schema if block_given?
9
+ schema.models.map(&:define)
10
+ end
11
+
12
+ def dump(&block)
13
+ schema = Schema::Dump.new
14
+ yield schema if block_given?
15
+ schema.models.map(&:write)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ module MagicModels
2
+ module Associations
3
+ class BelongsTo
4
+ attr_reader :foreign_key, :primary_key
5
+
6
+ def initialize(foreign_key)
7
+ @table_name = foreign_key.to_table
8
+ @primary_key = foreign_key.options[:primary_key]
9
+ @foreign_key = foreign_key.options[:column]
10
+ end
11
+
12
+ def name
13
+ @foreign_key.sub(/_id$/, '').singularize
14
+ end
15
+
16
+ def macro
17
+ 'belongs_to'
18
+ end
19
+
20
+ def class_name
21
+ @table_name.singularize.camelize
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,58 @@
1
+ require 'erb'
2
+ require 'magic_models/associations'
3
+
4
+ module MagicModels
5
+ class Model
6
+ attr_reader :schema, :name
7
+
8
+ delegate :base_class, to: :schema
9
+ delegate :constantize, to: :model_name
10
+
11
+ def initialize(schema, name)
12
+ @schema = schema
13
+ @name = name
14
+ end
15
+
16
+ def filename
17
+ File.join(schema.destination, "#{model_name.underscore}.rb")
18
+ end
19
+
20
+ def render
21
+ ERB.new(File.read(template)).result(binding)
22
+ end
23
+
24
+ def write
25
+ File.open(filename, 'w') { |f| f.write render }
26
+ filename
27
+ end
28
+
29
+ def define
30
+ schema.evaluate(render)
31
+ constantize
32
+ end
33
+
34
+ def primary_key
35
+ @primary_key ||= schema.primary_key(name)
36
+ end
37
+
38
+ def belongs_to
39
+ schema.foreign_keys(name).map do |fk|
40
+ Associations::BelongsTo.new(fk)
41
+ end
42
+ end
43
+
44
+ # Currently, we only support belongs_to associations. I don't think
45
+ # there is a way to differentiate a has_one from a has_many.
46
+ alias associations belongs_to
47
+
48
+ def model_name
49
+ name.singularize.camelize
50
+ end
51
+
52
+ private
53
+
54
+ def template
55
+ File.expand_path('../../templates/model.erb', __FILE__)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,52 @@
1
+ require 'active_record'
2
+ require 'magic_models/model'
3
+
4
+ module MagicModels
5
+ module Schema
6
+ class Base
7
+ attr_accessor :base_class, :connection
8
+ delegate :primary_key, :foreign_keys, to: :connection
9
+
10
+ def initialize
11
+ @base_class = 'ActiveRecord::Base'
12
+ @connection = ActiveRecord::Base.connection
13
+ @exclude = ['schema_migrations', 'ar_internal_metadata']
14
+ end
15
+
16
+ def exclude(*tables)
17
+ @exclude += tables.flatten
18
+ end
19
+
20
+ def data_sources
21
+ connection.data_sources - @exclude
22
+ end
23
+
24
+ def models
25
+ data_sources.map do |name|
26
+ Model.new(self, name)
27
+ end
28
+ end
29
+ end
30
+
31
+ class Define < Base
32
+ attr_accessor :namespace
33
+
34
+ def evaluate(*args, &block)
35
+ if namespace
36
+ namespace.module_eval(*args, &block)
37
+ else
38
+ TOPLEVEL_BINDING.eval(*args, &block)
39
+ end
40
+ end
41
+ end
42
+
43
+ class Dump < Base
44
+ attr_accessor :destination
45
+
46
+ def initialize
47
+ super
48
+ @destination = File.join(Dir.pwd, 'app', 'models')
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module MagicModels
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ class <%= model_name %> < <%= base_class %>
2
+ self.table_name = <%= name.inspect %>
3
+ self.primary_key = :<%= primary_key %>
4
+ <% associations.each do |association| %>
5
+ <%= association.macro %> :<%= association.name %>,
6
+ class_name: <%= association.class_name.inspect %>,
7
+ foreign_key: :<%= association.foreign_key %>,
8
+ primary_key: :<%= association.primary_key %>
9
+ <% end %>
10
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'magic_models/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "magic_models"
8
+ spec.version = MagicModels::VERSION
9
+ spec.authors = ["Ray Zane"]
10
+ spec.email = ["ray@promptworks.com"]
11
+
12
+ spec.summary = %q{Generate models at runtime from a database connection!}
13
+ spec.homepage = "https://github.com/rzane/magic_models"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency 'activerecord'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic_models
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ray Zane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
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.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - ray@promptworks.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/magic_models.rb
86
+ - lib/magic_models/associations.rb
87
+ - lib/magic_models/model.rb
88
+ - lib/magic_models/schema.rb
89
+ - lib/magic_models/version.rb
90
+ - lib/templates/model.erb
91
+ - magic_models.gemspec
92
+ homepage: https://github.com/rzane/magic_models
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.10
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Generate models at runtime from a database connection!
116
+ test_files: []