Skip to content

Commit

Permalink
use same implementation for all Hash like objects, DRY code
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed Aug 3, 2017
1 parent 97d25a4 commit eb544ca
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 144 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
1.2.0
-----
* `HashWithIndifferentAccess` and `Hash` do not need different implementations. Same module can be included in both and achieve the correct results. To mixin the methods to any `Hash`-like class just `include Forbidium`

1.1.1
-----
Expand Down
18 changes: 5 additions & 13 deletions benchmarks/forbidium.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,29 @@
require 'benchmark'
require 'bundler/setup'
require 'forbidium'
require 'action_pack'
require 'active_support'
require 'action_controller'

ActionController::Parameters.class_eval do
include Forbidium::ActionController::Parameters
include Forbidium
end

hash = {}

1_000_000.times do |n|
hash[n.to_s] = n
hash[n] = n
end

params = ActionController::Parameters.new({})
params_filter = {}

1_000_000.times do |n|
params[n] = n
params_filter[n] = n
end
params = ActionController::Parameters.new(hash)

Benchmark.bmbm do |bm|
bm.report('Hash#allow') { hash.allow(hash) }
bm.report('Hash#forbid') { hash.forbid(hash) }

bm.report('ActionController::Parameters#allow') do
params.allow(params_filter)
params.allow(hash)
end

bm.report('ActionController::Parameters#forbid') do
params.forbid(params_filter)
params.forbid(hash)
end
end
2 changes: 0 additions & 2 deletions lib/forbidium.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

require 'forbidium/version'
require 'forbidium/forbidium'
require 'forbidium/action_controller/parameters'
require 'forbidium/hash'
require 'forbidium/core_ext/hash'
require 'forbidium/railtie' if defined? Rails
34 changes: 0 additions & 34 deletions lib/forbidium/action_controller/parameters.rb

This file was deleted.

17 changes: 17 additions & 0 deletions lib/forbidium/allow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Forbidium
# Adds the #allow and #allow! methods
module Allow
def allow(filters = {})
dup.allow!(filters)
end

def allow!(filters = {})
filters.each do |key, val|
delete(key) unless Array(val).include?(self[key])
end
self
end
end
end
2 changes: 1 addition & 1 deletion lib/forbidium/core_ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

# Add #allow, #allow!, #forbid, and #forbid! to Hash
class Hash
include Forbidium::Hash
include Forbidium
end
17 changes: 17 additions & 0 deletions lib/forbidium/forbid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Forbidium
# Adds the #forbid and #forbid! methods
module Forbid
def forbid(filters = {})
dup.forbid!(filters)
end

def forbid!(filters = {})
filters.each do |key, val|
delete(key) if Array(val).include?(self[key])
end
self
end
end
end
16 changes: 5 additions & 11 deletions lib/forbidium/forbidium.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require 'forbidium/allow'
require 'forbidium/forbid'

# Filter hashes by setting allowed or forbidden values for specific keys.
#
# hash = { one: 'one', two: 'two' }
Expand All @@ -18,15 +21,6 @@
#
# hash # => {}
module Forbidium
module Allow # :nodoc:
def allow(filters = {})
dup.allow!(filters)
end
end

module Forbid # :nodoc:
def forbid(filters = {})
dup.forbid!(filters)
end
end
include Forbid
include Allow
end
32 changes: 0 additions & 32 deletions lib/forbidium/hash.rb

This file was deleted.

4 changes: 1 addition & 3 deletions lib/forbidium/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ module Forbidium
class Railtie < ::Rails::Railtie
initializer :forbidium do
ActiveSupport.on_load :action_controller do
::ActionController::Parameters.send(
:include, Forbidium::ActionController::Parameters
)
ActionController::Parameters.send :include, Forbidium
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/forbidium/allow_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe Forbidium::Hash::Allow do
describe Forbidium::Allow do
let(:symbol_hash) { { hi: 'hi', hello: 'hello' } }
let(:string_hash) { { 'hi' => 'hi', 'hello' => 'hello' } }

Expand All @@ -11,7 +11,7 @@ class AllowTestObject; end
expect(AllowTestObject.new).not_to respond_to :allow
expect(AllowTestObject.new).not_to respond_to :allow!

AllowTestObject.class_eval { include Forbidium::Hash::Allow }
AllowTestObject.class_eval { include Forbidium::Allow }

expect(AllowTestObject.new).to respond_to :allow
expect(AllowTestObject.new).to respond_to :allow!
Expand Down
4 changes: 2 additions & 2 deletions spec/forbidium/forbid_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe Forbidium::Hash::Forbid do
describe Forbidium::Forbid do
let(:symbol_hash) { { hi: 'hi', hello: 'hello' } }
let(:string_hash) { { 'hi' => 'hi', 'hello' => 'hello' } }

Expand All @@ -11,7 +11,7 @@ class ForbidTestObject; end
expect(ForbidTestObject.new).not_to respond_to :forbid
expect(ForbidTestObject.new).not_to respond_to :forbid!

ForbidTestObject.class_eval { include Forbidium::Hash::Forbid }
ForbidTestObject.class_eval { include Forbidium::Forbid }

expect(ForbidTestObject.new).to respond_to :forbid
expect(ForbidTestObject.new).to respond_to :forbid!
Expand Down
48 changes: 8 additions & 40 deletions spec/forbidium_spec.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,19 @@
# frozen_string_literal: true

describe Forbidium do
context 'Forbidium::ActionController::Parameters' do
it 'includes namespaced Allow and Forbid modules when included' do
class ForbidiumParametersTestObject; end
it 'includes Forbidium::Allow and Forbidium::Forbid when included' do
class ForbidiumTestObject; end

expect(
ForbidiumParametersTestObject.include?(Forbidium::ActionController::Parameters::Allow)
).not_to be true
expect(
ForbidiumParametersTestObject.include?(Forbidium::ActionController::Parameters::Forbid)
).not_to be true
expect(ForbidiumTestObject.include?(Forbidium::Allow)).not_to be true
expect(ForbidiumTestObject.include?(Forbidium::Forbid)).not_to be true

ForbidiumParametersTestObject.class_eval { include Forbidium::ActionController::Parameters }
ForbidiumTestObject.class_eval { include Forbidium }

expect(
ForbidiumParametersTestObject.include?(Forbidium::ActionController::Parameters::Allow)
).to be true
expect(
ForbidiumParametersTestObject.include?(Forbidium::ActionController::Parameters::Forbid)
).to be true
end
end

context 'Forbidium::Hash' do
it 'includes namespaced Allow and Forbid modules when included' do
class ForbidiumHashTestObject; end

expect(
ForbidiumHashTestObject.include?(Forbidium::Hash::Allow)
).not_to be true
expect(
ForbidiumHashTestObject.include?(Forbidium::Hash::Forbid)
).not_to be true

ForbidiumHashTestObject.class_eval { include Forbidium::Hash }

expect(
ForbidiumHashTestObject.include?(Forbidium::Hash::Allow)
).to be true
expect(
ForbidiumHashTestObject.include?(Forbidium::Hash::Forbid)
).to be true
end
expect(ForbidiumTestObject.include?(Forbidium::Allow)).to be true
expect(ForbidiumTestObject.include?(Forbidium::Forbid)).to be true
end

it 'is included in Hash when required' do
expect(Hash.include?(Forbidium::Hash)).to be true
expect(Hash.include?(Forbidium)).to be true
end
end
6 changes: 2 additions & 4 deletions spec/rails/action_controller/parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
ActiveSupport.run_load_hooks(:action_controller, ActionController::Base)

describe ActionController::Parameters do
it 'includes Forbidium::ActionController::Parameters' do
expect(
ActionController::Parameters.include?(Forbidium::ActionController::Parameters)
).to be true
it 'includes Forbidium' do
expect(ActionController::Parameters.include?(Forbidium)).to be true
end

let(:params) do
Expand Down

0 comments on commit eb544ca

Please sign in to comment.