Class: Object
- Inherits:
- BasicObject
- Includes:
- Ramaze::ThreadAccessor
- Defined in:
- lib/ramaze/snippets/metaid.rb,
lib/vendor/bacon.rb,
lib/vendor/bacon.rb,
lib/ramaze/snippets/object/scope.rb,
lib/ramaze/snippets/object/pretty.rb,
lib/ramaze/snippets/object/traits.rb,
lib/ramaze/snippets/object/thread_accessor.rb,
lib/ramaze/snippets/object/instance_variable_defined.rb
Overview
Extensions for Object
Instance Method Summary collapse
-
#ancestral_trait ⇒ Object
builds a trait from all the ancestors, closer ancestors overwrite distant ancestors.
-
#class_def(name, &blk) ⇒ Object
Defines an instance method within a class.
-
#class_trait ⇒ Object
trait for self.class.
- #false? ⇒ Boolean
- #instance_variable_defined?(variable) ⇒ Boolean
-
#meta_def(name, &blk) ⇒ Object
Adds methods to a metaclass.
- #meta_eval(&blk) ⇒ Object
-
#metaclass ⇒ Object
The hidden singleton lurks behind everyone.
- #pretty(s = '') ⇒ Object
-
#scope ⇒ Object
returns a new clean binding for this object usage: eval ‘self’, object.scope #=> returns object.
- #should(*args, &block) ⇒ Object
-
#trait(hash = nil) ⇒ Object
Adds a method to Object to annotate your objects with certain traits.
- #true? ⇒ Boolean
Methods included from Ramaze::ThreadAccessor
each, #thread_accessor, #thread_reader, #thread_writer
Instance Method Details
#ancestral_trait ⇒ Object
builds a trait from all the ancestors, closer ancestors overwrite distant ancestors
class Foo
trait :one => :eins
trait :first => :erstes
end
class Bar < Foo
trait :two => :zwei
end
class Foobar < Bar
trait :three => :drei
trait :first => :overwritten
end
Foobar.ancestral_trait :two=>:zwei, :one=>:eins, :first=>:overwritten
58 59 60 61 62 63 64 65 |
# File 'lib/ramaze/snippets/object/traits.rb', line 58 def ancestral_trait if respond_to?(:ancestors) ancs = ancestors else ancs = self.class.ancestors end ancs.reverse.inject({}){|s,v| s.merge(v.trait)}.merge(trait) end |
#class_def(name, &blk) ⇒ Object
Defines an instance method within a class
14 15 16 |
# File 'lib/ramaze/snippets/metaid.rb', line 14 def class_def name, &blk class_eval { define_method name, &blk } end |
#class_trait ⇒ Object
trait for self.class
69 70 71 72 73 74 75 |
# File 'lib/ramaze/snippets/object/traits.rb', line 69 def class_trait if respond_to?(:ancestors) trait else self.class.trait end end |
#false? ⇒ Boolean
193 |
# File 'lib/vendor/bacon.rb', line 193 def false?; false; end |
#instance_variable_defined?(variable) ⇒ Boolean
4 5 6 |
# File 'lib/ramaze/snippets/object/instance_variable_defined.rb', line 4 def instance_variable_defined?(variable) instance_variables.include?(variable.to_s) end |
#meta_def(name, &blk) ⇒ Object
Adds methods to a metaclass
9 10 11 |
# File 'lib/ramaze/snippets/metaid.rb', line 9 def name, &blk { define_method name, &blk } end |
#meta_eval(&blk) ⇒ Object
6 |
# File 'lib/ramaze/snippets/metaid.rb', line 6 def &blk; .instance_eval(&blk); end |
#metaclass ⇒ Object
The hidden singleton lurks behind everyone
5 |
# File 'lib/ramaze/snippets/metaid.rb', line 5 def ; class << self; self; end; end |
#pretty(s = '') ⇒ Object
2 3 4 5 |
# File 'lib/ramaze/snippets/object/pretty.rb', line 2 def pretty s = '' PP.pp(self, s) s end |
#scope ⇒ Object
returns a new clean binding for this object
usage: eval 'self', object.scope #=> returns object
7 8 9 |
# File 'lib/ramaze/snippets/object/scope.rb', line 7 def scope lambda{} end |
#should(*args, &block) ⇒ Object
245 |
# File 'lib/vendor/bacon.rb', line 245 def should(*args, &block) Should.new(self).be(*args, &block) end |
#trait(hash = nil) ⇒ Object
Adds a method to Object to annotate your objects with certain traits. It’s basically a simple Hash that takes the current object as key
Example:
class Foo
trait :instance => false
def initialize
trait :instance => true
end
end
Foo.trait[:instance]
# false
foo = Foo.new
foo.trait[:instance]
# true
30 31 32 33 34 35 36 |
# File 'lib/ramaze/snippets/object/traits.rb', line 30 def trait hash = nil if hash Traits[self].merge! hash else Traits[self] end end |
#true? ⇒ Boolean
192 |
# File 'lib/vendor/bacon.rb', line 192 def true?; false; end |