Apply Rubocop config from Rails

This commit is contained in:
Benjamin Fleischer 2017-05-15 10:12:34 -05:00
parent 2771e3225b
commit 2d116f6455
14 changed files with 106 additions and 106 deletions

12
Gemfile
View File

@ -1,14 +1,14 @@
# frozen_string_literal: true # frozen_string_literal: true
source 'https://rubygems.org' source "https://rubygems.org"
gemspec gemspec
group :test do group :test do
gem 'm', '~> 1.5' gem "m", "~> 1.5"
gem 'pry', '~> 0.10' gem "pry", "~> 0.10"
gem 'pry-byebug', '~> 3.4', platform: :ruby gem "pry-byebug", "~> 3.4", platform: :ruby
end end
group :development, :test do group :development, :test do
gem 'rubocop', '>= 0.47', require: false gem "rubocop", ">= 0.47", require: false
gem 'yard', require: false gem "yard", require: false
end end

View File

@ -1,9 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'English' require "English"
require 'rake/clean' require "rake/clean"
GEMSPEC = 'ams.gemspec' GEMSPEC = "ams.gemspec"
require File.join(File.dirname(__FILE__), 'lib', 'ams', 'version') require File.join(File.dirname(__FILE__), "lib", "ams", "version")
VERSION = AMS::VERSION VERSION = AMS::VERSION
VERSION_TAG = "AMS_#{VERSION}" VERSION_TAG = "AMS_#{VERSION}"
GEMPATH = "AMS-#{VERSION}.gem" GEMPATH = "AMS-#{VERSION}.gem"
@ -12,67 +12,67 @@ CLOBBER << GEMPATH
SOURCE_FILES = Rake::FileList.new(GEMSPEC) SOURCE_FILES = Rake::FileList.new(GEMSPEC)
rule '.gem' => '.gemspec' do rule ".gem" => ".gemspec" do
sh "gem build -V #{GEMSPEC}" sh "gem build -V #{GEMSPEC}"
end end
desc 'build gem' desc "build gem"
task build: [:clobber, SOURCE_FILES.ext('.gem')] task build: [:clobber, SOURCE_FILES.ext(".gem")]
desc 'install gem' desc "install gem"
task install: :build do task install: :build do
sh "gem install #{GEMPATH}" sh "gem install #{GEMPATH}"
end end
desc 'uninstall gem' desc "uninstall gem"
task :uninstall do task :uninstall do
sh 'gem uninstall -aIx AMS' sh "gem uninstall -aIx AMS"
end end
desc 'test install message' desc "test install message"
task :test_install do task :test_install do
gemspec = Gem::Specification.load(GEMSPEC) gemspec = Gem::Specification.load(GEMSPEC)
puts "You should see post install message '#{gemspec.post_install_message}' below:" puts "You should see post install message '#{gemspec.post_install_message}' below:"
begin begin
Rake::Task['install'].invoke Rake::Task["install"].invoke
ensure ensure
Rake::Task['uninstall'].invoke Rake::Task["uninstall"].invoke
end end
puts "#{GEMSPEC} => #{GEMPATH}" puts "#{GEMSPEC} => #{GEMPATH}"
end end
desc 'abort when repo not clean or has uncommited code' desc "abort when repo not clean or has uncommited code"
task :assert_clean_repo do task :assert_clean_repo do
sh 'git diff --exit-code' sh "git diff --exit-code"
abort 'Git repo not clean' unless $CHILD_STATUS.success? abort "Git repo not clean" unless $CHILD_STATUS.success?
sh 'git diff-index --quiet --cached HEAD' sh "git diff-index --quiet --cached HEAD"
abort 'Git repo not commited' unless $CHILD_STATUS.success? abort "Git repo not commited" unless $CHILD_STATUS.success?
end end
task push_and_tag: [:build] do task push_and_tag: [:build] do
sh "gem push #{GEMPATH}" sh "gem push #{GEMPATH}"
if $CHILD_STATUS.success? if $CHILD_STATUS.success?
Rake::Task['tag_globally'].invoke Rake::Task["tag_globally"].invoke
else else
abort 'tagging aborted; pushing gem failed.' abort "tagging aborted; pushing gem failed."
end end
end end
task :tag_globally do task :tag_globally do
sh "git tag -a -m \"Version #{VERSION}\" #{VERSION_TAG}" sh "git tag -a -m \"Version #{VERSION}\" #{VERSION_TAG}"
STDOUT.puts "Tagged #{VERSION_TAG}." STDOUT.puts "Tagged #{VERSION_TAG}."
sh 'git push' sh "git push"
sh 'git push --tags' sh "git push --tags"
end end
desc 'Release' desc "Release"
task release: [:assert_clean_repo, :push_and_tag] task release: [:assert_clean_repo, :push_and_tag]
import('lib/tasks/rubocop.rake') import("lib/tasks/rubocop.rake")
import('lib/tasks/doc.rake') import("lib/tasks/doc.rake")
import('lib/tasks/test.rake') import("lib/tasks/test.rake")
task default: [:test, :rubocop] task default: [:test, :rubocop]
desc 'CI test task' desc "CI test task"
task ci: [:default, 'doc:stats'] task ci: [:default, "doc:stats"]

View File

@ -1,3 +1,3 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'ams/version' require "ams/version"
require 'ams/serializer' require "ams/serializer"

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'json' require "json"
module AMS module AMS
# Lightweight mapping of a model to a JSON API resource object # Lightweight mapping of a model to a JSON API resource object
# with attributes and relationships # with attributes and relationships
@ -38,7 +38,7 @@ module AMS
class << self class << self
attr_accessor :_attributes, :_relations, :_id_field, :_type attr_accessor :_attributes, :_relations, :_id_field, :_type
def add_instance_method(body, receiver=self) def add_instance_method(body, receiver = self)
cl = caller_locations[0] cl = caller_locations[0]
silence_warnings { receiver.module_eval body, cl.absolute_path, cl.lineno } silence_warnings { receiver.module_eval body, cl.absolute_path, cl.lineno }
end end
@ -60,7 +60,7 @@ module AMS
super super
base._attributes = _attributes.dup base._attributes = _attributes.dup
base._relations = _relations.dup base._relations = _relations.dup
base._type = base.name.split('::')[-1].sub('Serializer', '').downcase base._type = base.name.split("::")[-1].sub("Serializer", "").downcase
add_class_method "def class; #{base}; end", base add_class_method "def class; #{base}; end", base
add_instance_method "def id; object.id; end", base add_instance_method "def id; object.id; end", base
end end
@ -79,7 +79,7 @@ module AMS
end end
def attribute(attribute_name, key: attribute_name) def attribute(attribute_name, key: attribute_name)
fail 'ForbiddenKey' if attribute_name == :id fail "ForbiddenKey" if attribute_name == :id
_attributes[attribute_name] = { key: key } _attributes[attribute_name] = { key: key }
add_instance_method <<-METHOD add_instance_method <<-METHOD
def #{attribute_name} def #{attribute_name}

View File

@ -1,4 +1,4 @@
# frozen_string_literal: true # frozen_string_literal: true
module AMS module AMS
VERSION = '0.99.0' VERSION = "0.99.0"
end end

View File

@ -1,26 +1,26 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'yard' require "yard"
namespace :doc do namespace :doc do
desc 'start a gem server' desc "start a gem server"
task :server do task :server do
sh 'bundle exec yard server --gems' sh "bundle exec yard server --gems"
end end
desc 'use Graphviz to generate dot graph' desc "use Graphviz to generate dot graph"
task :graph do task :graph do
output_file = 'doc/erd.dot' output_file = "doc/erd.dot"
sh "bundle exec yard graph --protected --full --dependencies > #{output_file}" sh "bundle exec yard graph --protected --full --dependencies > #{output_file}"
puts 'open doc/erd.dot if you have graphviz installed' puts "open doc/erd.dot if you have graphviz installed"
end end
YARD::Rake::YardocTask.new(:stats) do |t| YARD::Rake::YardocTask.new(:stats) do |t|
t.stats_options = ['--list-undoc'] t.stats_options = ["--list-undoc"]
end end
DOC_PATH = File.join('doc') DOC_PATH = File.join("doc")
YARD::Rake::YardocTask.new(:pages) do |t| YARD::Rake::YardocTask.new(:pages) do |t|
t.options = ['-o', DOC_PATH] t.options = ["-o", DOC_PATH]
end end
end end
task doc: ['doc:pages'] task doc: ["doc:pages"]

View File

@ -1,53 +1,53 @@
# frozen_string_literal: true # frozen_string_literal: true
begin begin
require 'rubocop' require "rubocop"
require 'rubocop/rake_task' require "rubocop/rake_task"
rescue LoadError # rubocop:disable Lint/HandleExceptions rescue LoadError # rubocop:disable Lint/HandleExceptions
else else
require 'rbconfig' require "rbconfig"
# https://github.com/bundler/bundler/blob/1b3eb2465a/lib/bundler/constants.rb#L2 # https://github.com/bundler/bundler/blob/1b3eb2465a/lib/bundler/constants.rb#L2
windows_platforms = /(msdos|mswin|djgpp|mingw)/ windows_platforms = /(msdos|mswin|djgpp|mingw)/
if RbConfig::CONFIG['host_os'] =~ windows_platforms if RbConfig::CONFIG["host_os"] =~ windows_platforms
desc 'No-op rubocop on Windows-- unsupported platform' desc "No-op rubocop on Windows-- unsupported platform"
task :rubocop do task :rubocop do
puts 'Skipping rubocop on Windows' puts "Skipping rubocop on Windows"
end end
elsif defined?(::Rubinius) elsif defined?(::Rubinius)
desc 'No-op rubocop to avoid rbx segfault' desc "No-op rubocop to avoid rbx segfault"
task :rubocop do task :rubocop do
puts 'Skipping rubocop on rbx due to segfault' puts "Skipping rubocop on rbx due to segfault"
puts 'https://github.com/rubinius/rubinius/issues/3499' puts "https://github.com/rubinius/rubinius/issues/3499"
end end
else else
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop) Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
patterns = [ patterns = [
'Gemfile', "Gemfile",
'Rakefile', "Rakefile",
'lib/**/*.{rb,rake}', "lib/**/*.{rb,rake}",
'config/**/*.rb', "config/**/*.rb",
'app/**/*.rb', "app/**/*.rb",
'test/**/*.rb' "test/**/*.rb"
] ]
desc 'Execute rubocop' desc "Execute rubocop"
RuboCop::RakeTask.new(:rubocop) do |task| RuboCop::RakeTask.new(:rubocop) do |task|
task.options = ['--rails', '--display-cop-names', '--display-style-guide'] task.options = ["--rails", "--display-cop-names", "--display-style-guide"]
task.formatters = ['progress'] task.formatters = ["progress"]
task.patterns = patterns task.patterns = patterns
task.fail_on_error = true task.fail_on_error = true
end end
namespace :rubocop do namespace :rubocop do
desc 'Auto-gen rubocop config' desc "Auto-gen rubocop config"
task :auto_gen_config do task :auto_gen_config do
options = ['--auto-gen-config'].concat patterns options = ["--auto-gen-config"].concat patterns
require 'benchmark' require "benchmark"
result = 0 result = 0
cli = RuboCop::CLI.new cli = RuboCop::CLI.new
time = Benchmark.realtime do time = Benchmark.realtime do
result = cli.run(options) result = cli.run(options)
end end
puts "Finished in #{time} seconds" if cli.options[:debug] puts "Finished in #{time} seconds" if cli.options[:debug]
abort('RuboCop failed!') if result.nonzero? abort("RuboCop failed!") if result.nonzero?
end end
end end
end end

View File

@ -1,11 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rake/testtask' require "rake/testtask"
Rake::TestTask.new(:test) do |t| Rake::TestTask.new(:test) do |t|
t.libs << 'lib' t.libs << "lib"
t.libs << 'test' t.libs << "test"
t.pattern = 'test/**/*_test.rb' t.pattern = "test/**/*_test.rb"
t.ruby_opts = ['-r./test/test_helper.rb'] t.ruby_opts = ["-r./test/test_helper.rb"]
t.ruby_opts << ' -w' unless ENV['NO_WARN'] == 'true' t.ruby_opts << " -w" unless ENV["NO_WARN"] == "true"
t.verbose = true t.verbose = true
end end

View File

@ -50,7 +50,7 @@ class PlainModel
end end
end end
class ParentModel< PlainModel class ParentModel < PlainModel
attributes :id, :name, :description attributes :id, :name, :description
associations :child_models associations :child_models
end end

View File

@ -1,9 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'bundler/setup' require "bundler/setup"
require 'simplecov' require "simplecov"
require 'minitest/autorun' require "minitest/autorun"
require 'ams' require "ams"
require 'fixtures/poro' require "fixtures/poro"
module AMS module AMS
class Test < Minitest::Test class Test < Minitest::Test

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'test_helper' require "test_helper"
module AMS module AMS
class Serializer class Serializer
@ -9,16 +9,16 @@ module AMS
type :profiles type :profiles
attribute :name attribute :name
attribute :description, key: :summary attribute :description, key: :summary
relation :child_models, type: :comments, to: :many, ids: 'object.child_models.map(&:id)' relation :child_models, type: :comments, to: :many, ids: "object.child_models.map(&:id)"
end end
def setup def setup
super super
@relation = ChildModel.new(id: 2, name: 'comment') @relation = ChildModel.new(id: 2, name: "comment")
@object = ParentModel.new( @object = ParentModel.new(
id: 1, id: 1,
name: 'name', name: "name",
description: 'description', description: "description",
child_models: [@relation] child_models: [@relation]
) )
@serializer_class = ParentModelSerializer @serializer_class = ParentModelSerializer
@ -28,9 +28,9 @@ module AMS
def test_model_instance_as_json def test_model_instance_as_json
expected = { expected = {
id: 1, type: :profiles, id: 1, type: :profiles,
attributes: {name: "name", summary: "description"}, attributes: { name: "name", summary: "description" },
relationships: relationships:
{child_models: [{data: {id: 2, type: "comments"}}]} { child_models: [{ data: { id: 2, type: "comments" } }] }
} }
assert_equal expected, @serializer_instance.as_json assert_equal expected, @serializer_instance.as_json
end end
@ -38,9 +38,9 @@ module AMS
def test_model_instance_to_json def test_model_instance_to_json
expected = { expected = {
id: 1, type: :profiles, id: 1, type: :profiles,
attributes: {name: "name", summary: "description"}, attributes: { name: "name", summary: "description" },
relationships: relationships:
{child_models: [{data: {id: 2, type: "comments"}}]} { child_models: [{ data: { id: 2, type: "comments" } }] }
}.to_json }.to_json
assert_equal expected, @serializer_instance.to_json assert_equal expected, @serializer_instance.to_json
end end

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'test_helper' require "test_helper"
module AMS module AMS
class Serializer class Serializer
@ -13,8 +13,8 @@ module AMS
super super
@object = ParentModel.new( @object = ParentModel.new(
id: 1, id: 1,
name: 'name', name: "name",
description: 'description' description: "description"
) )
@serializer_class = ParentModelSerializer @serializer_class = ParentModelSerializer
@serializer_instance = @serializer_class.new(@object) @serializer_instance = @serializer_class.new(@object)
@ -22,8 +22,8 @@ module AMS
def test_model_instance_attributes def test_model_instance_attributes
expected_attributes = { expected_attributes = {
name: 'name', name: "name",
summary: 'description' summary: "description"
} }
assert_equal expected_attributes, @serializer_instance.attributes assert_equal expected_attributes, @serializer_instance.attributes
end end

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'test_helper' require "test_helper"
module AMS module AMS
class Serializer class Serializer
@ -10,7 +10,7 @@ module AMS
def setup def setup
super super
@object = ParentModel.new( id: 1,) @object = ParentModel.new(id: 1,)
@serializer_class = ParentModelSerializer @serializer_class = ParentModelSerializer
@serializer_instance = @serializer_class.new(@object) @serializer_instance = @serializer_class.new(@object)
end end

View File

@ -1,16 +1,16 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'test_helper' require "test_helper"
module AMS module AMS
class Serializer class Serializer
class RelationshipsTest < Test class RelationshipsTest < Test
class ParentModelSerializer < Serializer class ParentModelSerializer < Serializer
relation :child_models, type: :comments, to: :many, ids: 'object.child_models.map(&:id)' relation :child_models, type: :comments, to: :many, ids: "object.child_models.map(&:id)"
end end
def setup def setup
super super
@relation = ChildModel.new(id: 2, name: 'comment') @relation = ChildModel.new(id: 2, name: "comment")
@object = ParentModel.new( @object = ParentModel.new(
child_models: [@relation] child_models: [@relation]
) )
@ -21,7 +21,7 @@ module AMS
def test_model_instance_relations def test_model_instance_relations
expected_relations = { expected_relations = {
child_models: [{ child_models: [{
data: {type: 'comments', id: 2 } data: { type: "comments", id: 2 }
}] }]
} }
assert_equal expected_relations, @serializer_instance.relations assert_equal expected_relations, @serializer_instance.relations
@ -29,7 +29,7 @@ module AMS
def test_model_instance_relationship_object def test_model_instance_relationship_object
expected = { expected = {
data: {type: :bananas, id: 5 } data: { type: :bananas, id: 5 }
} }
assert_equal expected, @serializer_instance.relationship_object(5, :bananas) assert_equal expected, @serializer_instance.relationship_object(5, :bananas)
end end