Class: Class
- Includes:
- Style
- Defined in:
- lib/more/facets/typecast.rb,
lib/more/facets/aop.rb,
lib/more/facets/prepend.rb,
lib/more/facets/stylize.rb,
lib/core/facets/conversion.rb,
lib/core/facets/module/cattr.rb,
lib/more/facets/classmethods.rb,
lib/core/facets/module/require.rb,
lib/more/facets/class_extension.rb,
lib/core/facets/class/descendents.rb,
lib/core/facets/class/initializer.rb
Overview
Extensions
Defined Under Namespace
Modules: Style
Class Method Summary collapse
-
.from_string(string) ⇒ Object
(also: from_symbol)
“string”.cast_to Class #=> String.
Instance Method Summary collapse
- #apply(aspect) ⇒ Object
-
#aspects ⇒ Object
def cut; @cut; end.
-
#cattr(*syms) ⇒ Object
(also: #mattr)
Creates a class-variable attribute that can be accessed both on an instance and class level.
-
#cattr_accessor(*syms) ⇒ Object
(also: #mattr_accessor)
Creates a class-variable attr_accessor that can be accessed both on an instance and class level.
-
#cattr_reader(*syms) ⇒ Object
(also: #mattr_reader)
Creates a class-variable attr_reader that can be accessed both on an instance and class level.
-
#cattr_writer(*syms) ⇒ Object
(also: #mattr_writer)
Creates a class-variable attr_writer that can be accessed both on an instance and class level.
-
#class_extension(&blk) ⇒ Object
For a class #class_extension is the same as class_eval.
-
#descendents ⇒ Object
(also: #subclasses)
List all descedents of this class.
-
#initializer(*attributes, &block) ⇒ Object
Automatically create an initializer assigning the given arguments.
-
#prepend(aspect) ⇒ Object
Prepend an
aspectmodule to a class. -
#remove_descendents ⇒ Object
(also: #remove_subclasses)
Remove descendents.
-
#to_proc ⇒ Object
Convert instatiation of a class into a Proc.
Methods included from Style
Class Method Details
.from_string(string) ⇒ Object Also known as: from_symbol
“string”.cast_to Class #=> String
206 207 208 209 210 211 212 213 214 |
# File 'lib/more/facets/typecast.rb', line 206 def from_string(string) string = string.to_s.modulize base = string.sub!(/^::/, '') ? Object : (self.kind_of?(Module) ? self : self.class ) klass = string.split(/::/).inject(base){ |mod, name| mod.const_get(name) } return klass if klass.kind_of? Class nil rescue nil end |
Instance Method Details
#apply(aspect) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/more/facets/aop.rb', line 163 def apply(aspect) if aspects.empty? cross_cut(self) #(class << self;self;end).class_eval do # alias_method :__new, :new # def new(*args, &block) # CrossConcerns.new(self,*args, &block) # end #end end aspects.unshift(aspect) end |
#aspects ⇒ Object
def cut; @cut; end
161 |
# File 'lib/more/facets/aop.rb', line 161 def aspects; @aspects ||= []; end |
#cattr(*syms) ⇒ Object Also known as: mattr
Creates a class-variable attribute that can be accessed both on an instance and class level.
NOTE These used to be a Module methods. But turns out these to work as expected when included. The class-level method is not carried along. So the are now just class methods. Accordingly, #mattr will eventually be deprecated so use #cattr instead.
CREDIT: David Heinemeier Hansson
14 15 16 17 18 19 20 21 |
# File 'lib/core/facets/module/cattr.rb', line 14 def cattr( *syms ) accessors, readers = syms.flatten.partition { |a| a.to_s =~ /=$/ } writers = accessors.collect{ |e| e.to_s.chomp('=').to_sym } readers.concat( writers ) cattr_writer( *writers ) cattr_reader( *readers ) return readers + accessors end |
#cattr_accessor(*syms) ⇒ Object Also known as: mattr_accessor
Creates a class-variable attr_accessor that can be accessed both on an instance and class level.
class MyClass
cattr_accessor :a
end
MyClass.a = 10
MyClass.a #=> 10
mc = MyClass.new
mc.a #=> 10
CREDIT: David Heinemeier Hansson
101 102 103 104 105 106 |
# File 'lib/core/facets/module/cattr.rb', line 101 def cattr_accessor(*syms) m = [] m.concat( cattr_reader(*syms) ) m.concat( cattr_writer(*syms) ) m end |
#cattr_reader(*syms) ⇒ Object Also known as: mattr_reader
Creates a class-variable attr_reader that can be accessed both on an instance and class level.
class MyClass
@@a = 10
cattr_reader :a
end
MyClass.a #=> 10
MyClass.new.a #=> 10
CREDIT: David Heinemeier Hansson
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/core/facets/module/cattr.rb', line 38 def cattr_reader( *syms ) syms.flatten.each do |sym| class_eval(<<-EOS, __FILE__, __LINE__) def self.#{sym} @@#{sym} end def #{sym} @@#{sym} end EOS end return syms end |
#cattr_writer(*syms) ⇒ Object Also known as: mattr_writer
Creates a class-variable attr_writer that can be accessed both on an instance and class level.
class MyClass
cattr_writer :a
def a
@@a
end
end
MyClass.a = 10
MyClass.a #=> 10
MyClass.new.a = 29
MyClass.a #=> 29
CREDIT: David Heinemeier Hansson
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/core/facets/module/cattr.rb', line 71 def cattr_writer(*syms) syms.flatten.each do |sym| class_eval(<<-EOS, __FILE__, __LINE__) def self.#{sym}=(obj) @@#{sym} = obj end def #{sym}=(obj) @@#{sym}=(obj) end EOS end return syms end |
#class_extension(&blk) ⇒ Object
For a class #class_extension is the same as class_eval. The alternative is to “undef_method :class_extension”, but this seems uneccessarily limited.
90 91 92 |
# File 'lib/more/facets/class_extension.rb', line 90 def class_extension(&blk) class_eval(&blk) end |
#descendents ⇒ Object Also known as: subclasses
List all descedents of this class.
class X ; end
class A < X; end
class B < X; end
X.descendents #=> [A,B]
NOTE: This is a intesive operation. Do not expect it to be super fast.
13 14 15 16 17 18 19 20 21 |
# File 'lib/core/facets/class/descendents.rb', line 13 def descendents subclass = [] ObjectSpace.each_object( Class ) do |c| if c.ancestors.include?( self ) and self != c subclass << c end end return subclass end |
#initializer(*attributes, &block) ⇒ Object
Automatically create an initializer assigning the given arguments.
class MyClass
initializer(:a, "b", :c)
end
_is equivalent to_
class MyClass
def initialize(a, b, c)
@a,@b,@c = a,b,c
end
end
Downside: Initializers defined like this can’t take blocks. This can be fixed when Ruby 1.9 is out.
The initializer will not raise an Exception when the user does not supply a value for each instance variable. In that case it will just set the instance variable to nil. You can assign default values or raise an Exception in the block.
26 27 28 29 30 31 32 33 34 |
# File 'lib/core/facets/class/initializer.rb', line 26 def initializer(*attributes, &block) define_method(:initialize) do |*args| attributes.zip(args) do |sym, value| instance_variable_set("@#{sym}", value) end instance_eval(&block) if block end attributes end |
#prepend(aspect) ⇒ Object
Prepend an aspect module to a class.
class Firetruck
def put_out_fire(option)
"Put out #{option}"
end
end
module FastFiretruck
def put_out_fire(option)
super("very #{option}!")
end
end
Firetruck.prepend(FastFiretruck)
ft = Firetruck.new ft.put_out_fire(‘fast’) #=> “Put out very fast!”
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/more/facets/prepend.rb', line 75 def prepend( aspect ) _new = method(:new) _allocate = method(:allocate) (class << self; self; end).class_eval do define_method(:new) do |*args| o = _new.call(*args) o.extend aspect o end define_method(:allocate) do |*args| o = _allocate.call(*args) o.extend aspect o end end end |
#remove_descendents ⇒ Object Also known as: remove_subclasses
Remove descendents. This simple deletes the constant associated to the descendents name.
30 31 32 33 34 35 |
# File 'lib/core/facets/class/descendents.rb', line 30 def remove_descendents self.descendents.each do |subclass| Object.send(:remove_const, subclass.name) rescue nil end ObjectSpace.garbage_collect end |
#to_proc ⇒ Object
Convert instatiation of a class into a Proc.
class Person
def initialize(name)
@name = name
end
def inspect
@name.to_str
end
end
%w(john bob jane hans).map(&Person) => [john, bob, jane, hans]
CREDIT: Daniel Schierbeck
89 90 91 |
# File 'lib/core/facets/conversion.rb', line 89 def to_proc proc{|*args| new(*args)} end |