Class: DuckPuncher::Duck
- Inherits:
-
Object
- Object
- DuckPuncher::Duck
- Defined in:
- lib/duck_puncher/duck.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#mod ⇒ Object
Returns the value of attribute mod.
-
#options ⇒ Object
Returns the value of attribute options.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
-
#call(opts = {}) ⇒ Class
(also: #punch)
The class that was just punched.
-
#initialize(target, mod, options = {}) ⇒ Duck
constructor
A new instance of Duck.
Constructor Details
#initialize(target, mod, options = {}) ⇒ Duck
Returns a new instance of Duck.
12 13 14 15 16 |
# File 'lib/duck_puncher/duck.rb', line 12 def initialize(target, mod, = {}) @options = @target = DuckPuncher.lookup_constant(target) @mod = DuckPuncher.lookup_constant(mod) end |
Instance Attribute Details
#mod ⇒ Object
Returns the value of attribute mod.
3 4 5 |
# File 'lib/duck_puncher/duck.rb', line 3 def mod @mod end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/duck_puncher/duck.rb', line 3 def @options end |
#target ⇒ Object
Returns the value of attribute target.
3 4 5 |
# File 'lib/duck_puncher/duck.rb', line 3 def target @target end |
Instance Method Details
#call(opts = {}) ⇒ Class Also known as: punch
Returns The class that was just punched.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/duck_puncher/duck.rb', line 23 def call(opts = {}) opts = .merge(opts) targets = Array(opts[:target] || self.target) targets.each do |target| opts[:before].call(target) if opts[:before] punches = Array(opts[:only] || Ducks::Module.instance_method(:local_methods).bind(mod).call) unless target.is_a?(::Module) fail ArgumentError, "Invalid target #{target}. Please pass a module as :target" end DuckPuncher.logger.info %Q(#{target}#{" <-- #{mod.name}#{punches}" if punches.any?}) extender = Usable::ModExtender.new(mod, only: opts.delete(:only), method: opts.delete(:method)) extender.call target opts[:after].call(target) if opts[:after] end targets end |