Class: Wallaby::ClassArray

Inherits:
Object
  • Object
show all
Includes:
Classifier
Defined in:
lib/wallaby/class_array.rb

Overview

This is a constant-safe array that stores Class value as String.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Classifier

#class_name_of, #to_class

Constructor Details

#initialize(*array) ⇒ ClassArray

Returns a new instance of ClassArray.

Parameters:

  • array (Array)


9
10
11
12
13
14
# File 'lib/wallaby/class_array.rb', line 9

def initialize(*array)
  @internal = (array || []).flatten
  return if @internal.blank?

  @internal.map! { |klass| class_name_of(klass) }.compact!
end

Instance Attribute Details

#internalArray (readonly)

Returns The array to store Class values as String.

Returns:

  • (Array)

    The array to store Class values as String.



18
19
20
# File 'lib/wallaby/class_array.rb', line 18

def internal
  @internal
end

#originArray (readonly)

Returns The original array.

Returns:

  • (Array)

    The original array.



22
23
24
25
# File 'lib/wallaby/class_array.rb', line 22

def origin
  # NOTE: DO NOT cache it using instance variable!
  @internal.map { |klass| to_class(klass) }.compact
end

Instance Method Details

#<<(item) ⇒ ClassArray

Returns self.

Parameters:

  • item (Class, String)

Returns:



45
46
47
48
# File 'lib/wallaby/class_array.rb', line 45

def <<(item)
  @internal << class_name_of(item)
  self
end

#==(other) ⇒ Object

Compare ##origin with other.



58
# File 'lib/wallaby/class_array.rb', line 58

delegate :==, to: :origin

#[](index) ⇒ Object

Return the value for the given index



33
34
35
# File 'lib/wallaby/class_array.rb', line 33

def [](index)
  to_class @internal[index]
end

#[]=(index, value) ⇒ Object

Save the value to the #internal array at the given index, and convert the Class value to String



28
29
30
# File 'lib/wallaby/class_array.rb', line 28

def []=(index, value)
  @internal[index] = class_name_of value
end

#blank?Object



65
# File 'lib/wallaby/class_array.rb', line 65

delegate :blank?, to: :internal

#concat(other) ⇒ ClassArray

Returns new Class array.

Parameters:

  • other (Array)

Returns:



39
40
41
# File 'lib/wallaby/class_array.rb', line 39

def concat(other)
  self.class.new origin.concat(other.try(:origin) || other)
end

#each(&block) ⇒ ClassArray

Returns self.

Returns:



51
52
53
54
# File 'lib/wallaby/class_array.rb', line 51

def each(&block)
  origin.each(&block)
  self
end

#each_with_object(object) ⇒ Object



68
# File 'lib/wallaby/class_array.rb', line 68

delegate :each_with_object, to: :origin

#freezeClassArray

Ensure to freeze the #internal

Returns:



75
76
77
78
# File 'lib/wallaby/class_array.rb', line 75

def freeze
  @internal.freeze
  super
end

#to_aObject

Get the array of ##origin.



62
# File 'lib/wallaby/class_array.rb', line 62

delegate :to_a, to: :origin

#to_sentenceObject



71
# File 'lib/wallaby/class_array.rb', line 71

delegate :to_sentence, to: :origin