Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interface filter #182

Merged
merged 6 commits into from
May 6, 2014
Merged

Add interface filter #182

merged 6 commits into from
May 6, 2014

Conversation

tfausak
Copy link
Collaborator

@tfausak tfausak commented May 2, 2014

(This is like #181 but into v1.3.0 instead of master.) Fixes #178.

This allows you to define an interface that an input must conform to. In most cases, you don't actually want to do this. Instead you should use a model filter. For instance, if you wanted your input to respond to #each, you could do this:

class EachInteraction < ActiveInteraction::Base
  interface :thing,
    methods: [:each]
end

But what you probably want is an object that implements Enumerable. So you should do this:

class EnumerableInteraction < ActiveInteraction::Base
  model :thing,
    class: Enumerable
end

Why have an interface filter, then? It may be that the classes you're willing to accept don't have anything in common and there's nothing you can do about it. For instance, you could have a serializer input that can take either JSON or YAML. You couldn't do that with a model filter. (Well, not without class: Object.) With an interface filter, you could do it.

class SerializerInteraction < ActiveInteraction::Base
  interface :serializer,
    methods: [:dump, :load]
  def execute
    h = serializer.load('{}')
    h['now'] = Time.now
    serializer.dump(h)
  end
end

require 'json'
SerializerInteraction.run!(serializer: JSON)
# => "{\"now\":\"2014-05-02 21:57:41 +0000\"}"

require 'yaml'
SerializerInteraction.run!(serializer: YAML)
# => "---\nnow: 2014-05-02 21:57:52.900349283 +00:00\n"

@tfausak tfausak added this to the v1.3.0 milestone May 2, 2014
@tfausak tfausak self-assigned this May 2, 2014
@tfausak tfausak mentioned this pull request May 2, 2014
@AaronLasseigne
Copy link
Owner

Don't forget the translation.

@tfausak
Copy link
Collaborator Author

tfausak commented May 3, 2014

Good catch! I updated the i18n tests (6816075) so things like that can't happen anymore. In fact, we were also missing a translation for the symbol filter (650e8b5). That's kind of embarrassing; it's been around since 0.6.0.

@AaronLasseigne
Copy link
Owner

:shipit:

tfausak added a commit that referenced this pull request May 6, 2014
@tfausak tfausak merged commit bfc7a49 into v1.3.0 May 6, 2014
@tfausak tfausak deleted the interface-filter branch May 6, 2014 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants