Class: ClassInheritable::Array
- Inherits:
-
Object
- Object
- ClassInheritable::Array
- Defined in:
- lib/class_inheritable/array.rb
Constant Summary collapse
- DEFAULT_ARRAY_NAME =
'class_inheritable_array'
Instance Attribute Summary collapse
-
#caller_class ⇒ Object
readonly
Returns the value of attribute caller_class.
Class Method Summary collapse
- .appender_name(options = {}) ⇒ Object
- .array_name(options = {}) ⇒ Object
-
.array_of_parent(child, options = {}) ⇒ Object
Class methods.
-
.attach(options = {}, &block) ⇒ Object
Factory method:.
- .calling_object(&block) ⇒ Object
- .eigenclass_for(base) ⇒ Object
- .instance_reader_name(options = {}) ⇒ Object
- .reader_name(options = {}) ⇒ Object
- .writer_name(options = {}) ⇒ Object
Instance Method Summary collapse
- #attach(options = {}) ⇒ Object
-
#initialize(_caller_class) ⇒ Array
constructor
A new instance of Array.
Constructor Details
#initialize(_caller_class) ⇒ Array
Returns a new instance of Array.
7 8 9 10 |
# File 'lib/class_inheritable/array.rb', line 7 def initialize( _caller_class ) raise ArgumentError, "invalid calling class" if _caller_class <= ClassInheritable::Array @caller_class = _caller_class end |
Instance Attribute Details
#caller_class ⇒ Object (readonly)
Returns the value of attribute caller_class.
6 7 8 |
# File 'lib/class_inheritable/array.rb', line 6 def caller_class @caller_class end |
Class Method Details
.appender_name(options = {}) ⇒ Object
42 43 44 |
# File 'lib/class_inheritable/array.rb', line 42 def appender_name( = {} ) [ :appender_name ] || "append_#{array_name( )}" end |
.array_name(options = {}) ⇒ Object
54 55 56 |
# File 'lib/class_inheritable/array.rb', line 54 def array_name( = {} ) [ :as ] || DEFAULT_ARRAY_NAME end |
.array_of_parent(child, options = {}) ⇒ Object
Class methods
30 31 32 33 34 35 36 |
# File 'lib/class_inheritable/array.rb', line 30 def array_of_parent( child, = {} ) array = array_name( ) parent = child.send( :superclass ) return [] unless parent.respond_to?( array ) parent.send( array ) || [] end |
.attach(options = {}, &block) ⇒ Object
Factory method:
22 23 24 25 26 27 |
# File 'lib/class_inheritable/array.rb', line 22 def attach( = {}, &block ) caller_class = [ :to ] caller_class ||= calling_object( &block ) if block_given? raise ArgumentError, ":to or block required" unless caller_class new( caller_class ).attach( ) end |
.calling_object(&block) ⇒ Object
62 63 64 |
# File 'lib/class_inheritable/array.rb', line 62 def calling_object( &block ) eval 'self', block.send(:binding) end |
.eigenclass_for(base) ⇒ Object
58 59 60 |
# File 'lib/class_inheritable/array.rb', line 58 def eigenclass_for( base ) class << base; self; end end |
.instance_reader_name(options = {}) ⇒ Object
46 47 48 |
# File 'lib/class_inheritable/array.rb', line 46 def instance_reader_name( = {} ) [ :instance_reader_name ] || reader_name( ) end |
.reader_name(options = {}) ⇒ Object
50 51 52 |
# File 'lib/class_inheritable/array.rb', line 50 def reader_name( = {} ) [ :reader_name ] || array_name( ) end |
.writer_name(options = {}) ⇒ Object
38 39 40 |
# File 'lib/class_inheritable/array.rb', line 38 def writer_name( = {} ) [ :writer_name ] || "#{array_name( )}=" end |
Instance Method Details
#attach(options = {}) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/class_inheritable/array.rb', line 12 def attach( = {} ) define_writer_for( caller_class, ) define_appender_for( caller_class, ) define_reader_for( caller_class, ) define_instance_reader_for( caller_class, ) _init( ) end |