Class: TypedParams::Types::Type
- Inherits:
-
Object
- Object
- TypedParams::Types::Type
- Defined in:
- lib/typed_params/types/type.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #abstract? ⇒ Boolean
- #accepts_block? ⇒ Boolean
- #archetype ⇒ Object
- #coercable? ⇒ Boolean
- #coerce(value) ⇒ Object
- #humanize ⇒ Object
-
#initialize(type:, name:, match:, coerce:, scalar:, abstract:, archetype:, accepts_block:) ⇒ Type
constructor
A new instance of Type.
- #inspect ⇒ Object
-
#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.
- #mismatch?(v) ⇒ Boolean
- #scalar? ⇒ Boolean
- #subtype? ⇒ Boolean
- #to_s ⇒ Object
- #to_sym ⇒ Object
Constructor Details
#initialize(type:, name:, match:, coerce:, scalar:, abstract:, archetype:, accepts_block:) ⇒ Type
Returns a new instance of Type.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/typed_params/types/type.rb', line 6 def name @name end |
#type ⇒ Object (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
44 |
# File 'lib/typed_params/types/type.rb', line 44 def abstract? = !!@abstract |
#accepts_block? ⇒ Boolean
41 |
# File 'lib/typed_params/types/type.rb', line 41 def accepts_block? = !!@accepts_block |
#archetype ⇒ Object
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
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 |
#humanize ⇒ Object
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 |
#inspect ⇒ Object
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.
50 |
# File 'lib/typed_params/types/type.rb', line 50 def match?(v) = self == v || !!@match.call(v) |
#mismatch?(v) ⇒ Boolean
51 |
# File 'lib/typed_params/types/type.rb', line 51 def mismatch?(v) = !match?(v) |
#to_s ⇒ Object
62 |
# File 'lib/typed_params/types/type.rb', line 62 def to_s = type.to_s |
#to_sym ⇒ Object
61 |
# File 'lib/typed_params/types/type.rb', line 61 def to_sym = type.to_sym |