-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from orgsync/interface-filter
Add interface filter
- Loading branch information
Showing
7 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# [Master][] | ||
|
||
- Add an interface filter. | ||
- Add missing translation for symbol filters. | ||
|
||
# [1.2.1][] (2014-05-02) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# coding: utf-8 | ||
|
||
module ActiveInteraction | ||
class Base | ||
# @!method self.interface(*attributes, options = {}) | ||
# Creates accessors for the attributes and ensures that values passed to | ||
# the attributes implement an interface. | ||
# | ||
# @!macro filter_method_params | ||
# @option options [Array<Symbol>] :methods ([]) the methods that objects | ||
# conforming to this interface should respond to | ||
# | ||
# @example | ||
# interface :anything | ||
# @example | ||
# interface :serializer, | ||
# methods: [:dump, :load] | ||
end | ||
|
||
# @private | ||
class InterfaceFilter < Filter | ||
def cast(value) | ||
matches?(value) ? value : super | ||
end | ||
|
||
private | ||
|
||
# @param object [Object] | ||
# | ||
# @return [Boolean] | ||
def matches?(object) | ||
methods.all? { |method| object.respond_to?(method) } | ||
rescue NoMethodError | ||
false | ||
end | ||
|
||
# @return [Array<Symbol>] | ||
def methods | ||
options.fetch(:methods, []) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# coding: utf-8 | ||
|
||
require 'spec_helper' | ||
require 'json' | ||
require 'yaml' | ||
|
||
describe ActiveInteraction::InterfaceFilter, :filter do | ||
include_context 'filters' | ||
it_behaves_like 'a filter' | ||
|
||
before { options[:methods] = [:dump, :load] } | ||
|
||
describe '#cast' do | ||
let(:result) { filter.cast(value) } | ||
|
||
context 'with an Object' do | ||
let(:value) { Object.new } | ||
|
||
it 'raises an error' do | ||
expect { result }.to raise_error ActiveInteraction::InvalidValueError | ||
end | ||
end | ||
|
||
context 'with JSON' do | ||
let(:value) { JSON } | ||
|
||
it 'returns an Array' do | ||
expect(result).to eql value | ||
end | ||
end | ||
|
||
context 'with YAML' do | ||
let(:value) { YAML } | ||
|
||
it 'returns an Hash' do | ||
expect(result).to eql value | ||
end | ||
end | ||
end | ||
|
||
describe '#database_column_type' do | ||
it 'returns :string' do | ||
expect(filter.database_column_type).to eql :string | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
spec/active_interaction/integration/interface_interaction_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# coding: utf-8 | ||
|
||
require 'spec_helper' | ||
require 'json' | ||
require 'yaml' | ||
|
||
describe 'InterfaceInteraction' do | ||
it_behaves_like 'an interaction', | ||
:interface, | ||
-> { [JSON, YAML].sample }, | ||
methods: [:dump, :load] | ||
end |