Class: Sheetah::Types::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/sheetah/types/type.rb

Direct Known Subclasses

Composites::Composite, Scalars::Scalar

Defined Under Namespace

Classes: SimpleCast

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Type

Returns a new instance of Type.



47
48
49
50
51
52
53
# File 'lib/sheetah/types/type.rb', line 47

def initialize(**opts)
  @cast_chain = CastChain.new

  self.class.cast_classes.each do |cast_class|
    @cast_chain.append(cast_class.new(**opts))
  end
end

Class Attribute Details

.cast_classesObject



16
17
18
# File 'lib/sheetah/types/type.rb', line 16

def cast_classes
  defined?(@cast_classes) ? @cast_classes : superclass.cast_classes
end

Instance Attribute Details

#cast_chainObject (readonly)



56
57
58
# File 'lib/sheetah/types/type.rb', line 56

def cast_chain
  @cast_chain
end

Class Method Details

.all(&block) ⇒ Object



9
10
11
12
13
14
# File 'lib/sheetah/types/type.rb', line 9

def all(&block)
  return enum_for(:all) unless block

  ObjectSpace.each_object(singleton_class, &block)
  nil
end

.cast(cast_class = nil, &cast_block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sheetah/types/type.rb', line 22

def cast(cast_class = nil, &cast_block)
  if cast_class && cast_block
    raise ArgumentError, "Expected either a Class or a block, got both"
  elsif !(cast_class || cast_block)
    raise ArgumentError, "Expected either a Class or a block, got none"
  end

  type = Class.new(self)
  type.cast_classes += [cast_class || SimpleCast.new(cast_block)]
  type
end

.freezeObject



34
35
36
37
38
# File 'lib/sheetah/types/type.rb', line 34

def freeze
  @cast_classes = cast_classes.dup unless defined?(@cast_classes)
  @cast_classes.freeze
  super
end

.new!Object



40
41
42
# File 'lib/sheetah/types/type.rb', line 40

def new!(...)
  new(...).freeze
end

Instance Method Details

#castObject



58
59
60
# File 'lib/sheetah/types/type.rb', line 58

def cast(...)
  @cast_chain.call(...)
end

#composite(_value, _messenger) ⇒ Object

Raises:

  • (NoMethodError)


74
75
76
# File 'lib/sheetah/types/type.rb', line 74

def composite(_value, _messenger)
  raise NoMethodError, "You must implement this method in a subclass"
end

#composite?Boolean

Returns:

  • (Boolean)

Raises:

  • (NoMethodError)


66
67
68
# File 'lib/sheetah/types/type.rb', line 66

def composite?
  raise NoMethodError, "You must implement this method in a subclass"
end

#freezeObject



78
79
80
81
# File 'lib/sheetah/types/type.rb', line 78

def freeze
  @cast_chain.freeze
  super
end

#scalar(_index, _value, _messenger) ⇒ Object

Raises:

  • (NoMethodError)


70
71
72
# File 'lib/sheetah/types/type.rb', line 70

def scalar(_index, _value, _messenger)
  raise NoMethodError, "You must implement this method in a subclass"
end

#scalar?Boolean

Returns:

  • (Boolean)

Raises:

  • (NoMethodError)


62
63
64
# File 'lib/sheetah/types/type.rb', line 62

def scalar?
  raise NoMethodError, "You must implement this method in a subclass"
end