nrb 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3.0
3
+ - 2.2.1
4
4
  before_install: gem install bundler -v 1.11.2
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+ guard :minitest do
2
+ ignore(%r{^test/(fixtures|coverage)})
3
+
4
+ watch(%r(^test/(.*)\/?(.*)_test\.rb$))
5
+ watch(%r(^lib/(.*/)?([^/]+)\.rb$)) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
6
+ watch(%r(^test/test_helper\.rb$)) { 'test' }
7
+ end
8
+
9
+ guard :rubocop do
10
+ watch(%r{.+\.rb$})
11
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
12
+ end
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
- # Ninja Ruby - Nrb
1
+ # Ninja Ruby - `nrb` [![Gem Version](https://badge.fury.io/rb/nrb.svg)](https://badge.fury.io/rb/nrb)
2
2
 
3
- Ninja Ruby scripts with easy persistence for your experimenting needs
3
+ [![Build Status](https://travis-ci.org/shuriu/nrb.svg?branch=master)](https://travis-ci.org/shuriu/nrb) [![Coverage Status](https://coveralls.io/repos/github/shuriu/nrb/badge.svg?branch=master)](https://coveralls.io/github/shuriu/nrb?branch=master) [![Code Climate](https://codeclimate.com/github/shuriu/nrb/badges/gpa.svg)](https://codeclimate.com/github/shuriu/nrb) [![Dependency Status](https://gemnasium.com/shuriu/nrb.svg)](https://gemnasium.com/shuriu/nrb)
4
+
5
+ Ninja Ruby scripts with easy persistence for your experimenting needs.
4
6
 
5
7
  # Idea
6
8
 
@@ -40,15 +42,103 @@ Or you could just use `$ nrb new <idea-name>` and not worry about anything!
40
42
 
41
43
  ## Usage
42
44
 
43
- $ nrb help
44
-
45
- ## TODO
45
+ ```sh
46
+ $ nrb help
47
+ Commands:
48
+ nrb [c]onsole # Jump into a Pry console with your project loaded. NOTE: Available inside a NinjaRuby project
49
+ nrb [d]estroy <resource> <name> # Destroy a generated resource. NOTE: Available inside a NinjaRuby project
50
+ nrb [g]enerate <resource> <name> # Generate a resource (models, services). NOTE: Available inside a NinjaRuby project
51
+ nrb [s]tart # Require the main file. NOTE: Available inside a NinjaRuby project
52
+ nrb help [COMMAND] # Describe available commands or one specific command
53
+ nrb new <name-or-path> [OPTIONS] # Creates a Ninja Ruby Script at the given path
54
+ ```
55
+
56
+ Create an app and some resources:
57
+
58
+ ```sh
59
+ $ nrb help new
60
+ Usage:
61
+ nrb new <name-or-path> [OPTIONS]
62
+
63
+ Options:
64
+ -r, [--init-repo], [--no-init-repo] # Initialize a repository at the target location
65
+ # Default: true
66
+ -b, [--bundle-install], [--no-bundle-install] # Run bundle install after generating the skeleton
67
+ # Default: true
68
+ Creates a Ninja Ruby Script at the given path
69
+
70
+ $ nrb new user_importer
71
+ create user_importer/.gitignore
72
+ create user_importer/README.md
73
+ create user_importer/Gemfile
74
+ create user_importer/Rakefile
75
+ create user_importer/config/nrb.rb
76
+ create user_importer/models/.keep
77
+ create user_importer/services/.keep
78
+ create user_importer/config/boot.rb
79
+ create user_importer/db/config.yml
80
+ create user_importer/user_importer.rb
81
+ $ cd user_importer
82
+
83
+ $ rake db:create
84
+
85
+ $ nrb g model user first_name last_name email
86
+ create models/user.rb
87
+ run rake db:new_migration name=create_users options='first_name last_name email' from "."
88
+ create db/migrate/20160309074029_create_users.rb
89
+
90
+ $ rake db:migrate
91
+
92
+ $ nrb console
93
+
94
+ [1] pry(main)> User.create(first_name: 'Gandalf', last_name: 'The Grey', email: 'gandalf@example.com')
95
+ D, [2016-03-09T09:44:12.434679 #21739] DEBUG -- : (0.1ms) begin transaction
96
+ D, [2016-03-09T09:44:12.440635 #21739] DEBUG -- : SQL (0.5ms) INSERT INTO "users" ("first_name", "last_name", "email") VALUES (?, ?, ?) [["first_name", "Gandalf"], ["last_name", "The Grey"], ["email", "gandalf@example.com"]]
97
+ D, [2016-03-09T09:44:12.444638 #21739] DEBUG -- : (3.6ms) commit transaction
98
+ => #<User:0x005582ec4153d8 id: 1, first_name: "Gandalf", last_name: "The Grey", email: "gandalf@example.com">
99
+
100
+ [2] pry(main)> User.all
101
+ D, [2016-03-09T09:45:11.252371 #21739] DEBUG -- : User Load (0.2ms) SELECT "users".* FROM "users"
102
+ => [#<User:0x005582ed31c3e0 id: 1, first_name: "Gandalf", last_name: "The Grey", email: "gandalf@example.com">]
103
+
104
+ ```
105
+
106
+ ## Configuration
107
+
108
+ To add custom configurations or change existing ones, you can modify `config/nrb.rb`:
109
+
110
+ ```ruby
111
+ Nrb.configure do |config|
112
+ # Root of the script folder
113
+ config.root = File.expand_path('..', __dir__)
114
+
115
+ # Default resources to autoload
116
+ # config.resources = %w(models services)
117
+ end
118
+ ```
119
+
120
+ To configure the default sqlite db, you can update `db/config.yml`:
121
+
122
+ ```yaml
123
+ development:
124
+ adapter: sqlite3
125
+ pool: 5
126
+ timeout: 5000
127
+ database: db/new_script.sqlite3
128
+ ```
129
+
130
+ ## Roadmap
131
+
132
+ - [x] Tests
133
+ - [ ] Support for custom bundler groups
134
+ - [ ] Ability to change the persistence adapter
135
+ - [ ] Ability to skip persistence altogether
46
136
 
47
137
  ## Development
48
138
 
49
139
  Run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
140
 
51
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
141
+ To install this gem onto your local machine, run `bundle exec rake install`.
52
142
 
53
143
  ## Contributing
54
144
 
data/Rakefile CHANGED
@@ -7,4 +7,4 @@ Rake::TestTask.new(:test) do |t|
7
7
  t.test_files = FileList['test/**/*_test.rb']
8
8
  end
9
9
 
10
- task :default => :spec
10
+ task :default => :test
data/lib/nrb.rb CHANGED
@@ -4,10 +4,16 @@ require 'binding_of_caller'
4
4
  require 'pry-byebug'
5
5
  require 'standalone_migrations'
6
6
  require 'sqlite3'
7
+ require 'nrb/errors'
7
8
  require 'nrb/version'
8
9
 
9
10
  module Nrb
10
- class Configuration < OpenStruct; end
11
+ class Configuration < OpenStruct
12
+ def initialize(*args)
13
+ super
14
+ self.resources = %w(models services)
15
+ end
16
+ end
11
17
 
12
18
  class << self
13
19
  def config
@@ -27,11 +33,16 @@ module Nrb
27
33
 
28
34
  !!(File.read(gemfile) =~ /gem\s+['"]nrb['"]/)
29
35
  end
30
- end
31
- end
32
36
 
33
- Nrb.configure do |config|
34
- config.directories = %w(models services)
37
+ def silently(verbose: false)
38
+ yield and return if verbose
39
+
40
+ original_stdout = $stdout.clone
41
+ $stdout.reopen '/dev/null'
42
+ yield
43
+ $stdout.reopen original_stdout
44
+ end
45
+ end
35
46
  end
36
47
 
37
48
  require 'nrb/cli'
data/lib/nrb/cli.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  require 'pry'
2
2
  require 'binding_of_caller'
3
3
  require 'thor'
4
- require 'nrb/commands/script'
5
- require 'nrb/commands/starter'
6
- require 'nrb/commands/console'
7
- require 'nrb/commands/generate'
8
- require 'nrb/commands/destroy'
4
+ require 'nrb/commands/all'
9
5
 
10
6
  module Nrb
11
7
  class CLI < Thor
8
+ class_option :verbose, type: :boolean, default: true,
9
+ desc: 'Verbose mode.',
10
+ aliases: '-v'
11
+
12
12
  register Nrb::Commands::Script, 'new', 'new <name-or-path> [OPTIONS]',
13
13
  Nrb::Commands::Script.desc
14
14
  tasks['new'].options = Nrb::Commands::Script.class_options
@@ -0,0 +1,11 @@
1
+ require 'nrb/commands/concerns/resource_generator'
2
+
3
+ require 'nrb/commands/base/base'
4
+ require 'nrb/commands/base/inside'
5
+ require 'nrb/commands/base/resource'
6
+
7
+ require 'nrb/commands/script'
8
+ require 'nrb/commands/starter'
9
+ require 'nrb/commands/console'
10
+ require 'nrb/commands/generate'
11
+ require 'nrb/commands/destroy'
@@ -0,0 +1,30 @@
1
+ module Nrb
2
+ module Commands
3
+ class Base < Thor::Group
4
+ include Thor::Actions
5
+
6
+ class_option :verbose, type: :boolean, default: true,
7
+ desc: 'Verbose mode.',
8
+ aliases: '-v'
9
+
10
+ def self.source_root
11
+ File.expand_path('../..', __dir__)
12
+ end
13
+
14
+ def opts
15
+ options.to_h.symbolize_keys
16
+ end
17
+
18
+ private
19
+
20
+ def require_main_file
21
+ assumed_file_name = "#{Nrb.root.split('/').last}.rb"
22
+ assumed_file_path = File.join(Nrb.root, assumed_file_name)
23
+
24
+ if File.exist?(assumed_file_path)
25
+ require File.join(Nrb.root, assumed_file_name)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ module Nrb
2
+ module Commands
3
+ class Inside < Commands::Base
4
+ def ensure_inside_root
5
+ return true if Nrb.inside?
6
+ fail_outside_root
7
+ end
8
+
9
+ private
10
+
11
+ def fail_outside_root
12
+ command = self.class.to_s.split('::').last
13
+
14
+ fail Nrb::OutsideRootError,
15
+ "You need to be inside a NinjaRuby folder to run the #{command} command."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module Nrb
2
+ module Commands
3
+ class Resource < Commands::Inside
4
+ include ResourceGenerator
5
+
6
+ def self.valid_resources
7
+ arguments.find { |a| a.name == 'resource' }.enum
8
+ end
9
+
10
+ def ensure_valid_resource
11
+ return true if valid_resources.include? resource
12
+ fail_invalid_resource
13
+ end
14
+
15
+ private
16
+
17
+ def fail_invalid_resource
18
+ fail Nrb::InvalidResourceError,
19
+ "<resource> must be one of: #{valid_resources.join(', ')}."
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ require 'active_support/concern'
2
+
3
+ module Nrb
4
+ module Commands
5
+ module ResourceGenerator
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ argument :resource, type: :string, required: true,
10
+ desc: 'resource type',
11
+ banner: 'resource',
12
+ enum: Nrb.config.resources.map(&:singularize)
13
+
14
+ argument :name, type: :string, required: true,
15
+ desc: 'name of the resource',
16
+ banner: 'name'
17
+ end
18
+
19
+ private
20
+
21
+ def target(final = nil)
22
+ File.join(File.expand_path(resource.pluralize), final.to_s)
23
+ end
24
+
25
+ def valid_resources
26
+ self.class.valid_resources
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,13 +1,15 @@
1
- require 'nrb/commands/inside_group'
2
-
3
1
  module Nrb
4
2
  module Commands
5
- class Console < InsideGroup
6
- desc_with_warning 'Jump into a Pry console with your project loaded'
3
+ class Console < Commands::Inside
4
+ desc 'Jump into a Pry console with your project loaded.'
5
+
6
+ class_option :pretend, default: false, type: :boolean,
7
+ desc: 'Pretend opening the console. Useful for testing.'
7
8
 
8
9
  def start
9
- require_main_file
10
- Pry.start
10
+ required = require_main_file
11
+ Pry.start unless options[:pretend]
12
+ required
11
13
  end
12
14
  end
13
15
  end
@@ -1,41 +1,18 @@
1
- require 'nrb/commands/inside_group'
1
+ require 'nrb/commands/concerns/resource_generator'
2
2
 
3
3
  module Nrb
4
4
  module Commands
5
- class Destroy < InsideGroup
6
- desc_with_warning "Destroy a generated resource"
5
+ class Destroy < Commands::Resource
6
+ desc 'Destroy a generated resource.'
7
7
 
8
- argument :resource, type: :string, required: true,
9
- desc: 'resource to destroy',
10
- banner: 'RESOURCE',
11
- enum: Nrb.config.directories.map(&:singularize)
12
-
13
-
14
- argument :name, type: :string, required: true,
15
- desc: 'name of the resource',
16
- banner: 'NAME'
17
-
18
- def valid_resource?
19
- valid_resources = Nrb.config.directories.map(&:singularize)
20
- return if valid_resources.include? resource
21
- say "RESOURCE must be one of: #{valid_resources.join(', ')}."
22
- exit
8
+ def destroy_resource
9
+ remove_file target("#{name.underscore}.rb"), opts
23
10
  end
24
11
 
25
- def generate_resource
26
- remove_file target("#{name.underscore}.rb")
27
-
28
- # Also remove the *_create_resource migration
29
- if resource == 'model'
30
- migration_file = Dir["db/migrate/*create_#{name.underscore.pluralize}.rb"].first
31
- remove_file migration_file if migration_file
32
- end
33
- end
34
-
35
- private
36
-
37
- def target(final = nil)
38
- File.join(File.expand_path(resource.pluralize), final.to_s)
12
+ def destroy_migration
13
+ return false unless resource == 'model'
14
+ migration_file = Dir["db/migrate/*_create_#{name.underscore.pluralize}.rb"].first
15
+ remove_file(migration_file, opts) if migration_file
39
16
  end
40
17
  end
41
18
  end
@@ -1,48 +1,26 @@
1
- require 'nrb/commands/inside_group'
2
-
3
1
  module Nrb
4
2
  module Commands
5
- class Generate < InsideGroup
6
- desc_with_warning "Generate a resource (#{Nrb.config.directories.join(', ')})"
7
-
8
- argument :resource, type: :string, required: true,
9
- desc: 'resource to generate',
10
- banner: 'RESOURCE',
11
- enum: Nrb.config.directories.map(&:singularize)
12
-
13
-
14
- argument :name, type: :string, required: true,
15
- desc: 'name of the resource',
16
- banner: 'NAME'
17
-
18
- def valid_resource?
19
- valid_resources = Nrb.config.directories.map(&:singularize)
20
- return if valid_resources.include? resource
21
- say "RESOURCE must be one of: #{valid_resources.join(', ')}."
22
- exit
23
- end
3
+ class Generate < Commands::Resource
4
+ desc "Generate a resource (#{valid_resources.join(', ')})."
24
5
 
25
6
  def generate_resource
26
7
  template "templates/#{resource}.rb.tt", target("#{name.underscore}.rb"),
27
- name: name.camelize
8
+ opts.merge({ name: name.camelize })
28
9
  end
29
10
 
30
- def create_table
31
- return unless resource == 'model'
11
+ def generate_table
12
+ return false unless resource == 'model'
32
13
 
33
14
  migration_name = "create_#{name.underscore.pluralize}"
34
- options = args.join(' ')
15
+ rake_options = args.join(' ')
35
16
 
36
- inside Nrb.root do
37
- run "rake db:new_migration name=#{migration_name} options='#{options}'"
17
+ inside Nrb.root, opts do
18
+ Nrb.silently(opts) do
19
+ run "bundle exec rake db:new_migration name=#{migration_name} options='#{rake_options}'",
20
+ opts
21
+ end
38
22
  end
39
23
  end
40
-
41
- private
42
-
43
- def target(final = nil)
44
- File.join(File.expand_path(resource.pluralize), final.to_s)
45
- end
46
24
  end
47
25
  end
48
26
  end