Methods
Public Instance methods
Cast on object from another
String.cast_from(1234) => "1234"
[ show source ]
# File lib/facets/more/typecast.rb, line 121 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 an object to another
1234.cast_to(String) => "1234"
[ show source ]
# File lib/facets/more/typecast.rb, line 113 def cast_to(klass) klass.cast_from(self) end
Create an inheritor "class attribute".
[ show source ]
# File lib/facets/more/inheritor.rb, line 55 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
[ show source ]
# File lib/facets/more/nullclass.rb, line 52 def null? false end
[ show source ]
# File lib/facets/more/snapshot.rb, line 131 def restore_snapshot(snap) instance_variables.each do |iv| instance_variable_set(iv, snap[iv]) end end
[ show source ]
# File lib/facets/more/taskable.rb, line 247 def send_task( name ) (class << self; self; end).instance_task( name ) end
[ show source ]
# File lib/facets/more/snapshot.rb, line 123 def take_snapshot snap = Hash.new instance_variables.each do |iv| snap[iv] = instance_variable_get(iv) end snap end
Converts this object to a string (calling to_s), converts it to a JSON string, and returns the result. This is a fallback, if no special method to_json was defined for some object. state is a JSON::State object, that can also be used to configure the produced JSON string output further.
[ show source ]
# File lib/facets/more/json.rb, line 490 def to_json(*) to_s.to_json end