Module: Usable
- Defined in:
- lib/usable.rb,
lib/usable/config.rb,
lib/usable/version.rb,
lib/usable/mod_extender.rb
Defined Under Namespace
Classes: Config, ModExtender
Constant Summary collapse
- VERSION =
"2.1.2".freeze
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#usable(mod, options = {}, &block) ⇒ ModExtender
Containing the original and modified module.
-
#usable_method(context, method_name) ⇒ Method
Bound to the given -context-.
Instance Attribute Details
#usables ⇒ Object
30 31 32 33 34 |
# File 'lib/usable.rb', line 30 def usables @usables ||= Config.new return @usables unless block_given? @usables.instance_eval &Proc.new end |
Class Method Details
.extended(base) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/usable.rb', line 10 def self.extended(base) base.class_eval do def usables self.class.usables end def usable_method(method_name) self.class.usable_method(self, method_name) end end unless base.is_a? Class base.instance_eval do def config(&block) usables(&block) end unless defined? config end end end |
Instance Method Details
#usable(mod, options = {}, &block) ⇒ ModExtender
Note:
Hides methods
Returns containing the original and modified module.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/usable.rb', line 55 def usable(mod, = {}, &block) = { only: .delete(:only), method: .delete(:method) } # Define settings on @usables and on the scoped @usables scope = Config.new if mod.name scope_name = mod.name.split('::').last.gsub(/\B([A-Z])([a-z_0-9])/, '_\1\2').downcase usables[scope_name] = scope end if mod.respond_to? :usables mod.usables.each do |k, v| [scope, usables].each { |x| x.spec k, v } end end [scope, usables].each { |x| .each { |k, v| x[k] = v } } [scope, usables].each { |x| x.instance_eval &block } if block_given? ModExtender.new(mod, ).call self send :include, mod.const_get(:InstanceMethods) if mod.const_defined?(:InstanceMethods) send :extend, mod.const_get(:ClassMethods) if mod.const_defined?(:ClassMethods) self end |
#usable_method(context, method_name) ⇒ Method
Returns bound to the given -context-.
77 78 79 |
# File 'lib/usable.rb', line 77 def usable_method(context, method_name) usables.available_methods[method_name].bind(context) end |