From 8033627e1bec8b9e7f8bcec43445d09677fedcbc Mon Sep 17 00:00:00 2001 From: Guilherme Moretti Date: Fri, 29 Sep 2023 16:51:27 -0300 Subject: [PATCH] Fix rubocop issues --- packages/ostruct-sanitizer/.rubocop.yml | 8 + packages/ostruct-sanitizer/Gemfile | 9 +- packages/ostruct-sanitizer/Rakefile | 6 +- .../lib/ostruct/sanitizer.rb | 17 +- .../lib/ostruct/sanitizer/version.rb | 2 +- .../ostruct-sanitizer.gemspec | 31 ++-- .../spec/ostruct/sanitizer_spec.rb | 161 +++++++++--------- .../ostruct-sanitizer/spec/spec_helper.rb | 8 +- 8 files changed, 132 insertions(+), 110 deletions(-) create mode 100644 packages/ostruct-sanitizer/.rubocop.yml diff --git a/packages/ostruct-sanitizer/.rubocop.yml b/packages/ostruct-sanitizer/.rubocop.yml new file mode 100644 index 00000000..79901410 --- /dev/null +++ b/packages/ostruct-sanitizer/.rubocop.yml @@ -0,0 +1,8 @@ +require: + - rubocop-powerhome + +AllCops: + TargetRubyVersion: 2.6 + +Style/OpenStructUse: + Enabled: false diff --git a/packages/ostruct-sanitizer/Gemfile b/packages/ostruct-sanitizer/Gemfile index b8a196df..57eff6f1 100644 --- a/packages/ostruct-sanitizer/Gemfile +++ b/packages/ostruct-sanitizer/Gemfile @@ -1,6 +1,11 @@ # frozen_string_literal: true -source 'https://rubygems.org' +source "https://rubygems.org" -# Specify your gem's dependencies in ostruct-sanitizer.gemspec gemspec + +gem "bundler", "~> 2.4.7" +gem "byebug" +gem "rake", "~> 10.0" +gem "rspec", "~> 3.0" +gem "rubocop-powerhome", "0.5.0" diff --git a/packages/ostruct-sanitizer/Rakefile b/packages/ostruct-sanitizer/Rakefile index 3b878b3c..ce952e46 100644 --- a/packages/ostruct-sanitizer/Rakefile +++ b/packages/ostruct-sanitizer/Rakefile @@ -1,11 +1,11 @@ # frozen_string_literal: true -require 'bundler/gem_tasks' +require "bundler/gem_tasks" -require 'rspec/core/rake_task' +require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) -require 'rubocop/rake_task' +require "rubocop/rake_task" RuboCop::RakeTask.new(:rubocop) task default: %i[rubocop spec] diff --git a/packages/ostruct-sanitizer/lib/ostruct/sanitizer.rb b/packages/ostruct-sanitizer/lib/ostruct/sanitizer.rb index 12a36e2b..8f628653 100644 --- a/packages/ostruct-sanitizer/lib/ostruct/sanitizer.rb +++ b/packages/ostruct-sanitizer/lib/ostruct/sanitizer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require 'ostruct' -require 'ostruct/sanitizer/version' +require "ostruct" +require "ostruct/sanitizer/version" module OStruct # Provides a series of sanitization rules to be applied on OpenStruct fields on @@ -55,6 +55,10 @@ def method_missing(method, *args) send(method, args[0]) end + def respond_to_missing?(method) + setter?(method) + end + # Set attribute's value via setter so that any existing sanitization rules # may be applied # @@ -65,7 +69,7 @@ def []=(name, value) send("#{name}=", value) end - private + private def setter?(method) method[/.*(?==\z)/m].to_s.to_sym @@ -98,7 +102,8 @@ module ClassMethods # Registers a sanitization block for a given field # # @param [Array] a list of field names to be sanitized - # @param [#call] block sanitization block to be applied to the current value of each field and returns the new sanitized value + # @param [#call] block sanitization block to be applied to the current value of each field + # and returns the new sanitized value # def sanitize(*fields, &block) @sanitizers ||= {} @@ -125,7 +130,7 @@ def truncate(*fields, length:, strip_whitespaces: true) # @param [Array] a list of field names to be sanitized # def alphanumeric(*fields) - sanitize(*fields) { |value| value.gsub(/[^A-Za-z0-9\s]/, '') } + sanitize(*fields) { |value| value.gsub(/[^A-Za-z0-9\s]/, "") } end # Strips out leading and trailing spaces from the values of the given fields @@ -141,7 +146,7 @@ def strip(*fields) # @param [Array] fields list of fields to be sanitized # def digits(*fields) - sanitize(*fields) { |value| value.to_s.gsub(/[^0-9]/, '') } + sanitize(*fields) { |value| value.to_s.gsub(/[^0-9]/, "") } end end end diff --git a/packages/ostruct-sanitizer/lib/ostruct/sanitizer/version.rb b/packages/ostruct-sanitizer/lib/ostruct/sanitizer/version.rb index e90e7f96..16c761f4 100644 --- a/packages/ostruct-sanitizer/lib/ostruct/sanitizer/version.rb +++ b/packages/ostruct-sanitizer/lib/ostruct/sanitizer/version.rb @@ -2,6 +2,6 @@ module OStruct module Sanitizer - VERSION = '0.7.0' + VERSION = "0.7.0" end end diff --git a/packages/ostruct-sanitizer/ostruct-sanitizer.gemspec b/packages/ostruct-sanitizer/ostruct-sanitizer.gemspec index 02738e49..2fc70ecf 100644 --- a/packages/ostruct-sanitizer/ostruct-sanitizer.gemspec +++ b/packages/ostruct-sanitizer/ostruct-sanitizer.gemspec @@ -1,32 +1,27 @@ # frozen_string_literal: true -lib = File.expand_path('lib', __dir__) +lib = File.expand_path("lib", __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'ostruct/sanitizer/version' +require "ostruct/sanitizer/version" Gem::Specification.new do |spec| - spec.name = 'ostruct-sanitizer' + spec.name = "ostruct-sanitizer" spec.version = OStruct::Sanitizer::VERSION - spec.authors = ['Diego Borges'] - spec.email = ['drborges.cic@gmail.com'] + spec.authors = ["Diego Borges"] + spec.email = ["drborges.cic@gmail.com"] - spec.summary = 'Provides Rails-like sanitization rules for OpenStruct fields.' - spec.description = 'Provides Rails-like sanitization rules for OpenStruct fields.' - spec.homepage = 'https://github.com/powerhome/ostruct-sanitizer' - spec.license = 'MIT' + spec.summary = "Provides Rails-like sanitization rules for OpenStruct fields." + spec.description = "Provides Rails-like sanitization rules for OpenStruct fields." + spec.homepage = "https://github.com/powerhome/ostruct-sanitizer" + spec.license = "MIT" spec.files = `git ls-files -z`.split("\x0").reject do |f| f.match(%r{^(test|spec|features)/}) end - spec.bindir = 'exe' + spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } - spec.require_paths = ['lib'] + spec.require_paths = ["lib"] - spec.required_ruby_version = '>= 2.6' - - spec.add_development_dependency 'bundler', '~> 2.4.7' - spec.add_development_dependency 'byebug' - spec.add_development_dependency 'rake', '~> 10.0' - spec.add_development_dependency 'rspec', '~> 3.0' - spec.add_development_dependency 'rubocop-powerhome', '0.5.0' + spec.required_ruby_version = ">= 2.6" + spec.metadata["rubygems_mfa_required"] = "true" end diff --git a/packages/ostruct-sanitizer/spec/ostruct/sanitizer_spec.rb b/packages/ostruct-sanitizer/spec/ostruct/sanitizer_spec.rb index f772e42a..f16194e1 100644 --- a/packages/ostruct-sanitizer/spec/ostruct/sanitizer_spec.rb +++ b/packages/ostruct-sanitizer/spec/ostruct/sanitizer_spec.rb @@ -1,166 +1,175 @@ # frozen_string_literal: true -require 'spec_helper' +require "spec_helper" describe OStruct::Sanitizer do - it 'has a version number' do + it "has a version number" do expect(OStruct::Sanitizer::VERSION).not_to be nil end - describe 'invalid usage' do - it 'fails including OStruct::Sanitizer within a non OpenStruct class' do - define_invalid_usage = lambda { - class InvalidUsage - include OStruct::Sanitizer - end + describe "invalid usage" do + it "fails including OStruct::Sanitizer within a non OpenStruct class" do + define_invalid_usage = -> { + Class.new { include OStruct::Sanitizer } } - expect(define_invalid_usage).to raise_error "#{OStruct::Sanitizer.name} can only be used within OpenStruct classes" + expect(define_invalid_usage).to( + raise_error "#{OStruct::Sanitizer.name} can only be used within OpenStruct classes" + ) end end - describe '#truncate' do - class User < OpenStruct - include OStruct::Sanitizer - truncate :first_name, :last_name, length: 10 - truncate :middle_name, length: 3, strip_whitespaces: false + describe "#truncate" do + let(:user_class) do + Class.new(OpenStruct) do + include OStruct::Sanitizer + truncate :first_name, :last_name, length: 10 + truncate :middle_name, length: 3, strip_whitespaces: false + end end let(:user) do - User.new( - first_name: ' first name longer than 10 characters', - last_name: ' last name longer than 10 characters', - middle_name: ' Rose ' + user_class.new( + first_name: " first name longer than 10 characters", + last_name: " last name longer than 10 characters", + middle_name: " Rose " ) end it "truncates user's first name to 10 characters" do - expect(user.first_name).to eq 'first name' + expect(user.first_name).to eq "first name" end it "truncates user's last name to 10 characters" do - expect(user.last_name).to eq 'last name' + expect(user.last_name).to eq "last name" end it "truncates user's middle name without stipping whitespaces" do - expect(user.middle_name).to eq ' Ro' + expect(user.middle_name).to eq " Ro" end - it 'does not sanitize if value is nil' do - user = User.new first_name: nil, last_name: nil + it "does not sanitize if value is nil" do + user = user_class.new first_name: nil, last_name: nil expect(user.first_name).to be nil expect(user.last_name).to be nil end end - describe '#alphanumeric' do - class User < OpenStruct - include OStruct::Sanitizer - alphanumeric :city, :country + describe "#alphanumeric" do + let(:user_class) do + Class.new(OpenStruct) do + include OStruct::Sanitizer + alphanumeric :city, :country + end end let(:user) do - User.new( - city: 'Porto, Alegre!', - country: 'B.r!_a,z#i%l^' + user_class.new( + city: "Porto, Alegre!", + country: "B.r!_a,z#i%l^" ) end it "drops punctuation from user's city name" do - expect(user.city).to eq 'Porto Alegre' + expect(user.city).to eq "Porto Alegre" end it "drops punctuation from user's country name" do - expect(user.country).to eq 'Brazil' + expect(user.country).to eq "Brazil" end - it 'does not sanitize if value is nil' do - user = User.new city: nil, country: nil + it "does not sanitize if value is nil" do + user = user_class.new city: nil, country: nil expect(user.city).to be nil expect(user.country).to be nil end end - describe '#strip' do - class User < OpenStruct - include OStruct::Sanitizer - strip :email, :phone + describe "#strip" do + let(:user_class) do + Class.new(OpenStruct) do + include OStruct::Sanitizer + strip :email, :phone + end end - let(:user) do - User.new( - email: ' drborges.cic@gmail.com ', - phone: ' (55) 51 00000000 ' + user_class.new( + email: " drborges.cic@gmail.com ", + phone: " (55) 51 00000000 " ) end it "strips out leading and trailing spaces from user's email" do - expect(user.email).to eq 'drborges.cic@gmail.com' + expect(user.email).to eq "drborges.cic@gmail.com" end it "strips out leading and trailing spaces from user's phone number" do - expect(user.phone).to eq '(55) 51 00000000' + expect(user.phone).to eq "(55) 51 00000000" end - it 'does not sanitize if value is nil' do - user = User.new email: nil, phone: nil + it "does not sanitize if value is nil" do + user = user_class.new email: nil, phone: nil expect(user.email).to be nil expect(user.phone).to be nil end end - describe '#digits' do - class User < OpenStruct - include OStruct::Sanitizer - digits :ssn, :cell_phone + describe "#digits" do + let(:user_class) do + Class.new(OpenStruct) do + include OStruct::Sanitizer + digits :ssn, :cell_phone + end end let(:user) do - User.new( - ssn: '111-11-1111', - cell_phone: '+1-541-000-0000' + user_class.new( + ssn: "111-11-1111", + cell_phone: "+1-541-000-0000" ) end - it 'keeps only digits from ssn field' do - expect(user.ssn).to eq '111111111' + it "keeps only digits from ssn field" do + expect(user.ssn).to eq "111111111" end - it 'keeps only digits from cell_phone field' do - expect(user.cell_phone).to eq '15410000000' + it "keeps only digits from cell_phone field" do + expect(user.cell_phone).to eq "15410000000" end - it 'does not sanitize if value is nil' do - user = User.new ssn: nil, cell_phone: nil + it "does not sanitize if value is nil" do + user = user_class.new ssn: nil, cell_phone: nil expect(user.ssn).to be nil expect(user.cell_phone).to be nil end end - describe '#sanitize' do - class User < OpenStruct - include OStruct::Sanitizer - sanitize 'my ssn' do |value| - value.to_s.gsub(/[^0-9]/, '') + describe "#sanitize" do + let(:user_class) do + Class.new(OpenStruct) do + include OStruct::Sanitizer + sanitize "my ssn" do |value| + value.to_s.gsub(/[^0-9]/, "") + end end end - context 'hash syntax' do - it 'applies sanitization rules using string as key' do - user = User.new - user['my ssn'] = '111-11-1111' - expect(user['my ssn']).to eq '111111111' + context "hash syntax" do + it "applies sanitization rules using string as key" do + user = user_class.new + user["my ssn"] = "111-11-1111" + expect(user["my ssn"]).to eq "111111111" end - it 'applies sanitization rules using symbol as key' do - user = User.new - user[:"my ssn"] = '111-11-1111' - expect(user['my ssn']).to eq '111111111' + it "applies sanitization rules using symbol as key" do + user = user_class.new + user[:"my ssn"] = "111-11-1111" + expect(user["my ssn"]).to eq "111111111" end - it 'applies sanitization rules using string as key in the constructor' do - user = User.new 'my ssn' => '111-11-1111' - expect(user[:"my ssn"]).to eq '111111111' + it "applies sanitization rules using string as key in the constructor" do + user = user_class.new "my ssn" => "111-11-1111" + expect(user[:"my ssn"]).to eq "111111111" end end end diff --git a/packages/ostruct-sanitizer/spec/spec_helper.rb b/packages/ostruct-sanitizer/spec/spec_helper.rb index b7df9f3d..4baa04fd 100644 --- a/packages/ostruct-sanitizer/spec/spec_helper.rb +++ b/packages/ostruct-sanitizer/spec/spec_helper.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -$LOAD_PATH.unshift File.expand_path('../lib', __dir__) +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) -require 'byebug' -require 'ostruct/sanitizer' +require "byebug" +require "ostruct/sanitizer" -Dir['spec/fixtures/*.rb'].each do |f| +Dir["spec/fixtures/*.rb"].each do |f| require_relative "../#{f}" end