Skip to content

Commit

Permalink
configure rubocop to enforce coding style on the project
Browse files Browse the repository at this point in the history
  • Loading branch information
avsej committed Sep 29, 2022
1 parent 2a8aaee commit 5ba2c11
Show file tree
Hide file tree
Showing 87 changed files with 4,316 additions and 4,136 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: linters

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
rubocop:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set up ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7
- name: Install dependencies
run: bundle install
- name: Run rubocop
run: bundle exec rubocop
39 changes: 39 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
inherit_from: .rubocop_todo.yml
require:
- rubocop-rspec
- rubocop-performance
- rubocop-rake
- rubocop-thread_safety

AllCops:
TargetRubyVersion: 2.6
NewCops: enable
SuggestExtensions: false
Exclude:
- 'vendor/**/*'

Layout/LineLength:
Max: 170

Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space
EnforcedStyleForEmptyBraces: no_space

Style/Lambda:
EnforcedStyle: lambda

Style/SymbolArray:
EnforcedStyle: brackets

Style/RegexpLiteral:
EnforcedStyle: mixed
AllowInnerSlashes: true

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: comma

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: comma

Layout/FirstHashElementIndentation:
EnforcedStyle: consistent
92 changes: 92 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# TODO: gradually revisit these settings.
# Ideally the code should be refactored so that this file will be empty

Metrics/ParameterLists:
Max: 30

Metrics/BlockNesting:
Max: 4

Metrics/ModuleLength:
Max: 150

Metrics/ClassLength:
Max: 150

Metrics/MethodLength:
Max: 80

RSpec/ExampleLength:
Max: 40

RSpec/MultipleExpectations:
Max: 20

RSpec/InstanceVariable:
Enabled: false

RSpec/LeakyConstantDeclaration:
Enabled: false

RSpec/MultipleDescribes:
Enabled: false

RSpec/BeforeAfterAll:
Enabled: false

RSpec/MessageSpies:
Enabled: false

Style/Documentation:
Enabled: false

Metrics/AbcSize:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

ThreadSafety/InstanceVariableInClassMethod:
Enabled: false

ThreadSafety/NewThread:
Enabled: false

Lint/UnderscorePrefixedVariableName:
Enabled: false

Style/OptionalBooleanParameter:
Enabled: false

Naming/ClassAndModuleCamelCase:
Enabled: false

Naming/MemoizedInstanceVariableName:
Enabled: false

Naming/MethodName:
Enabled: false

Naming/MethodParameterName:
Enabled: false

Naming/PredicateName:
Enabled: false

Style/ClassVars:
Enabled: false

Style/MissingRespondToMissing:
Enabled: false

ThreadSafety/ClassAndModuleAttributes:
Enabled: false

Naming/FileName:
Enabled: false

Lint/ConstantDefinitionInBlock:
Enabled: false
12 changes: 10 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# frozen_string_literal: true

git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
"https://github.com/#{repo_name}.git"
end
source "https://rubygems.org"
gemspec
source 'https://rubygems.org'
gemspec

gem 'rubocop'
gem 'rubocop-performance'
gem 'rubocop-rake'
gem 'rubocop-rspec'
gem 'rubocop-thread_safety'
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# frozen_string_literal: true

require 'rubygems'
require 'rspec/core/rake_task' # testing framework
require 'yard' # yard documentation

# By default we don't run network tests
task :default => :test
task default: :test

RSpec::Core::RakeTask.new(:spec)

desc 'Run all tests'
task :test => [:spec]
task test: [:spec]

YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb', '-', 'README.md']
t.files = ['lib/**/*.rb', '-', 'README.md']
end
48 changes: 25 additions & 23 deletions couchbase-orm.gemspec
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
require File.expand_path("../lib/couchbase-orm/version", __FILE__)
# frozen_string_literal: true

require File.expand_path('lib/couchbase_orm/version', __dir__)

Gem::Specification.new do |gem|
gem.name = "couchbase-orm"
gem.version = CouchbaseOrm::VERSION
gem.license = 'MIT'
gem.authors = ["Stephen von Takach"]
gem.email = ["[email protected]"]
gem.homepage = "https://github.com/cotag/couchbase-orm"
gem.summary = "Couchbase ORM for Rails"
gem.description = "A Couchbase ORM for Rails"
gem.name = 'couchbase-orm'
gem.version = CouchbaseOrm::VERSION
gem.license = 'MIT'
gem.authors = ['Stephen von Takach']
gem.email = ['[email protected]']
gem.homepage = 'https://github.com/cotag/couchbase-orm'
gem.summary = 'Couchbase ORM for Rails'
gem.description = 'A Couchbase ORM for Rails'

gem.required_ruby_version = '>= 2.1.0'
gem.require_paths = ["lib"]
gem.required_ruby_version = '>= 2.6.0'
gem.require_paths = ['lib']

gem.add_runtime_dependency 'activemodel', ENV["ACTIVE_MODEL_VERSION"] || '>= 5.2'
gem.add_runtime_dependency 'activerecord', ENV["ACTIVE_MODEL_VERSION"] || '>= 5.2'
gem.add_runtime_dependency 'activemodel', ENV['ACTIVE_MODEL_VERSION'] || '>= 5.2'
gem.add_runtime_dependency 'activerecord', ENV['ACTIVE_MODEL_VERSION'] || '>= 5.2'

gem.add_runtime_dependency 'couchbase'
gem.add_runtime_dependency 'radix', '~> 2.2' # converting numbers to and from any base
gem.add_runtime_dependency 'couchbase'
gem.add_runtime_dependency 'radix', '~> 2.2' # converting numbers to and from any base

gem.add_development_dependency 'rake', '~> 12.2'
gem.add_development_dependency 'rspec', '~> 3.7'
gem.add_development_dependency 'yard', '~> 0.9'
gem.add_development_dependency 'pry'
gem.add_development_dependency 'pry-stack_explorer'
gem.add_development_dependency 'simplecov'
gem.add_development_dependency 'pry'
gem.add_development_dependency 'pry-stack_explorer'
gem.add_development_dependency 'rake', '~> 12.2'
gem.add_development_dependency 'rspec', '~> 3.7'
gem.add_development_dependency 'simplecov'
gem.add_development_dependency 'yard', '~> 0.9'

gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.files = `git ls-files`.split("\n")
gem.metadata['rubygems_mfa_required'] = 'true'
end
90 changes: 2 additions & 88 deletions lib/couchbase-orm.rb
Original file line number Diff line number Diff line change
@@ -1,89 +1,3 @@
# frozen_string_literal: true, encoding: ASCII-8BIT
require "logger"
require "active_support/lazy_load_hooks"

ActiveSupport.on_load(:i18n) do
I18n.load_path << File.expand_path("couchbase-orm/locale/en.yml", __dir__)
end

module CouchbaseOrm
autoload :Error, 'couchbase-orm/error'
autoload :Connection, 'couchbase-orm/connection'
autoload :IdGenerator, 'couchbase-orm/id_generator'
autoload :Base, 'couchbase-orm/base'
autoload :HasMany, 'couchbase-orm/utilities/has_many'

def self.logger
@@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT).tap { |l| l.level = Logger::INFO unless ENV["COUCHBASE_ORM_DEBUG"] }
end

def self.logger=(logger)
@@logger = logger
end

def self.try_load(id)
result = nil
was_array = id.is_a?(Array)
if was_array && id.length == 1
query_id = id.first
else
query_id = id
end

result = query_id.is_a?(Array) ? CouchbaseOrm::Base.bucket.default_collection.get_multi(query_id) : CouchbaseOrm::Base.bucket.default_collection.get(query_id)

result = Array.wrap(result) if was_array

if result&.is_a?(Array)
return result.zip(id).map { |r, id| try_load_create_model(r, id) }.compact
end

return try_load_create_model(result, id)
end

private

def self.try_load_create_model(result, id)
ddoc = result&.content["type"]
return nil unless ddoc
::CouchbaseOrm::Base.descendants.each do |model|
if model.design_document == ddoc
return model.new(result, id: id)
end
end
nil
end
end

# Provide Boolean conversion function
# See: http://www.virtuouscode.com/2012/05/07/a-ruby-conversion-idiom/
module Kernel
private

def Boolean(value)
case value
when String, Symbol
case value.to_s.strip.downcase
when 'true'
return true
when 'false'
return false
end
when Integer
return value != 0
when false, nil
return false
when true
return true
end

raise ArgumentError, "invalid value for Boolean(): \"#{value.inspect}\""
end
end
class Boolean < TrueClass; end

# If we are using Rails then we will include the Couchbase railtie.
if defined?(Rails)
require 'couchbase-orm/railtie'
end
# frozen_string_literal: true

require 'couchbase_orm'
Loading

0 comments on commit 5ba2c11

Please sign in to comment.