mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Apply Rubocop config from Rails
This commit is contained in:
parent
2771e3225b
commit
2d116f6455
12
Gemfile
12
Gemfile
@ -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
|
||||||
|
|||||||
56
Rakefile
56
Rakefile
@ -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"]
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
require 'ams/version'
|
require "ams/version"
|
||||||
require 'ams/serializer'
|
require "ams/serializer"
|
||||||
|
|||||||
@ -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
|
||||||
@ -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}
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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"]
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user