Skip to content

Commit

Permalink
Rubocop auto correct
Browse files Browse the repository at this point in the history
  • Loading branch information
gbmoretti committed Sep 29, 2023
1 parent 390c796 commit 8fc96a0
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 85 deletions.
2 changes: 2 additions & 0 deletions packages/ostruct-sanitizer/Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in ostruct-sanitizer.gemspec
Expand Down
6 changes: 3 additions & 3 deletions packages/ostruct-sanitizer/Rakefile
Original file line number Diff line number Diff line change
@@ -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]
7 changes: 4 additions & 3 deletions packages/ostruct-sanitizer/bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "ostruct/sanitizer"
require 'bundler/setup'
require 'ostruct/sanitizer'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -10,5 +11,5 @@ require "ostruct/sanitizer"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start
29 changes: 15 additions & 14 deletions packages/ostruct-sanitizer/lib/ostruct/sanitizer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "ostruct"
require "ostruct/sanitizer/version"
# frozen_string_literal: true

require 'ostruct'
require 'ostruct/sanitizer/version'

module OStruct
# Provides a series of sanitization rules to be applied on OpenStruct fields on
Expand All @@ -18,9 +20,7 @@ module OStruct
#
module Sanitizer
def self.included(base)
unless base.ancestors.include? OpenStruct
raise "#{self.name} can only be used within OpenStruct classes"
end
raise "#{name} can only be used within OpenStruct classes" unless base.ancestors.include? OpenStruct

base.extend ClassMethods
end
Expand All @@ -45,14 +45,14 @@ def method_missing(method, *args)
# corresponding field
super method, *args

if field = setter?(method)
# override setter logic to apply any existing sanitization rules before
# assigning the new value to the field
override_setter_for(field) if sanitize?(field)
# uses the newly created setter to set the field's value and apply any
# existing sanitization rules
send(method, args[0])
end
return unless (field = setter?(method))

# override setter logic to apply any existing sanitization rules before
# assigning the new value to the field
override_setter_for(field) if sanitize?(field)
# uses the newly created setter to set the field's value and apply any
# existing sanitization rules
send(method, args[0])
end

# Set attribute's value via setter so that any existing sanitization rules
Expand All @@ -79,6 +79,7 @@ def override_setter_for(field)

def sanitize(field, value)
return value if value.nil?

self.class.sanitizers[field].reduce(value) do |current_value, sanitizer|
sanitizer.call(current_value)
end
Expand Down Expand Up @@ -132,7 +133,7 @@ def alphanumeric(*fields)
# @param [Array<Symbol>] fields list of fields to be sanitized
#
def strip(*fields)
sanitize(*fields) { |value| value.strip }
sanitize(*fields, &:strip)
end

# Removes any non-digit character from the values of the given fields
Expand Down
4 changes: 3 additions & 1 deletion packages/ostruct-sanitizer/lib/ostruct/sanitizer/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module OStruct
module Sanitizer
VERSION = "0.7.0"
VERSION = '0.7.0'
end
end
33 changes: 17 additions & 16 deletions packages/ostruct-sanitizer/ostruct-sanitizer.gemspec
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
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 = ["[email protected]"]
spec.authors = ['Diego Borges']
spec.email = ['[email protected]']

spec.summary = %q{Provides Rails-like sanitization rules for OpenStruct fields.}
spec.description = %q{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 "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rubocop-powerhome", "0.5.0"
spec.add_development_dependency "byebug"
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'
end
92 changes: 47 additions & 45 deletions packages/ostruct-sanitizer/spec/ostruct/sanitizer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
require "spec_helper"
# frozen_string_literal: true

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 = -> {
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
Expand All @@ -17,7 +19,7 @@ class InvalidUsage
end
end

describe "#truncate" do
describe '#truncate' do
class User < OpenStruct
include OStruct::Sanitizer
truncate :first_name, :last_name, length: 10
Expand All @@ -26,139 +28,139 @@ class User < OpenStruct

let(:user) do
User.new(
first_name: " first name longer than 10 characters",
last_name: " last name longer than 10 characters",
middle_name: " Rose ",
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
it 'does not sanitize if value is nil' do
user = User.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
describe '#alphanumeric' do
class User < OpenStruct
include OStruct::Sanitizer
alphanumeric :city, :country
end

let(:user) do
User.new(
city: "Porto, Alegre!",
country: "B.r!_a,z#i%l^",
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
it 'does not sanitize if value is nil' do
user = User.new city: nil, country: nil
expect(user.city).to be nil
expect(user.country).to be nil
end
end

describe "#strip" do
describe '#strip' do
class User < OpenStruct
include OStruct::Sanitizer
strip :email, :phone
end

let(:user) do
User.new(
email: " [email protected] ",
phone: " (55) 51 00000000 ",
email: ' [email protected] ',
phone: ' (55) 51 00000000 '
)
end

it "strips out leading and trailing spaces from user's email" do
expect(user.email).to eq "[email protected]"
expect(user.email).to eq '[email protected]'
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
it 'does not sanitize if value is nil' do
user = User.new email: nil, phone: nil
expect(user.email).to be nil
expect(user.phone).to be nil
end
end

describe "#digits" do
describe '#digits' do
class User < OpenStruct
include OStruct::Sanitizer
digits :ssn, :cell_phone
end

let(:user) do
User.new(
ssn: "111-11-1111",
cell_phone: "+1-541-000-0000",
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
it 'does not sanitize if value is nil' do
user = User.new ssn: nil, cell_phone: nil
expect(user.ssn).to be nil
expect(user.cell_phone).to be nil
end
end

describe "#sanitize" do
describe '#sanitize' do
class User < OpenStruct
include OStruct::Sanitizer
sanitize "my ssn" do |value|
sanitize 'my ssn' do |value|
value.to_s.gsub(/[^0-9]/, '')
end
end

context "hash syntax" do
it "applies sanitization rules using string as key" do
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"
user['my ssn'] = '111-11-1111'
expect(user['my ssn']).to eq '111111111'
end

it "applies sanitization rules using symbol as key" do
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"
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.new 'my ssn' => '111-11-1111'
expect(user[:"my ssn"]).to eq '111111111'
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions packages/ostruct-sanitizer/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
# frozen_string_literal: true

require "byebug"
require "ostruct/sanitizer"
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)

require 'byebug'
require 'ostruct/sanitizer'

Dir['spec/fixtures/*.rb'].each do |f|
require_relative "../#{f}"
Expand Down

0 comments on commit 8fc96a0

Please sign in to comment.