Skip to content

Commit

Permalink
make cast truly private and remove AbstractFilter class
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Jan 10, 2021
1 parent 5a5337c commit c524621
Show file tree
Hide file tree
Showing 24 changed files with 58 additions and 90 deletions.
1 change: 0 additions & 1 deletion lib/active_interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module ActiveInteraction

require 'active_interaction/filter_column'
require 'active_interaction/filter'
require 'active_interaction/filters/abstract_filter'
require 'active_interaction/filters/interface_filter'
require 'active_interaction/filters/abstract_date_time_filter'
require 'active_interaction/filters/abstract_numeric_filter'
Expand Down
59 changes: 25 additions & 34 deletions lib/active_interaction/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ def initialize(name, options = {}, &block)
#
# @return [Object]
#
# @raise (see #cast)
# @raise [MissingValueError] If the value is missing and there is no
# default.
# @raise [InvalidValueError] If the value is invalid.
# @raise (see #default)
def clean(value, context)
value = cast(value, context)
Expand All @@ -119,7 +121,7 @@ def clean(value, context)
#
# @param context [Base, nil]
#
# @return (see #raw_default)
# @return [Object]
#
# @raise [NoDefaultError] If the default is missing.
# @raise [InvalidDefaultError] If the default is invalid.
Expand Down Expand Up @@ -161,16 +163,23 @@ def default?
options.key?(:default)
end

# @param value [Object]
# @param context [Base, nil]
#
# @return [Object]
# Gets the type of database column that would represent the filter data.
#
# @raise [MissingValueError] If the value is missing and there is no
# default.
# @raise [InvalidValueError] If the value is invalid.
# @example
# ActiveInteraction::TimeFilter.new(Time.now).database_column_type
# # => :datetime
# @example
# ActiveInteraction::Filter.new(:example).database_column_type
# # => :string
#
# @private
# @return [Symbol] A database column type. If no sensible mapping exists,
# returns `:string`.
def database_column_type
:string
end

private

# rubocop:disable Metrics/MethodLength
def cast(value, context, convert: true, reconstantize: true)
if matches?(value)
Expand All @@ -181,12 +190,12 @@ def cast(value, context, convert: true, reconstantize: true)

nil
elsif reconstantize
public_send(__method__, value, context,
send(__method__, value, context,
convert: convert,
reconstantize: false
)
elsif convert
public_send(__method__, convert(value), context,
send(__method__, convert(value), context,
convert: false,
reconstantize: reconstantize
)
Expand All @@ -196,23 +205,6 @@ def cast(value, context, convert: true, reconstantize: true)
end
# rubocop:enable Metrics/MethodLength

# Gets the type of database column that would represent the filter data.
#
# @example
# ActiveInteraction::TimeFilter.new(Time.now).database_column_type
# # => :datetime
# @example
# ActiveInteraction::Filter.new(:example).database_column_type
# # => :string
#
# @return [Symbol] A database column type. If no sensible mapping exists,
# returns `:string`.
def database_column_type
:string
end

private

def matches?(_value)
false
end
Expand All @@ -225,17 +217,16 @@ def convert(value)
value
end

# @param value [Object]
# @return [String]
def klass
@klass ||= Object.const_get(self.class.slug.to_s.camelize, false)
end

def describe(value)
value.inspect
rescue NoMethodError
"(Object doesn't support #inspect)"
end

# @param context [Base, nil]
#
# @return [Object]
def raw_default(context)
value = options.fetch(:default)
return value unless value.is_a?(Proc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module ActiveInteraction
# objects.
#
# @private
class AbstractDateTimeFilter < AbstractFilter
class AbstractDateTimeFilter < Filter
def database_column_type
self.class.slug
end
Expand Down
18 changes: 0 additions & 18 deletions lib/active_interaction/filters/abstract_filter.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/active_interaction/filters/abstract_numeric_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module ActiveInteraction
# Common logic for filters that handle numeric objects.
#
# @private
class AbstractNumericFilter < AbstractFilter
class AbstractNumericFilter < Filter
def database_column_type
self.class.slug
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:value) { nil }

it 'raises an error' do
expect { filter.cast(value, nil) }.to raise_error NameError
expect { filter.send(:cast, value, nil) }.to raise_error NameError
end
end
end
6 changes: 0 additions & 6 deletions spec/active_interaction/filters/abstract_filter_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:value) { nil }

it 'raises an error' do
expect { filter.cast(value, nil) }.to raise_error NameError
expect { filter.send(:cast, value, nil) }.to raise_error NameError
end
end
end
2 changes: 1 addition & 1 deletion spec/active_interaction/filters/array_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
end

describe '#cast' do
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with an Array' do
let(:value) { [] }
Expand Down
12 changes: 6 additions & 6 deletions spec/active_interaction/filters/boolean_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
context 'falsey' do
[false, '0', 'false', 'FALSE', 'off', 'OFF'].each do |value|
it "returns false for #{value.inspect}" do
expect(filter.cast(value, nil)).to be_falsey
expect(filter.send(:cast, value, nil)).to be_falsey
end
end

Expand All @@ -22,15 +22,15 @@ def to_str
end

it 'returns false' do
expect(filter.cast(value, nil)).to be_falsey
expect(filter.send(:cast, value, nil)).to be_falsey
end
end
end

context 'truthy' do
[true, '1', 'true', 'TRUE', 'on', 'ON'].each do |value|
it "returns true for #{value.inspect}" do
expect(filter.cast(value, nil)).to be_truthy
expect(filter.send(:cast, value, nil)).to be_truthy
end
end

Expand All @@ -44,7 +44,7 @@ def to_str
end

it 'returns true' do
expect(filter.cast(value, nil)).to be_truthy
expect(filter.send(:cast, value, nil)).to be_truthy
end
end
end
Expand All @@ -62,7 +62,7 @@ def to_str
include_context 'optional'

it 'returns the default' do
expect(filter.cast(value, nil)).to eql options[:default]
expect(filter.send(:cast, value, nil)).to eql options[:default]
end
end

Expand All @@ -71,7 +71,7 @@ def to_str

it 'raises an error' do
expect do
filter.cast(value, nil)
filter.send(:cast, value, nil)
end.to raise_error ActiveInteraction::MissingValueError
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/active_interaction/filters/date_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

describe '#cast' do
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with a Date' do
let(:value) { Date.new }
Expand Down
2 changes: 1 addition & 1 deletion spec/active_interaction/filters/date_time_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

describe '#cast' do
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with a Datetime' do
let(:value) { DateTime.new }
Expand Down
2 changes: 1 addition & 1 deletion spec/active_interaction/filters/decimal_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

describe '#cast' do
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with a Float' do
let(:value) { rand }
Expand Down
2 changes: 1 addition & 1 deletion spec/active_interaction/filters/file_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
it_behaves_like 'a filter'

describe '#cast' do
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with a File' do
let(:value) { File.new(__FILE__) }
Expand Down
2 changes: 1 addition & 1 deletion spec/active_interaction/filters/float_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
it_behaves_like 'a filter'

describe '#cast' do
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with a Float' do
let(:value) { rand }
Expand Down
2 changes: 1 addition & 1 deletion spec/active_interaction/filters/hash_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

describe '#cast' do
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with a Hash' do
let(:value) { {} }
Expand Down
8 changes: 5 additions & 3 deletions spec/active_interaction/filters/integer_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
it_behaves_like 'a filter'

describe '#cast' do
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with an Integer' do
let(:value) { rand(1 << 16) }
Expand Down Expand Up @@ -85,9 +85,11 @@ def to_str
end

it 'supports different bases' do
expect(described_class.new(name, base: 8).cast('071', nil)).to eql 57
expect(
described_class.new(name, base: 8).send(:cast, '071', nil)
).to eql 57
expect do
described_class.new(name, base: 8).cast('081', nil)
described_class.new(name, base: 8).send(:cast, '081', nil)
end.to raise_error ActiveInteraction::InvalidValueError
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/active_interaction/filters/interface_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class InterfaceClass; end
end

describe '#cast' do
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with an implicit constant name' do
context 'passed an instance' do
Expand Down
6 changes: 3 additions & 3 deletions spec/active_interaction/filters/object_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ObjectThings; end

describe '#cast' do
let(:value) { ObjectThing.new }
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with an instance of the class' do
it 'returns the instance' do
Expand All @@ -45,7 +45,7 @@ class ObjectThings; end
ObjectThing = BackupObjectThing
value = ObjectThing.new

expect(filter.cast(value, nil)).to eql value
expect(filter.send(:cast, value, nil)).to eql value
end

it 'handles reconstantizing subclasses' do
Expand All @@ -56,7 +56,7 @@ class ObjectThings; end
class SubObjectThing < ObjectThing; end
value = SubObjectThing.new

expect(filter.cast(value, nil)).to eql value
expect(filter.send(:cast, value, nil)).to eql value
end

context 'without the class available' do
Expand Down
6 changes: 3 additions & 3 deletions spec/active_interaction/filters/record_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RecordThings; end
end

let(:value) { RecordThing.new }
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with an instance of the class' do
it 'returns the instance' do
Expand All @@ -61,7 +61,7 @@ class RecordThings; end
RecordThing = BackupRecordThing
value = RecordThing.new

expect(filter.cast(value, nil)).to eql value
expect(filter.send(:cast, value, nil)).to eql value
end

it 'handles reconstantizing subclasses' do
Expand All @@ -72,7 +72,7 @@ class RecordThings; end
class SubRecordThing < RecordThing; end
value = SubRecordThing.new

expect(filter.cast(value, nil)).to eql value
expect(filter.send(:cast, value, nil)).to eql value
end

context 'without the class available' do
Expand Down
2 changes: 1 addition & 1 deletion spec/active_interaction/filters/string_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
end

describe '#cast' do
let(:result) { filter.cast(value, nil) }
let(:result) { filter.send(:cast, value, nil) }

context 'with a String' do
let(:value) { SecureRandom.hex }
Expand Down
Loading

0 comments on commit c524621

Please sign in to comment.