Skip to content

Commit 84750b0

Browse files
committed
initial copy from ams
1 parent fa57965 commit 84750b0

14 files changed

+720
-0
lines changed

.codeclimate.yml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# This is a sample .codeclimate.yml configured for Engine analysis on Code
2+
# Climate Platform. For an overview of the Code Climate Platform, see here:
3+
# http://docs.codeclimate.com/article/300-the-codeclimate-platform
4+
5+
# Under the engines key, you can configure which engines will analyze your repo.
6+
# Each key is an engine name. For each value, you need to specify enabled: true
7+
# to enable the engine as well as any other engines-specific configuration.
8+
9+
# For more details, see here:
10+
# http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
11+
12+
# For a list of all available engines, see here:
13+
# http://docs.codeclimate.com/article/296-engines-available-engines
14+
15+
engines:
16+
# to turn on an engine, add it here and set enabled to `true`
17+
# to turn off an engine, set enabled to `false` or remove it
18+
rubocop:
19+
enabled: true
20+
# golint:
21+
# enabled: true
22+
# gofmt:
23+
# enabled: true
24+
# eslint:
25+
# enabled: true
26+
# csslint:
27+
# enabled: true
28+
29+
# Engines can analyze files and report issues on them, but you can separately
30+
# decide which files will receive ratings based on those issues. This is
31+
# specified by path patterns under the ratings key.
32+
33+
# For more details see here:
34+
# http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
35+
36+
# Note: If the ratings key is not specified, this will result in a 0.0 GPA on your dashboard.
37+
38+
ratings:
39+
paths:
40+
- lib/**
41+
# - app/**
42+
# - "**.rb"
43+
# - "**.go"
44+
45+
# You can globally exclude files from being analyzed by any engine using the
46+
# exclude_paths key.
47+
48+
exclude_paths:
49+
- spec/**/*
50+
- vendor/**/*

.rubocop.yml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
AllCops:
2+
Exclude:
3+
- config/initializers/forbidden_yaml.rb
4+
- !ruby/regexp /(vendor|bundle|bin|db|tmp)\/.*/
5+
DisplayCopNames: true
6+
DisplayStyleGuide: true
7+
8+
Lint/AssignmentInCondition:
9+
Enabled: false
10+
11+
Lint/NestedMethodDefinition:
12+
Enabled: false
13+
Exclude:
14+
- test/action_controller/serialization_test.rb
15+
16+
Style/Alias:
17+
EnforcedStyle: prefer_alias_method
18+
19+
Style/FrozenStringLiteralComment:
20+
EnforcedStyle: always
21+
22+
Style/StringLiterals:
23+
EnforcedStyle: single_quotes
24+
25+
Metrics/AbcSize:
26+
Max: 35 # TODO: Lower to 15
27+
28+
Metrics/ClassLength:
29+
Max: 261 # TODO: Lower to 100
30+
Exclude:
31+
- test/**/*.rb
32+
33+
Metrics/CyclomaticComplexity:
34+
Max: 7 # TODO: Lower to 6
35+
36+
Metrics/LineLength:
37+
Max: 110 # TODO: Lower to 80
38+
39+
Metrics/MethodLength:
40+
Max: 25
41+
42+
Metrics/PerceivedComplexity:
43+
Max: 9 # TODO: Lower to 7
44+
45+
Style/AlignParameters:
46+
EnforcedStyle: with_fixed_indentation
47+
48+
Style/ClassAndModuleChildren:
49+
EnforcedStyle: nested
50+
51+
Style/Documentation:
52+
Enabled: false
53+
54+
Style/DoubleNegation:
55+
Enabled: false
56+
57+
Style/MissingElse:
58+
Enabled: false # TODO: maybe enable this?
59+
EnforcedStyle: case
60+
61+
Style/EmptyElse:
62+
EnforcedStyle: empty
63+
64+
Style/MultilineOperationIndentation:
65+
EnforcedStyle: indented
66+
67+
Style/BlockDelimiters:
68+
Enabled: true
69+
EnforcedStyle: line_count_based
70+
71+
Style/PredicateName:
72+
Enabled: false # TODO: enable with correct prefixes
73+
74+
Style/ClassVars:
75+
Enabled: false

.travis.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: ruby
2+
bundler_args: --without guard
3+
rvm:
4+
- "2.3.0"
5+
- "2.3.1"
6+
- ruby-head
7+
script: "bundle exec rake"
8+
addons:
9+
code_climate:
10+
repo_token: '9b1e2d38c6d07358eab4a8a1ad4846df8d7f34bfdb9dc3f885dfc4ca44c16e4c'
11+
branches:
12+
only: master
13+
notifications:
14+
email: false
15+
16+
matrix:
17+
fast_finish: true
18+
allow_failures:
19+
- rvm: ruby-head

Gemfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
source 'https://rubygems.org'
3+
4+
# Specify your gem's dependencies in authorizable.gemspec
5+
gemspec

Gemfile.lock

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
PATH
2+
remote: .
3+
specs:
4+
json_key_transform (0.1)
5+
activesupport
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
activesupport (5.0.0.1)
11+
concurrent-ruby (~> 1.0, >= 1.0.2)
12+
i18n (~> 0.7)
13+
minitest (~> 5.1)
14+
tzinfo (~> 1.1)
15+
ast (2.3.0)
16+
awesome_print (1.7.0)
17+
byebug (9.0.5)
18+
codeclimate-test-reporter (0.6.0)
19+
simplecov (>= 0.7.1, < 1.0.0)
20+
coderay (1.1.1)
21+
concurrent-ruby (1.0.2)
22+
docile (1.1.5)
23+
i18n (0.7.0)
24+
json (2.0.2)
25+
method_source (0.8.2)
26+
minitest (5.9.0)
27+
parser (2.3.1.2)
28+
ast (~> 2.2)
29+
powerpack (0.1.1)
30+
pry (0.10.4)
31+
coderay (~> 1.1.0)
32+
method_source (~> 0.8.1)
33+
slop (~> 3.4)
34+
pry-byebug (3.4.0)
35+
byebug (~> 9.0)
36+
pry (~> 0.10)
37+
rainbow (2.1.0)
38+
rubocop (0.42.0)
39+
parser (>= 2.3.1.1, < 3.0)
40+
powerpack (~> 0.1)
41+
rainbow (>= 1.99.1, < 3.0)
42+
ruby-progressbar (~> 1.7)
43+
unicode-display_width (~> 1.0, >= 1.0.1)
44+
ruby-progressbar (1.8.1)
45+
simplecov (0.12.0)
46+
docile (~> 1.1.0)
47+
json (>= 1.8, < 3)
48+
simplecov-html (~> 0.10.0)
49+
simplecov-html (0.10.0)
50+
slop (3.6.0)
51+
thread_safe (0.3.5)
52+
tzinfo (1.2.2)
53+
thread_safe (~> 0.1)
54+
unicode-display_width (1.1.1)
55+
56+
PLATFORMS
57+
ruby
58+
59+
DEPENDENCIES
60+
awesome_print
61+
codeclimate-test-reporter
62+
json_key_transform!
63+
minitest
64+
pry-byebug
65+
rubocop
66+
67+
BUNDLED WITH
68+
1.13.1

Rakefile

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
require 'bundler/gem_tasks'
3+
4+
# rubocop config copied from AMS
5+
begin
6+
require 'rubocop'
7+
require 'rubocop/rake_task'
8+
rescue LoadError # rubocop:disable Lint/HandleExceptions
9+
else
10+
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
11+
require 'rbconfig'
12+
# https://github.com/bundler/bundler/blob/1b3eb2465a/lib/bundler/constants.rb#L2
13+
windows_platforms = /(msdos|mswin|djgpp|mingw)/
14+
if RbConfig::CONFIG['host_os'] =~ windows_platforms
15+
desc 'No-op rubocop on Windows-- unsupported platform'
16+
task :rubocop do
17+
puts 'Skipping rubocop on Windows'
18+
end
19+
elsif defined?(::Rubinius)
20+
desc 'No-op rubocop to avoid rbx segfault'
21+
task :rubocop do
22+
puts 'Skipping rubocop on rbx due to segfault'
23+
puts 'https://github.com/rubinius/rubinius/issues/3499'
24+
end
25+
else
26+
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
27+
desc 'Execute rubocop'
28+
RuboCop::RakeTask.new(:rubocop) do |task|
29+
task.options = ['--rails', '--display-cop-names', '--display-style-guide']
30+
task.fail_on_error = true
31+
end
32+
end
33+
end
34+
35+
require 'rake/testtask'
36+
37+
Rake::TestTask.new(:test) do |t|
38+
t.libs << 'lib'
39+
t.libs << 'test'
40+
t.pattern = 'test/**/*_test.rb'
41+
t.ruby_opts = ['-r./test/test_helper.rb']
42+
t.ruby_opts << ' -w' unless ENV['NO_WARN'] == 'true'
43+
t.verbose = true
44+
end
45+
46+
task default: [:test, :rubocop]
47+
48+
desc 'CI test task'
49+
task ci: [:default]

json_key_transform.gemspec

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- encoding: utf-8 -*-
2+
# frozen_string_literal: true
3+
4+
# allows bundler to use the gemspec for dependencies
5+
lib = File.expand_path('../lib', __FILE__)
6+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
7+
8+
require 'json_key_transform/version'
9+
10+
Gem::Specification.new do |s|
11+
s.name = 'json_key_transform'
12+
s.version = JsonKeyTransform::VERSION
13+
s.platform = Gem::Platform::RUBY
14+
s.license = 'MIT'
15+
s.authors = ['L. Preston Sego III']
16+
s.email = '[email protected]'
17+
s.homepage = 'https://github.com/NullVoxPopuli/json_key_transform'
18+
s.summary = "JsonKeyTransform-#{JsonKeyTransform::VERSION}"
19+
s.description = 'Extraction of the key_transform abilities of ActiveModelSerializers'
20+
21+
s.files = Dir['CHANGELOG.md', 'LICENSE', 'README.md', 'lib/**/*']
22+
s.require_path = 'lib'
23+
24+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
25+
26+
s.required_ruby_version = '>= 2.0'
27+
28+
s.add_runtime_dependency 'activesupport'
29+
30+
# Quality Control
31+
s.add_development_dependency 'rubocop'
32+
s.add_development_dependency 'codeclimate-test-reporter'
33+
s.add_development_dependency 'minitest'
34+
35+
# Debugging
36+
s.add_development_dependency 'awesome_print'
37+
s.add_development_dependency 'pry-byebug'
38+
end

lib/json_key_transform.rb

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
require 'active_support/core_ext/hash/keys'
3+
require 'active_support/core_ext/string'
4+
5+
require 'json_key_transform/version'
6+
7+
module JsonKeyTransform
8+
module_function
9+
10+
# Transforms values to UpperCamelCase or PascalCase.
11+
#
12+
# @example:
13+
# "some_key" => "SomeKey",
14+
def camel(value)
15+
case value
16+
when Array then value.map { |item| camel(item) }
17+
when Hash then value.deep_transform_keys! { |key| camel(key) }
18+
when Symbol then camel(value.to_s).to_sym
19+
when String then value.underscore.camelize
20+
else value
21+
end
22+
end
23+
24+
# Transforms values to camelCase.
25+
#
26+
# @example:
27+
# "some_key" => "someKey",
28+
def camel_lower(value)
29+
case value
30+
when Array then value.map { |item| camel_lower(item) }
31+
when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
32+
when Symbol then camel_lower(value.to_s).to_sym
33+
when String then value.underscore.camelize(:lower)
34+
else value
35+
end
36+
end
37+
38+
# Transforms values to dashed-case.
39+
# This is the default case for the JsonApi adapter.
40+
#
41+
# @example:
42+
# "some_key" => "some-key",
43+
def dash(value)
44+
case value
45+
when Array then value.map { |item| dash(item) }
46+
when Hash then value.deep_transform_keys! { |key| dash(key) }
47+
when Symbol then dash(value.to_s).to_sym
48+
when String then value.underscore.dasherize
49+
else value
50+
end
51+
end
52+
53+
# Transforms values to underscore_case.
54+
# This is the default case for deserialization in the JsonApi adapter.
55+
#
56+
# @example:
57+
# "some-key" => "some_key",
58+
def underscore(value)
59+
case value
60+
when Array then value.map { |item| underscore(item) }
61+
when Hash then value.deep_transform_keys! { |key| underscore(key) }
62+
when Symbol then underscore(value.to_s).to_sym
63+
when String then value.underscore
64+
else value
65+
end
66+
end
67+
68+
# Returns the value unaltered
69+
def unaltered(value)
70+
value
71+
end
72+
end

lib/json_key_transform/version.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
module JsonKeyTransform
3+
VERSION = '0.1'.freeze
4+
end

0 commit comments

Comments
 (0)