Skip to content

Commit

Permalink
Merge branch 'release/v1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
imageaid committed Jul 26, 2014
2 parents eff91eb + a9d7b3e commit c5ce83d
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Description:
Generates a service class and accompanying spec file

Example:
rails generate service Thing Model|Controller Sub-folder [opt]
rails generate rails_services:create Thing Model|Controller Sub-folder [opt]

This will create:
app/services/model|controller/[sub-folder]/thing.rb
Expand Down
37 changes: 37 additions & 0 deletions lib/generators/rails_services/create/create_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module RailsServices
module Generators
class CreateGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
argument :service_name, type: :string
argument :parent_name, type: :string
argument :sub_folder, type: :string, default: ''

def create_service
template 'base_service.rb.erb', "app/services/#{service_directory}/#{service_file}"
template 'base_service_spec.rb.erb', "spec/services/#{service_directory}/#{spec_file}"
end

private
def service_directory
file_path = "#{parent_name.underscore}"
file_path += "/#{sub_folder.underscore}" if sub_folder.present?
file_path
end

def service_file
"#{service_name.underscore}.rb"
end

def spec_file
"#{service_name.underscore}_spec.rb"
end

def qualified_name
name = "#{parent_name.camelize}::"
name += "#{sub_folder.camelize}::" if sub_folder.present?
name += "#{service_name.camelize}"
name
end
end
end
end
9 changes: 9 additions & 0 deletions lib/generators/rails_services/destroy/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Description:
Destroys a service class and accompanying spec file

Example:
rails generate rails_services:destroy Thing Model|Controller Sub-folder [opt]

This will remove:
app/services/model|controller/[sub-folder]/thing.rb
spec/services/model|controller/[sub-folder]/thing_spec.rb
51 changes: 51 additions & 0 deletions lib/generators/rails_services/destroy/destroy_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module RailsServices
module Generators
class DestroyGenerator < Rails::Generators::Base
argument :service_name, type: :string
argument :parent_name, type: :string
argument :sub_folder, type: :string, default: ''

def destroy_service
remove_file "app/services/#{full_service_directory}/#{service_file}"
remove_file "spec/services/#{full_service_directory}/#{spec_file}"

manage_containing_directories('app', 'spec')
end

private
def full_service_directory
file_path = "#{service_parent_directory}"
file_path += "/#{sub_folder.underscore}" if sub_folder.present?
file_path
end

def service_parent_directory
"#{parent_name.underscore}"
end

def service_file
"#{service_name.underscore}.rb"
end

def spec_file
"#{service_name.underscore}_spec.rb"
end

def manage_containing_directories(*directory_types)
directory_types.each do |directory_type|
remove_dir("#{directory_type}/services/#{full_service_directory}") if directory_empty?(directory_type)
remove_dir("#{directory_type}/services/#{service_parent_directory}") if parent_directory_empty?(directory_type)
end
end

def directory_empty?(directory_type)
Dir["#{Rails.root}/#{directory_type}/services/#{full_service_directory}/*"].empty?
end

def parent_directory_empty?(directory_type)
Dir["#{Rails.root}/#{directory_type}/services/#{service_parent_directory}/*"].empty?
end

end
end
end
37 changes: 0 additions & 37 deletions lib/generators/rails_services/service/service_generator.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe 'My behaviour' do
it 'should do something' do
expect(true).to be_falsey
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe 'My behaviour' do

it 'should do something' do
expect(true).to be_falsey
end

end

0 comments on commit c5ce83d

Please sign in to comment.