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,
 lib/ov/ext/matching.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"
Work with blocks
Blocks, should be pass given without ampersand (&)
class MyClass
  let :my_method, Fixnum do |num, block| # instead of |num, &block|
    p num
    block.call
  end
end
MyClass.new.my_method(1) do 
  p "123"
end
# => 1
# => 123
Defined Under Namespace
Modules: Ext Classes: Any, NotImplementError, OA, OverrideMethod
Constant Summary collapse
- VERSION =
- "0.1.0"
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:
| 98 99 100 101 102 103 | # File 'lib/ov.rb', line 98 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
| 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | # File 'lib/ov.rb', line 118 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 k = *args k << block if !block.nil? instance_exec(*k, &z.body) end end end end end | 
#multimethods ⇒ Object
return all defined with let methods.
| 163 164 165 166 167 168 | # File 'lib/ov.rb', line 163 def multimethods() owner = need_owner() owner.send(:__overload_methods).map do |method| [method.name, method.types] if method.owner == owner end.compact end |