Skip to content

Commit

Permalink
Merge c3b31de into 2c3954a
Browse files Browse the repository at this point in the history
  • Loading branch information
sachin21 committed Apr 9, 2019
2 parents 2c3954a + c3b31de commit fa4a95f
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 35 deletions.
8 changes: 8 additions & 0 deletions .rubocop.yml
Expand Up @@ -4,5 +4,13 @@ AllCops:
Exclude:
- 'vendor/bundle/**/*'

DisplayCopNames: true

Style/Documentation:
Enabled: false

Metrics/LineLength:
Max: 100

Metrics/MethodLength:
Max: 15
8 changes: 4 additions & 4 deletions .travis.yml
@@ -1,10 +1,10 @@
language: ruby

rvm:
- ruby-2.3.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.3.7
- 2.4.4
- 2.5.1
- ruby-head

before_install:
- gem install bundler
Expand Down
15 changes: 6 additions & 9 deletions README.md
Expand Up @@ -3,17 +3,20 @@
![Gem Version](https://badge.fury.io/rb/space2underscore.svg) [![Build Status](https://travis-ci.org/sachin21/space2underscore.svg?branch=master)](https://travis-ci.org/sachin21/space2underscore) [![Code Climate](https://codeclimate.com/github/sachin21/space2underscore/badges/gpa.svg)](https://codeclimate.com/github/sachin21/space2underscore) [![Coverage Status](https://coveralls.io/repos/sachin21/space2underscore/badge.svg?branch=master&service=github)](https://coveralls.io/github/sachin21/space2underscore?branch=master) [![Gem](https://img.shields.io/gem/dt/space2underscore.svg)](https://rubygems.org/gems/space2underscore)

## What is space2underscore

Change the space into underscore.
space2underscore is a useful command when you want to check out a branch.

## Installation

Install it yourself as:

```
$ gem install space2underscore
```

## Usage

From the command line:

### 1. e.g. Create the new branch
Expand All @@ -32,16 +35,10 @@ $ git branch -m $(s2u renamed branch)
```

## !! Cation !!
This is a command line tool. So DO NOT include all modules because it uses `system` command. So crackers will be attacking your application.

This is a command line tool. So DO NOT include space2underscore's module in other Ruby application because it uses `system` command.
If you include this module to your application, there is a possibility of crackers attacking.

## Credits
- Help information based on [@motemen's ghq](https://github.com/motemen/ghq)
- Installed information based on [Tmuxinator](https://github.com/tmuxinator/tmuxinator)

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
45 changes: 33 additions & 12 deletions lib/space2underscore/cli.rb
@@ -1,31 +1,36 @@
# frozen_string_literal: true

module Space2underscore
class Cli
CREATE_FLAGS = %w[-c --create].freeze
RAW_FLAGS = %w[-r --raw].freeze

FLAGS = [CREATE_FLAGS, RAW_FLAGS].flatten.freeze

ERROR_MSG = 'Option is invalid format. It is only avaliable for `-c --create -d --downcase`'

OptionParseError = Class.new(ArgumentError)

def initialize(argv)
@argv = argv
@underscore_include_branch = Underscore.new(branch).convert
@executer = Executor.instance
@printer = Printer.instance
end

def start
$stdout.puts Usage.new.content && exit if @argv.empty?

case
when included?(CREATE_FLAGS) && included?(RAW_FLAGS)
Executor.new(@underscore_include_branch).run_with_raw
when included?(CREATE_FLAGS) && not_included?(RAW_FLAGS)
Executor.new(@underscore_include_branch).run_with_downcase
when not_included?(CREATE_FLAGS) && included?(RAW_FLAGS)
Printer.instance.run_with_raw(@underscore_include_branch)
when not_included?(CREATE_FLAGS) && not_included?(RAW_FLAGS)
Printer.instance.run_with_downcase(@underscore_include_branch)
return $stdout.puts Usage.new.content if @argv.empty?

if with_all_flags?
@executer.run_with_raw(@underscore_include_branch)
elsif create_flags_without_raw_flags?
@executer.run_with_downcase(@underscore_include_branch)
elsif raw_flags_without_create_flags?
@printer.run_with_raw(@underscore_include_branch)
elsif without_any_flags?
@printer.instance.run_with_downcase(@underscore_include_branch)
else
raise OptionParseError, 'Option is invalid format. It is only avaliable for `-c --create -d --downcase`'
raise OptionParseError, ERROR_MSG
end
end

Expand All @@ -42,5 +47,21 @@ def included?(flags)
def not_included?(flags)
!included?(flags)
end

def without_any_flags?
not_included?(CREATE_FLAGS) && not_included?(RAW_FLAGS)
end

def with_all_flags?
included?(CREATE_FLAGS) && included?(RAW_FLAGS)
end

def create_flags_without_raw_flags?
included?(CREATE_FLAGS) && not_included?(RAW_FLAGS)
end

def raw_flags_without_create_flags?
included?(RAW_FLAGS) && not_included?(CREATE_FLAGS)
end
end
end
16 changes: 9 additions & 7 deletions lib/space2underscore/executor.rb
@@ -1,15 +1,17 @@
# frozen_string_literal: true

require 'singleton'

module Space2underscore
class Executor
def initialize(underscore_include_sentence)
@underscore_include_sentence = underscore_include_sentence
end
include Singleton

def run_with_raw
run @underscore_include_sentence
def run_with_raw(underscore_include_sentence)
run underscore_include_sentence
end

def run_with_downcase
run @underscore_include_sentence.downcase
def run_with_downcase(underscore_include_sentence)
run underscore_include_sentence.downcase
end

private
Expand Down
2 changes: 2 additions & 0 deletions lib/space2underscore/printer.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'singleton'

module Space2underscore
Expand Down
2 changes: 2 additions & 0 deletions lib/space2underscore/underscore.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Space2underscore
class Underscore
def initialize(sentence)
Expand Down
6 changes: 4 additions & 2 deletions lib/space2underscore/usage.rb
@@ -1,16 +1,18 @@
# frozen_string_literal: true

module Space2underscore
class Usage
MESSAGE = <<-EOF.chomp
NAME:
space2underscore - Change the space into underscore
USAGE:
$ space2underscore new branch -c
$ s2u new branch -c
=> Switched to the new branch 'new_branch’
Or
$ git branch -m $(space2underscore renamed branch)
$ git branch -m $(s2u renamed branch)
VERSION:
#{Space2underscore::VERSION}
Expand Down
1 change: 1 addition & 0 deletions space2underscore.gemspec
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 2.3.0'

spec.post_install_message = File.read('post_install_message.txt')

Expand Down
2 changes: 1 addition & 1 deletion spec/space2underscore/underscore_spec.rb
Expand Up @@ -14,7 +14,7 @@
end

context 'when number of argument is many' do
let(:argument) { %w(foo bar) }
let(:argument) { %w[foo bar] }

it { is_expected.to include('_') }
end
Expand Down

0 comments on commit fa4a95f

Please sign in to comment.