Module: Ov
- Defined in:
- lib/ov.rb,
lib/ov/ov_any.rb,
lib/ov/version.rb,
lib/ov/ov_array.rb,
lib/ov/ov_method.rb
Overview
‘Ov` module provides functional for creating methods with types in Ruby.
Usage
Firstly include ‘Ov` in you class
class MyClass
include Ov
end
After define method with types:
class MyClass
include Ov
# with Fixnum type
let :cool_method, Fixnum do |num|
num * 100
end
# with String type
let :cool_method, String do |str|
str << "!"
end
end
And now
my_class = MyClass.new
my_class.cool_method(3) # => 300
my_class.cool_method("foo") # => foo!
Any Type
class MyClass
include Ov
let :cool_method, Any do |any|
any
end
end
my_class = MyClass.new
my_class.cool_method(1)
=> 1
my_class.cool_method("foo") # => "foo"
Is the same so ruby ‘def`
Define as class methods
class MyClass
self << class
let :cool_method, Fixnum do |f|
f + 1
end
let :cool_method, String do |s|
"{s}"
end
end
end
MyClass.cool_method(1) #=> 2
MyClass.cool_method("test") #=> "test"
Defined Under Namespace
Classes: Any, NotImplementError, OA, OverrideMethod
Constant Summary collapse
- VERSION =
"0.0.4"
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#let(name, *types, &block) ⇒ Object
Create new method with
nameandtypesWhen method calledblockwill be executed. -
#multimethods ⇒ Object
return all defined with
letmethods.
Class Method Details
.included(base) ⇒ Object
:nodoc:
79 80 81 82 83 84 |
# File 'lib/ov.rb', line 79 def self.included(base) # :nodoc: base.extend(self) base.class_eval do class_variable_set(:@@__overload_methods, OA.new) if !class_variable_defined?(:@@__overload_methods) end end |
Instance Method Details
#let(name, *types, &block) ⇒ Object
Create new method with name and types When method called block will be executed
name is symbol
types types for method
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ov.rb', line 99 def let(name, *types, &block) included = false if self.instance_methods.include?(name) included = true class_eval "alias :ov_old_#{name.to_s} #{name}" undef_method name end __overload_methods << OverrideMethod.new(name, types, self, block) if !self.method_defined?(name) self.instance_exec do = if self.class == Module :define_singleton_method else :define_method end self.send(, name) do |*args, &block| types = *args.map(&:class) owner = need_owner() method = OverrideMethod.new(name, types, owner) z = owner.send(:__overload_methods).where(method) if z.nil? if included send("ov_old_#{name.to_s}", *args, &block) else raise Ov::NotImplementError.new("Method `#{name}` in `#{self}` class with types `#{types.join(', ')}` not implemented.") end else instance_exec(*args, &z.body) end end end end end |
#multimethods ⇒ Object
return all defined with let methods.
142 143 144 145 146 147 |
# File 'lib/ov.rb', line 142 def multimethods() owner = need_owner() owner.send(:__overload_methods).map do |method| [method.name, method.types] if method.owner == owner end.compact end |