richrc 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,33 +1,81 @@
1
1
  # RichRC
2
2
 
3
- RichRC (Rich Rails Console) loads [hirb](https://github.com/cldwalker/hirb) and [wirble](http://pablotron.org/software/wirble/) in rails 3 console without editing files in rails app directory.
3
+ RichRC (Rich Rails Console) is a customization tool for Rails 3 console.
4
4
 
5
- Hirb and wirble are excellent IRB-extension which makes data more readable. In rails 2, we can simply require gems in IRB and start using them. However, In Rails 3, it's needed to add gems into `Gemfile` before using it. Sometimes it doesn't make sense to add IRB-extension gems into rails app Gemfile (Maybe not all developers want hirb and wirble, and it's annoying to add the gems whenever cloning a project).
5
+ There are some useful gems which extends function of IRB and makes IRB more convenient. For example, [hirb](https://github.com/cldwalker/hirb) and [wirble](http://pablotron.org/software/wirble/) are excellent gems which makes data more readable. In rails 2, we can simply require gems in IRB.
6
6
 
7
- Inspired from [xdite's article](http://blog.xdite.net/?p=1839), the extra gems cloud be loaded before `Bundler.setup` is invoked. RichRC mimics the bootstrap process of rails console, loads and initializes extra gems, and starts console normally.
7
+ However, In Rails 3, it's needed to add gems into `Gemfile` before using them. Sometimes it doesn't make sense to add IRB-extension gems into `Gemfile`. Maybe not all developers want hirb and wirble, and it's quite annoying to edit files whenever cloning a project.
8
+
9
+ Inspired from [xdite's article](http://blog.xdite.net/?p=1839), the extra gems cloud be loaded before `Bundler.setup` is invoked. RichRC mimics the bootstrap process of rails console, run extra code before or after specified event (such as `bundler.setup`).
8
10
 
9
11
  ## Installation
10
12
 
11
13
  $ gem install richrc hirb wirble
12
14
 
13
- P.S. In RichRC's gemspec, it doesn't depend on hirb and wirble. If some of the gems are missing, RichRC simply ignore them.
15
+ P.S. RichRC loads hirb and wirble by default, but RichRC doesn't have dependency to these gems. If you don't want them, read **Customization** section.
16
+
17
+ ## Quick Start
14
18
 
15
- ## Running RichRC
19
+ Though RichRC provides customization, you can simply start with default configuration:
16
20
 
17
21
  $ cd railsapp
18
22
  $ richrc # instead of `rails console`
19
23
 
20
- That's all :)
24
+ This will automatically:
25
+
26
+ * Enables `wirble` gem
27
+ * Enables `hirb` gem
28
+ * Makes ActiveRecord log outputted in console
29
+
30
+ P.S. The original rails console is not affected. If problems occured with RichRC, you can fallback to `rails console`.
31
+
32
+ ## Customization
33
+
34
+ When running `richrc`, it try to load configuration file in following sequence:
35
+
36
+ * `.richrc` in current directory
37
+ * `.richrc` in user's home directory
38
+ * The default configuration file in richrc gem
39
+
40
+ Run `richrc customize` to copy default configuration file to `.richrc` into your current directory:
41
+
42
+ before(:setup_bundler) do
43
+ # You can load extra gems here.
44
+ require 'irb'
45
+
46
+ begin
47
+ gem 'wirble'
48
+ require 'wirble'
49
+ Wirble.init
50
+ Wirble.colorize
51
+ rescue LoadError
52
+ puts "Failed to load wirble"
53
+ end
54
+
55
+ begin
56
+ gem 'hirb'
57
+ require 'hirb'
58
+ Hirb.enable
59
+ rescue LoadError
60
+ puts "Failed to load hirb"
61
+ end
62
+ end
63
+
64
+ after(:load_application) do
65
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
66
+ end
67
+
68
+ If you want to load any extra gems, write in `before(:setup_bundler)` section. After that, the gem loading process is controlled by bundler. If you want to do any setting to rails, Write in `after(:load_application)` section, which ensures rails and application environment is loaded.
21
69
 
22
- The original rails console is not affected. If problems occured with RichRC, you can simply fallback to `rails console`.
70
+ ## Notice
23
71
 
24
- ## Possible Issues
72
+ Loading a gem which conflict with gems (and their dependencies) in `Gemfile` will cause problem.
25
73
 
26
- * Theoretically, if hirb and wirble(and their dependent gems) conflict with gems in `Gemfile`, it may cause problems. However, I never encounter this situation. Hacking Bundler.setup might be a better way.
74
+ ## Issue Report
27
75
 
28
- Fell free to report issues in Github's issue tracker.
76
+ Fell free to report issues in Github's issue tracker. If it's an error report, I strongly recommand to write your `Gemfile` and `.richrc` in your issue.
29
77
 
30
78
  ## License
31
79
 
32
- See MIT-LICENSE file for details.
80
+ See MIT-LICENSE file for details.
33
81
 
data/bin/richrc CHANGED
@@ -1,13 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "richrc"
3
+ require 'richrc'
4
4
 
5
- rails_loader = Richrc::RailsLoader.new
6
- if rails_loader.app_path
7
- Richrc.wirble!
8
- Richrc.hirb!
9
- rails_loader.start_console!
5
+ if ARGV.empty?
6
+ Richrc::RailsLoader.new.run
10
7
  else
11
- puts "You are not in rails app path!"
8
+ # In order to keep gemset clean, thor is not loaded when executing rails console
9
+ # Thus we need to require rubygems and thor here
10
+ gem 'thor'
11
+ require 'thor'
12
+ require 'richrc/cli'
13
+ Richrc::CLI.start
12
14
  end
13
-
@@ -0,0 +1,28 @@
1
+ before(:setup_bundler) do
2
+ # You can load extra gems here.
3
+
4
+ require 'irb'
5
+
6
+ begin
7
+ gem 'wirble'
8
+ require 'wirble'
9
+ Wirble.init
10
+ Wirble.colorize
11
+ rescue LoadError
12
+ puts "Failed to load wirble"
13
+ end
14
+
15
+ begin
16
+ gem 'hirb'
17
+ require 'hirb'
18
+ Hirb.enable
19
+ rescue LoadError
20
+ puts "Failed to load hirb"
21
+ end
22
+
23
+ end
24
+
25
+ after(:load_application) do
26
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
27
+ end
28
+
@@ -1,4 +1,3 @@
1
1
  require 'richrc/version'
2
2
  require 'richrc/rails_loader'
3
- require 'richrc/ext_loader'
4
-
3
+ require 'richrc/config'
@@ -0,0 +1,13 @@
1
+ module Richrc
2
+ class CLI < Thor
3
+
4
+ include Thor::Actions
5
+
6
+ desc 'customize', 'generate .richrc config file in current path'
7
+ def customize
8
+ default_config = File.expand_path(File.join(__FILE__, "../../../config/default.rb"))
9
+ create_file(".richrc", File.read(default_config))
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ module Richrc
2
+
3
+ class ConfigLoader
4
+
5
+ def initialize(rails_loader)
6
+ @rails_loader = rails_loader
7
+ end
8
+
9
+ def load(path)
10
+ instance_eval(File.read(path))
11
+ end
12
+
13
+ def method_missing(method, *args, &block)
14
+ if %w{before after}.include?(method.to_s)
15
+ @rails_loader.add_callback(method.to_sym, args.first, block)
16
+ else
17
+ super
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -1,15 +1,36 @@
1
1
  module Richrc
2
2
  class RailsLoader
3
-
4
3
  SCRIPT_RAILS = File.join('script', 'rails')
5
4
 
5
+ def initialize
6
+ @callbacks = Hash.new []
7
+ end
8
+
9
+ def run
10
+ if app_path
11
+ start_console
12
+ else
13
+ puts "You are not in rails app path!"
14
+ end
15
+ end
16
+
6
17
  def app_path
7
18
  @app_path ||= find_app_path
8
19
  end
9
20
 
21
+ def add_callback(timing, kind, task)
22
+ key = [timing, kind]
23
+ @callbacks[key] = [] unless @callbacks.has_key?(key)
24
+ @callbacks[key] << task
25
+ end
26
+
27
+ private
28
+
29
+ # Find app path and chdir to app path
10
30
  def find_app_path
31
+ # The algorithm is mimicking from rails source code, I think there should be a smarter way
11
32
  last_dir = Dir.pwd
12
- until in_app_path?
33
+ until File.exists?(SCRIPT_RAILS)
13
34
  Dir.chdir("..")
14
35
  return nil if Dir.pwd==last_dir
15
36
  last_dir = Dir.pwd
@@ -19,18 +40,46 @@ module Richrc
19
40
  nil
20
41
  end
21
42
 
22
- def in_app_path?
23
- File.exists?(SCRIPT_RAILS)
43
+ def run_callbacks(kind)
44
+ @callbacks[[:before, kind]].each { |blk| Object.new.instance_eval(&blk) }
45
+ yield
46
+ @callbacks[[:after, kind]].each { |blk| Object.new.instance_eval(&blk) }
24
47
  end
25
48
 
26
- def start_console!
27
- require File.expand_path(File.join(app_path, "config/boot"))
28
- require 'rails/commands/console'
29
- require File.expand_path(File.join(app_path, "config/application"))
49
+ def start_console
50
+ puts "Loading RichRC environment"
51
+ load_config
52
+
53
+ run_callbacks(:setup_bundler) do
54
+ require File.expand_path(File.join(app_path, "config/boot"))
55
+ end
56
+
57
+ run_callbacks(:load_application) do
58
+ require 'rails/commands/console'
59
+ require File.expand_path(File.join(app_path, "config/application"))
60
+ Rails.application.require_environment!
61
+ end
30
62
 
31
- Rails.application.require_environment!
32
63
  Rails::Console.start(Rails.application)
33
64
  end
34
65
 
66
+ def load_config
67
+ current_config = File.expand_path(File.join(".", ".richrc"))
68
+ home_config = File.expand_path(File.join(ENV["HOME"], ".richrc"))
69
+ default_config = File.expand_path(File.join(__FILE__, "../../../config/default.rb"))
70
+
71
+ [current_config, home_config, default_config].each do |path|
72
+ if File.exists?(path)
73
+ ConfigLoader.new(self).load(path)
74
+ if path==default_config
75
+ puts "RichRC: Loading default config file"
76
+ else
77
+ puts "RichRC: Loading config: #{path}"
78
+ end
79
+ break
80
+ end
81
+ end
82
+ end
83
+
35
84
  end
36
85
  end
@@ -1,3 +1,3 @@
1
1
  module Richrc
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: richrc
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
4
+ hash: 25
5
+ prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - miaout17
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-09 00:00:00 +08:00
18
+ date: 2011-02-13 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -35,8 +35,10 @@ files:
35
35
  - README.md
36
36
  - Rakefile
37
37
  - bin/richrc
38
+ - config/default.rb
38
39
  - lib/richrc.rb
39
- - lib/richrc/ext_loader.rb
40
+ - lib/richrc/cli.rb
41
+ - lib/richrc/config.rb
40
42
  - lib/richrc/rails_loader.rb
41
43
  - lib/richrc/version.rb
42
44
  - richrc.gemspec
@@ -70,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
72
  requirements: []
71
73
 
72
74
  rubyforge_project: richrc
73
- rubygems_version: 1.4.1
75
+ rubygems_version: 1.3.7
74
76
  signing_key:
75
77
  specification_version: 3
76
78
  summary: Rich Rails Console
@@ -1,28 +0,0 @@
1
- module Richrc
2
- class << self
3
- def wirble!
4
- require "irb"
5
- begin
6
- gem 'wirble'
7
- require 'wirble'
8
- Wirble.init
9
- Wirble.colorize
10
- puts "Wirble is loaded"
11
- rescue LoadError
12
- puts "Wirble is not loaded!!(Did you install wirble gem?)"
13
- end
14
- end
15
-
16
- def hirb!
17
- begin
18
- gem 'hirb'
19
- require 'hirb'
20
- Hirb.enable
21
- puts "Hirb is loaded"
22
- rescue LoadError
23
- puts "Hirb is not loaded!!(Did you install hirb gem?)"
24
- end
25
- end
26
- end
27
- end
28
-