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

Callback class #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Callback class #3

wants to merge 1 commit into from

Conversation

holderbaum
Copy link

This optional addon allows creation of a semi-static callback-class. It's kind of semantic sugar.

@splattael
Copy link
Contributor

Nice! Can you squash the commits please?

@splattael
Copy link
Contributor

Mh, I already have a spike on class implementation for on.
Can you look at the on-class-spike branch?
Especially on https://github.com/neopoly/on/blob/on-class-spike/lib/on/class.rb

My attempt is different, though.

WDYT?

Replicate on_proc test for On::Class.

Implement empty On::Class.

Fix test, use build instead of new.

Add build-pattern to On::Class.

Implement class wrapper for On.

Update On::Class' inline documentation.
@holderbaum
Copy link
Author

Squashed!

I dont think those to features are similar. Your class implementation is for the caller, so he can encapsulate the callback-block into a class.

My class implementation is for encapsulating the callback-triggering of the callee.

Don't you thing both features are neat? :)

@splattael
Copy link
Contributor

Well, it depends.

My solution encourages you to create "callback" classes a la:

class MyCallback
  include On::Class

  def on_success
    ...
  end
end

Your solution seems to be "just" a syntatic sugar on how to create on callbacks.
Of course you can do:

class MyCallback < On::Class::CallbackClass
  callbacks :success
end

But I don't see the point of doing the above. Can you explain?

Yes, these are different features. We have to name them distinctly :)

@holderbaum
Copy link
Author

class UserCreating
  Callback = On::Class.build(:success, :failure)
  Response = Struct.new(:user_id)

  def initialize(user_name)
    @user_name = user_name
  end

  def create(&block)
    back = Callback.new(&block)

    user = user_repo.create(:user_name => @user_name)

    if user
      back.call :success, Response.new(user.id)
    else
      back.call :failure
    end
  end
end

class Call
  include On::Class

  def on_success(response)
    p :id => response.user_id
  end

  def on_failure
    # :(
  end
end

UserCreating.new('peter').create(&(Call.new))

Despite the naming-clash, those are the two usecases combined. I thought about this Class-implementation, because it cleans up the usecase action-method.

If you want to, I can name On::Class to On::Callback, this would still make sense in a semantic way. :)

@splattael
Copy link
Contributor

Ok, got it. Good example!

Sadly, Callback is already used in On.

I'd like to see On::Callback.create(:success, :failure). WDYT?

class UserCreating
  Callback = On::Callback.create(:success, :failure)
  # or
  Callback = On.callback(:succes, :failure)
...

My approch in on-class-spike is more "listener" class style. Maybe it should be named that way...

@holderbaum
Copy link
Author

Agreed! A Class named Class is perhaps just a bad idea, independent to the usecase! :)

If I think it true, even Callback is semantically wrong. The internal class Callback uses this term correctly. The feature I proposed is more something like a Caller? Or even another term, more distinct from pre- or postfixes to CALL.

Perhaps the feature could be embedded into On without such terminology?

Callback = On.create(:success, :failure)
# or 
Callback = On.to_class(:success, :failure)

On::Listener could be a great constant for your Class-Feature, since this describes nearly perfect its purpose.

@splattael
Copy link
Contributor

Mh

Callback = On.create(:success, :failure)
# or 
Callback = On.to_class(:success, :failure)

reads kind of ugly, isn't :/

@holderbaum enable your naming-power. NOW!! :)

@holderbaum
Copy link
Author

NAMINGPOWER ENABLED 👊

Callback = On::Back.new(:success, :failure)
# this one is still my favourite:
Callback = On::Callback.new(:success, :failure)

Is there any Chance, we can rename the innerclass Callback?
@splattael enable you rename-power! =)

@shluboto
Copy link
Member

shluboto commented Jul 6, 2013

:)

Diese Nachricht wurde von meinem Android Mobiltelefon mit GMX Mail gesendet.

Jakob Holderbaum notifications@github.com schrieb:

NAMINGPOWER ENABLED

Callback = On::Back.new(:success, :failure) # this one is still my favourite: Callback = On::Callback.new(:success, :failure)

Is there any Chance, we can rename the innerclass Callback?
@splattael enable you rename-power! =)


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants