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

Any plans to support module arguments as mentioned in the README? #1

Closed
ksouthworth opened this issue Mar 26, 2020 · 2 comments
Closed

Comments

@ksouthworth
Copy link

Just wondering if you have any plans to add support for module arguments, as mentioned in the "To-Do" section of the readme?
https://github.com/henrik/augmentations#to-do

@henrik
Copy link
Owner

henrik commented Mar 29, 2020

Hi! I haven't really touched or used this code in a decade (!) but just for fun, I tried it. If you only need to use the module arguments on the class level (not inside instance methods), you can do something like this:

class ::Object
  def self.augment(*mods, **args)
    mods.each { |mod| class_exec(**args, &mod.augmentation) }
  end
end

class ::Module
  def augmentation(&block)
    @augmentation ||= block
  end
end


module Person
  augmentation do |age:|
    if age
      def age
        42
      end
    end
  end
end

class AgefulCustomer
  augment Person, age: true
end

class AgelessCustomer
  augment Person, age: false
end

p AgefulCustomer.new.age
p AgelessCustomer.new.age  # Boom! Undefined method!

@henrik henrik closed this as completed Mar 29, 2020
@ksouthworth
Copy link
Author

ksouthworth commented Mar 29, 2020 via email

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