Class: ClassInheritable::Array

Inherits:
Object
  • Object
show all
Defined in:
lib/class_inheritable/array.rb

Constant Summary collapse

DEFAULT_ARRAY_NAME =
'class_inheritable_array'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_caller_class) ⇒ Array

Returns a new instance of Array.

Raises:

  • (ArgumentError)


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_classObject (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( options = {} )
  options[ :appender_name ] || "append_#{array_name( options )}"
end

.array_name(options = {}) ⇒ Object



54
55
56
# File 'lib/class_inheritable/array.rb', line 54

def array_name( options = {} )
  options[ :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, options = {} )
  array = array_name( options )
  parent = child.send( :superclass )
  return [] unless parent.respond_to?( array )

  parent.send( array ) || []
end

.attach(options = {}, &block) ⇒ Object

Factory method:

Raises:

  • (ArgumentError)


22
23
24
25
26
27
# File 'lib/class_inheritable/array.rb', line 22

def attach( options = {}, &block )
  caller_class = options[ :to ]
  caller_class ||= calling_object( &block ) if block_given?
  raise ArgumentError, ":to or block required" unless caller_class
  new( caller_class ).attach( options )
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( options = {} )
  options[ :instance_reader_name ] || reader_name( options )
end

.reader_name(options = {}) ⇒ Object



50
51
52
# File 'lib/class_inheritable/array.rb', line 50

def reader_name( options = {} )
  options[ :reader_name ] || array_name( options )
end

.writer_name(options = {}) ⇒ Object



38
39
40
# File 'lib/class_inheritable/array.rb', line 38

def writer_name( options = {} )
  options[ :writer_name ] || "#{array_name( options )}="
end

Instance Method Details

#attach(options = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/class_inheritable/array.rb', line 12

def attach( options = {} )
  define_writer_for( caller_class, options )
  define_appender_for( caller_class, options )
  define_reader_for( caller_class, options )
  define_instance_reader_for( caller_class, options )
  _init( options )
end