Class: TypedParams::Types::Type

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, name:, match:, coerce:, scalar:, abstract:, archetype:, accepts_block:) ⇒ Type

Returns a new instance of Type.

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/typed_params/types/type.rb', line 9

def initialize(type:, name:, match:, coerce:, scalar:, abstract:, archetype:, accepts_block:)
  raise ArgumentError, ':abstract not allowed with :archetype' if
    abstract && archetype.present?

  raise ArgumentError, ':abstract not allowed with :coerce' if
    abstract && coerce.present?

  raise ArgumentError, ':coerce must be callable' unless
    coerce.nil? || coerce.respond_to?(:call)

  raise ArgumentError, ':match must be callable' unless
    match.respond_to?(:call)

  @name          = name || type
  @type          = type
  @match         = match
  @coerce        = coerce
  @abstract      = abstract
  @archetype     = archetype
  @scalar        = scalar
  @accepts_block = accepts_block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/typed_params/types/type.rb', line 6

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/typed_params/types/type.rb', line 6

def type
  @type
end

Instance Method Details

#abstract?Boolean

Returns:



44
# File 'lib/typed_params/types/type.rb', line 44

def abstract?      = !!@abstract

#accepts_block?Boolean

Returns:



41
# File 'lib/typed_params/types/type.rb', line 41

def accepts_block? = !!@accepts_block

#archetypeObject



32
33
34
35
36
37
38
39
# File 'lib/typed_params/types/type.rb', line 32

def archetype
  return unless @archetype.present?

  return @archetype if
    @archetype.is_a?(Type)

  Types[@archetype]
end

#coercable?Boolean

Returns:



42
# File 'lib/typed_params/types/type.rb', line 42

def coercable?     = !!@coerce

#coerce(value) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/typed_params/types/type.rb', line 68

def coerce(value)
  raise CoercionError, 'type is not coercable' unless
    coercable?

  @coerce.call(value)
rescue
  raise CoercionError, "failed to coerce #{Types.for(value).name} to #{@name}"
end

#humanizeObject



53
54
55
56
57
58
59
# File 'lib/typed_params/types/type.rb', line 53

def humanize
  if subtype?
    "#{@name} #{archetype.humanize}"
  else
    @name.to_s
  end
end

#inspectObject



64
65
66
# File 'lib/typed_params/types/type.rb', line 64

def inspect
  "#<#{self.class.name} type=#{@type.inspect} name=#{@name.inspect} abstract=#{@abstract.inspect} archetype=#{@archetype.inspect}>"
end

#match?(v) ⇒ Boolean

NOTE(ezekg) Using yoda-style self#== because value may be a Type, and

we're overriding Type#coerce, which is a Ruby core method
expected to return an [x, y] tuple, so v == self breaks.

Returns:



50
# File 'lib/typed_params/types/type.rb', line 50

def match?(v)    = self == v || !!@match.call(v)

#mismatch?(v) ⇒ Boolean

Returns:



51
# File 'lib/typed_params/types/type.rb', line 51

def mismatch?(v) = !match?(v)

#scalar?Boolean

Returns:



43
# File 'lib/typed_params/types/type.rb', line 43

def scalar?        = !!@scalar

#subtype?Boolean

Returns:



45
# File 'lib/typed_params/types/type.rb', line 45

def subtype?       = !!@archetype

#to_sObject



62
# File 'lib/typed_params/types/type.rb', line 62

def to_s   = type.to_s

#to_symObject



61
# File 'lib/typed_params/types/type.rb', line 61

def to_sym = type.to_sym