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

decorating methods with default variable values #20

Open
JDutil opened this issue Nov 13, 2013 · 7 comments
Open

decorating methods with default variable values #20

JDutil opened this issue Nov 13, 2013 · 7 comments

Comments

@JDutil
Copy link
Contributor

JDutil commented Nov 13, 2013

What happens when you want to override methods with default values?

For instance:

    # Get current line item for variant if exists
    # Add variant qty to line_item
    def add(variant, quantity = 1, currency = nil, shipment = nil)
      line_item = order.find_line_item_by_variant(variant)
      add_to_line_item(line_item, variant, quantity, currency, shipment)
    end

Guess it must be done like so:

  # Get current line item for variant if exists
  # Add variant qty to line_item
  durably_decorate :add, mode: 'soft', sha: '' do |variant, quantity, currency, shipment|
    quantity ||= 1
    currency ||= nil
    shipment ||= nil
    # If variant is a gift card we say order doesn't already contain it so that each gift card is it's own line item.
    if variant.product.is_gift_card?
      line_item = nil
    else
      line_item = order.find_line_item_by_variant(variant)
    end
    add_to_line_item(line_item, variant, quantity, currency, shipment)
  end

Unless the decorated add remembers the variables default values which I'm guessing it doesn't. Is it even possible?

Or is this going to cause add to always require those variables to be passed in?

@JDutil
Copy link
Contributor Author

JDutil commented Nov 13, 2013

Actually doesn't appear to even work for me. I get:

/usr/local/opt/rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/durable_decorator-0.2.1/lib/durable_decorator/validator.rb:39:in `validate_method_arity': Attempting to override Spree::OrderContents's add with incorrect arity. (DurableDecorator::BadArityError)
    from /usr/local/opt/rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/durable_decorator-0.2.1/lib/durable_decorator/base.rb:35:in `existing_method'
    from /usr/local/opt/rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/durable_decorator-0.2.1/lib/durable_decorator/base.rb:21:in `redefine_instance'
    from /usr/local/opt/rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/durable_decorator-0.2.1/lib/durable_decorator/base.rb:16:in `redefine'
    from /usr/local/opt/rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/durable_decorator-0.2.1/lib/durable_decorator.rb:27:in `durably_decorate'
    from /Users/JDutil/Code/spree_gift_card/app/models/spree/order_contents_decorator.rb:5:in `block in <top (required)>'

@jumph4x
Copy link
Owner

jumph4x commented Nov 13, 2013

Oh man, good call. Let me really think about this one.

@jumph4x
Copy link
Owner

jumph4x commented Feb 14, 2014

This is a hard one, dude. Literally no ideas.

@JDutil
Copy link
Contributor Author

JDutil commented Feb 15, 2014

Darn I don't have any either. Hopefully someone at SpreeConf can think of something since I'm guessing this is the "problems" part of your lightning talk.

@jumph4x
Copy link
Owner

jumph4x commented Feb 15, 2014

Yup :(

@jumph4x
Copy link
Owner

jumph4x commented Mar 4, 2014

Actually, this entirely possible.

method_source does, in fact, return the entire method definition that includes the signature. Just need to write a method signature parser. Pretty involved feature. Deferring for now.

@JDutil
Copy link
Contributor Author

JDutil commented Mar 4, 2014

Cool in the meantime this workaround is sufficient https://github.com/jumph4x/durable_decorator#problems do you have any docs you can point me to in case I have time to get to working on this before you?

Also if you could comment on spree/spree#4374 I'd like to close that out if there are no config options you know of that we should add by default.

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

No branches or pull requests

2 participants