Skip to content

Commit

Permalink
make filter column objects flyweight
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Apr 7, 2014
1 parent 1d7920d commit ffcc994
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/active_interaction/base.rb
Expand Up @@ -166,7 +166,7 @@ def initialize(inputs = {})

def column_for_attribute(name)
filter = self.class.filters[name]
FilterColumn.new(filter.database_column_type) if filter
FilterColumn.intern(filter.database_column_type) if filter
end

# @!method compose(other, inputs = {})
Expand Down
14 changes: 14 additions & 0 deletions lib/active_interaction/filter_column.rb
Expand Up @@ -5,6 +5,20 @@ module ActiveInteraction
class FilterColumn
attr_reader :limit, :type

class << self
def intern(type)
@columns ||= {}

if @columns[type]
@columns.fetch(type)
else
@columns[type] = new(type)
end
end

private :new
end

def initialize(type)
@type = type
end
Expand Down
18 changes: 17 additions & 1 deletion spec/active_interaction/filter_column_spec.rb
Expand Up @@ -4,7 +4,23 @@

describe ActiveInteraction::FilterColumn do
let(:type) { :float }
subject(:column) { described_class.new(type) }
subject(:column) { described_class.intern(type) }

describe '.intern(type)' do
it 'returns the same object for each type' do
expect(described_class.intern(type)).to equal column
end

it 'returns different objects for different types' do
expect(described_class.intern(:integer)).to_not equal column
end
end

describe '.new(type)' do
it 'is private' do
expect { described_class.new(type) }.to raise_error NoMethodError
end
end

describe '#limit' do
it 'returns nil' do
Expand Down

3 comments on commit ffcc994

@tfausak
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should consider using a different method name: rubocop/rubocop#2260

@AaronLasseigne
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like the problem is with RuboCop.

@tfausak
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, they disabled it by default: rubocop/rubocop@44af764.

Please sign in to comment.