Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/more/facets/snapshot.rb,
lib/core/facets/boolean.rb,
lib/more/facets/typecast.rb,
lib/more/facets/inheritor.rb,
lib/more/facets/nullclass.rb,
lib/more/facets/basicobject.rb,
lib/core/facets/kernel/withattr.rb
Overview
See Kernel callback.
Instance Method Summary collapse
-
#cast_from(object) ⇒ Object
Cast on object from another.
-
#cast_to(klass) ⇒ Object
Cast an object to another.
- #from_data(snap) ⇒ Object
-
#inheritor(key, obj, op = nil) ⇒ Object
Create an inheritor "class attribute".
- #null? ⇒ Boolean
- #restore_snapshot(snap) ⇒ Object
- #take_snapshot ⇒ Object
- #to_bool ⇒ Object
- #to_data ⇒ Object
-
#with_accessor(h) ⇒ Object
Takes a hash and creates (singleton) attr_accessors for each key.
-
#with_reader(h) ⇒ Object
Takes a hash and creates (singleton) attr_readers for each key.
-
#with_writer(h) ⇒ Object
Takes a hash and creates (singleton) attr_writers for each key.
Instance Method Details
#cast_from(object) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/more/facets/typecast.rb', line 125 def cast_from(object) method_to = "to_#{self.name.methodize}".to_sym if object.respond_to? method_to retval = object.send(method_to) return retval end method_from = "from_#{object.class.name.methodize}".to_sym if respond_to? method_from retval = send(method_from, object) return retval end raise TypeCastException, "TypeCasting from #{object.class.name} to #{self.name} not supported" end |
#cast_to(klass) ⇒ Object
Cast an object to another
1234.cast_to(String) => "1234"
117 118 119 |
# File 'lib/more/facets/typecast.rb', line 117 def cast_to(klass) klass.cast_from(self) end |
#from_data(snap) ⇒ Object
152 153 154 |
# File 'lib/more/facets/snapshot.rb', line 152 def from_data(snap) restore_snapshot(snap) end |
#inheritor(key, obj, op = nil) ⇒ Object
Create an inheritor "class attribute".
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/more/facets/inheritor.rb', line 62 def inheritor( key, obj, op=nil ) # inhertiance operator op = op ? op.to_sym : :add # inheritor store a this level instance_variable_set("@#{key}", obj) #base = self deflambda = lambda do define_method( key ) do defined?(super) ? super.__send__(op,obj) : obj.dup end define_method( "#{key}!" ) do instance_variable_get("@#{key}") || inheritor( key, obj.class.new, op ) #if instance_variables.include?("@#{key}") # instance_variable_get("@#{key}") #else # if self != base # inheritor( key, obj.class.new, op ) # end #end end end # TODO This is an issue if you try to include a module # into Module or Class itself. How to fix? # if the object is a module (not a class or other object) if self == Class or self == Module class_eval &deflambda elsif is_a?(Class) (class << self; self; end).class_eval &deflambda elsif is_a?(Module) #class_inherit &deflambda extend class_extension( &deflambda ) else # other Object (class << self; self; end).class_eval &deflambda end obj end |
#null? ⇒ Boolean
62 63 64 |
# File 'lib/more/facets/nullclass.rb', line 62 def null? false end |
#restore_snapshot(snap) ⇒ Object
146 147 148 149 150 |
# File 'lib/more/facets/snapshot.rb', line 146 def restore_snapshot(snap) instance_variables.each do |iv| instance_variable_set(iv, snap[iv]) end end |
#take_snapshot ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/more/facets/snapshot.rb', line 134 def take_snapshot snap = Hash.new instance_variables.each do |iv| snap[iv] = instance_variable_get(iv) end snap end |
#to_bool ⇒ Object
140 141 142 |
# File 'lib/core/facets/boolean.rb', line 140 def to_bool return true end |
#to_data ⇒ Object
142 143 144 |
# File 'lib/more/facets/snapshot.rb', line 142 def to_data task_snapshot end |
#with_accessor(h) ⇒ Object
Takes a hash and creates (singleton) attr_accessors for each key.
with_accessor { :x => 1, :y => 2 }
@x #=> 1
@y #=> 2
self.x = 3
self.y = 4
self.x #=> 3
self.y #=> 4
CREDIT: Trans
48 49 50 51 |
# File 'lib/core/facets/kernel/withattr.rb', line 48 def with_accessor(h) (class << self ; self ; end).send( :attr_accessor, *h.keys ) h.each { |k,v| instance_variable_set("@#{k}", v) } end |
#with_reader(h) ⇒ Object
Takes a hash and creates (singleton) attr_readers for each key.
with_reader { :x => 1, :y => 2 }
@x #=> 1
@y #=> 2
self.x #=> 1
self.y #=> 2
CREDIT: Trans
13 14 15 16 |
# File 'lib/core/facets/kernel/withattr.rb', line 13 def with_reader(h) (class << self ; self ; end).send( :attr_reader, *h.keys ) h.each { |k,v| instance_variable_set("@#{k}", v) } end |
#with_writer(h) ⇒ Object
Takes a hash and creates (singleton) attr_writers for each key.
with_writer { :x => 1, :y => 2 }
@x #=> 1
@y #=> 2
self.x = 3
self.y = 4
@x #=> 3
@y #=> 4
CREDIT: Trans
30 31 32 33 |
# File 'lib/core/facets/kernel/withattr.rb', line 30 def with_writer(h) (class << self ; self ; end).send( :attr_writer, *h.keys ) h.each { |k,v| instance_variable_set("@#{k}", v) } end |