Class: Humanized::Wrapper
- Defined in:
- lib/humanized/wrapper.rb
Direct Known Subclasses
Constant Summary collapse
- UNWRAPPED =
[:object_id, :__send__ , :__id__ ,:method_missing , :respond_to?, :respond_to_missing? , :class, :instance_of?, :instance_eval, :instance_exec].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #__getobj__ ⇒ Object
- #__setobj__(obj) ⇒ Object
-
#initialize(object, __to_s__ = nil, &block) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #to_s ⇒ Object
Constructor Details
#initialize(object, __to_s__ = nil, &block) ⇒ Wrapper
Returns a new instance of Wrapper.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/humanized/wrapper.rb', line 51 def initialize(object, __to_s__ = nil, &block) self.__setobj__(object) if block_given? @block = block elsif __to_s__.kind_of? String @block = eval("lambda{ %{#{__to_s__}} }") elsif __to_s__.kind_of? Array @block = lambda{ __to_s__[0].to_s + to_s + __to_s__[1].to_s } else raise ArgumentError, "Wrapper.initialize expects a String or a block" end end |
Class Method Details
.wrap(*args, &block) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/humanized/wrapper.rb', line 44 def self.wrap(*args, &block) a = args.flatten.map{|o| self.new(o,&block) } return a.size == 1 ? a[0] : a end |
Instance Method Details
#__getobj__ ⇒ Object
36 37 38 |
# File 'lib/humanized/wrapper.rb', line 36 def __getobj__ @object end |
#__setobj__(obj) ⇒ Object
40 41 42 |
# File 'lib/humanized/wrapper.rb', line 40 def __setobj__(obj) @object = obj end |
#to_s ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/humanized/wrapper.rb', line 64 def to_s if @block.arity == 1 return @block.call(@object) else return @object.instance_exec(&@block) end end |