minirails 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 771b7f597898182a1fa53aface49303f6eb3185f
4
- data.tar.gz: af5236b60849641f505d8a2a81fceccdfa12896f
3
+ metadata.gz: 2cb8f20cc209da625d6cf79ab1332e44f38451a3
4
+ data.tar.gz: cc5c4ca139366812a8f6db09a6fcc63f1092e192
5
5
  SHA512:
6
- metadata.gz: 89cb680f0c831cabb870674a5ecf0a4bf67ddd90c22b283168bcc192681e121ab79a1e74c4bff83082a6d30af82199106cc4e7b72057175db4e951eb4df1db91
7
- data.tar.gz: 37fe4d4613b58e30f15e22a24b39aaff7b5503940ad222f6407cbd27c63c2327334b7849846ad20b7ecfb7ab54d21a44159158ee5fe58354a4fbe0f84106dde4
6
+ metadata.gz: 555a831b642c9459d4bdd7536cc1149d86762fbb6b2f8cba23716bd43021ac19370a0c4a9b568c2cf712ead864231a6bfe5fdeb2c02bb7ee171ae624aeeada42
7
+ data.tar.gz: b5d49462ea1025d0a5c910180e6db6eea8483bd682f6a1b77bcb6d5513f274cde6c0c26a7ddec7f4aea8016c542bf60341dd9e1a8d3d2f4eee8b3aae968c261d
data/README.md CHANGED
@@ -23,7 +23,7 @@ Create a file `myapp.rb` with the following content
23
23
  ```ruby
24
24
  # myapp.rb
25
25
  define do |app|
26
- class ::MainController < ActionController::Base
26
+ class MainController < ActionController::Base
27
27
  def index
28
28
  render text: "Hello"
29
29
  end
@@ -79,18 +79,14 @@ define "source" do |app|
79
79
  ENV["DATABASE_URL"] = "postgresql://localhost/minirails_test"
80
80
 
81
81
  # Migrations
82
- class CreateUsers < ActiveRecord::Migration
82
+ class Migrations < ActiveRecord::Migration
83
83
  def change
84
84
  create_table :users do |t|
85
85
  t.string :name
86
86
  t.string :email
87
87
  end
88
- end
89
- end
90
88
 
91
- # Seeds (using migartions)
92
- class CreateSomeUsersToStartWith < ActiveRecord::Migration
93
- def change
89
+ # Seeds
94
90
  User.create! [
95
91
  {name: "Jon", email: "jon@example.com"},
96
92
  {name: "Hodor", email: "hodor@example.com"}
@@ -117,16 +113,61 @@ define "source" do |app|
117
113
  end
118
114
  ```
119
115
 
120
- ## FAQ
121
116
 
122
- - **How to speed up loading?**
117
+ ### Non-rails apps
123
118
 
124
- Run `$ minirails --fast` and then use `bin/minirails myapp.rb` instead
119
+ You can start any rack compatible web app inside define block
120
+ or even generic worker process (sidekiq, clock, whatever)
121
+ by specifying `:type` option.
122
+
123
+ ```ruby
124
+ # file: types.rb
125
+ # usage: $ minirails types.rb
126
+
127
+ # this is rails app
128
+ define "rails" do |app|
129
+ app.routes.draw do
130
+ get "/", to: proc {|*| [200, {}, "Hello from Rails".lines] }
131
+ end
132
+ end
133
+
134
+ # with type: :rack you need to return Rack-compatible app
135
+ define "rack", type: :rack do
136
+ proc {|*| [200, {}, "Hello from Rack".lines] }
137
+ end
138
+
139
+ # with type: :blank you can run anything
140
+ define "worker", type: :blank do
141
+ loop do
142
+ $stderr.puts "Hello from worker"
143
+ sleep 1
144
+ end
145
+ end
146
+ ```
125
147
 
148
+ ### Running worker with Rails env loaded
149
+ ```ruby
150
+ define "my-rails-app" do |app|
151
+ app.routes.draw do
152
+ get "/", to: proc {|*| [200, {}, "Hello from Rails".lines] }
153
+ end
154
+ end
126
155
 
127
- - **I'm getting "constant not found" errors!**
156
+ # you can use :initialize option to load rails (or some rack)
157
+ # before loading worker code (similar to `rake environment`)
158
+ define "worker", type: :blank, initialize: "my-rails-app" do
159
+ loop do
160
+ Rails.logger.debug "Hello from worker with Rails env"
161
+ sleep 1
162
+ end
163
+ end
164
+ ```
165
+
166
+ ## FAQ
128
167
 
129
- You need to prefix your classes with `::` to put them in top level scope
168
+ - **How to speed up loading?**
169
+
170
+ Run `$ minirails --fast` and then use `bin/minirails myapp.rb` instead
130
171
 
131
172
 
132
173
  ## Contributing
@@ -53,46 +53,38 @@ OPTIONS:
53
53
  end
54
54
 
55
55
  def start(specfile, num = nil)
56
- builder = Builder.new(specfile)
56
+ self.builder = Builder.new(specfile)
57
57
  num ? builder.start(num.to_i) : builder.spawn
58
58
  end
59
+
60
+ attr_accessor :builder
59
61
  end
60
62
 
61
63
 
62
64
  class Builder < Struct.new(:specfile)
65
+
63
66
  # DSL API
64
- def define(name = nil, &block)
65
- apps << App.new(name, block)
67
+ def define(name = nil, opts = {}, &block)
68
+ type = opts[:type] || :rails
69
+ klazz = {
70
+ rails: RailsApp,
71
+ rack: RackApp,
72
+ blank: App
73
+ }[type]
74
+
75
+ if opts[:initialize]
76
+ parent = apps.find {|a| a.name == opts[:initialize] }
77
+ apps << WrappedApp.new(klazz.new(name, block), parent)
78
+ else
79
+ apps << klazz.new(name, block)
80
+ end
66
81
  end
67
82
 
68
83
  # internals
69
84
  def start(num)
70
85
  boot
71
-
72
- $stderr.puts "--> loading rails"
73
- require "action_controller"
74
- require "rails"
75
- $stderr.puts "--> loading app"
76
-
77
- Rack::Handler::WEBrick.run(endpoint(num), :Port => ENV["PORT"])
78
- end
79
-
80
- def endpoint(num)
81
- apps[num].endpoint
82
- rescue NameError => e
83
- # autoloading has some crazy weird error
84
- # and we can't just require activerecord upfront
85
- # because if app is not using it there will
86
- # be no database configuration
87
- case e.message
88
- when /uninitialized constant.*ActiveRecord/
89
- $stderr.puts "--> loading activerecord"
90
- require "active_record/railtie"
91
- else
92
- raise e
93
- end
94
-
95
- endpoint(num)
86
+ apps[num].init
87
+ apps[num].call
96
88
  end
97
89
 
98
90
  def spawn
@@ -119,11 +111,62 @@ OPTIONS:
119
111
  end
120
112
 
121
113
  def boot
122
- eval File.read(specfile)
114
+ load specfile
123
115
  end
124
116
  end
125
117
 
126
118
  class App < Struct.new(:name, :block)
119
+ def init
120
+ end
121
+
122
+ def call
123
+ block.call
124
+ end
125
+ end
126
+
127
+ class WrappedApp < Struct.new(:app, :parent)
128
+ def name
129
+ app.name
130
+ end
131
+
132
+ def init
133
+ parent.init
134
+ app.init
135
+ end
136
+
137
+ def call
138
+ app.call
139
+ end
140
+ end
141
+
142
+ class RackApp < App
143
+ def init
144
+ require "rack"
145
+ @endpoint = block.call
146
+ end
147
+
148
+ def call
149
+ Rack::Handler::WEBrick.run(@endpoint, :Port => ENV["PORT"])
150
+ end
151
+ end
152
+
153
+ class RailsApp < RackApp
154
+ def init
155
+ $stderr.puts "--> loading rails"
156
+ require "action_controller"
157
+ require "rails"
158
+ $stderr.puts "--> loading app"
159
+
160
+ @endpoint = endpoint
161
+ end
162
+
163
+ def call
164
+ migrate
165
+ super
166
+ end
167
+
168
+ protected
169
+
127
170
  def build
128
171
  # basic rails app bootstrap
129
172
  app = Class.new(Rails::Application) do
@@ -143,23 +186,49 @@ OPTIONS:
143
186
  # execute 'define' block
144
187
  block.call(app)
145
188
 
189
+ # and finally return app object
190
+ app
191
+ end
192
+
193
+ def migrate
146
194
  # ActiveRecord post functions
147
- if app.config.respond_to?(:active_record)
195
+ if @endpoint.config.respond_to?(:active_record)
148
196
  # load rake tasks
149
- app.load_tasks
197
+ @endpoint.load_tasks
150
198
 
151
199
  # create and migrate database
152
200
  Rake::Task["db:drop"].invoke
153
201
  Rake::Task["db:create"].invoke
154
- ActiveRecord::Migration.descendants.each {|e| e.migrate :up }
202
+ # run migrations in order they were defined in source file
203
+ ActiveRecord::Migration.descendants.sort_by {|c|
204
+ c.instance_methods(false).map {|m|
205
+ c.instance_method(m).source_location.last
206
+ }.min
207
+ }.each {|e| e.migrate :up }
155
208
  end
156
-
157
- # and finally return app object
158
- app
159
209
  end
160
210
 
161
211
  def endpoint
162
- @endpoint ||= build
212
+ build
213
+ rescue NameError => e
214
+ # autoloading has some crazy weird error
215
+ # and we can't just require activerecord upfront
216
+ # because if app is not using it there will
217
+ # be no database configuration
218
+ case e.message
219
+ when /uninitialized constant.*ActiveRecord/
220
+ $stderr.puts "--> loading activerecord"
221
+ require "active_record/railtie"
222
+ else
223
+ raise e
224
+ end
225
+
226
+ endpoint
163
227
  end
164
228
  end
165
229
  end
230
+
231
+ # Public DSL API
232
+ def define(*args, &block)
233
+ Minirails.builder.define(*args, &block)
234
+ end
@@ -1,3 +1,3 @@
1
1
  module Minirails
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minirails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tymon Tobolski