Module: TypedParams::Types
- Defined in:
- lib/typed_params/types.rb,
lib/typed_params/types/nil.rb,
lib/typed_params/types/date.rb,
lib/typed_params/types/hash.rb,
lib/typed_params/types/time.rb,
lib/typed_params/types/type.rb,
lib/typed_params/types/array.rb,
lib/typed_params/types/float.rb,
lib/typed_params/types/number.rb,
lib/typed_params/types/string.rb,
lib/typed_params/types/symbol.rb,
lib/typed_params/types/boolean.rb,
lib/typed_params/types/decimal.rb,
lib/typed_params/types/integer.rb
Defined Under Namespace
Modules: Boolean Classes: Type
Class Method Summary collapse
- .[](key) ⇒ Object
- .array?(value) ⇒ Boolean
- .coerce(value, to:) ⇒ Object
- .for(value, try: nil) ⇒ Object
- .hash?(value) ⇒ Boolean
- .nil?(value) ⇒ Boolean
- .register(type, match:, name: nil, coerce: nil, archetype: nil, abstract: false, scalar: true, accepts_block: false) ⇒ Object
- .scalar?(value) ⇒ Boolean
- .unregister(type) ⇒ Object
Class Method Details
.[](key) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/typed_params/types.rb', line 48 def self.[](key) type = abstracts[key] || subtypes[key] || types[key] raise ArgumentError, "invalid type: #{key.inspect}" if type.nil? type end |
.array?(value) ⇒ Boolean
43 |
# File 'lib/typed_params/types.rb', line 43 def self.array?(value) = types[:array].match?(value) |
.coerce(value, to:) ⇒ Object
41 |
# File 'lib/typed_params/types.rb', line 41 def self.coerce(value, to:) = (subtypes[to] || types[to]).coerce(value) |
.for(value, try: nil) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/typed_params/types.rb', line 59 def self.for(value, try: nil) _, type = subtypes.slice(*try).find { |_, t| t.match?(value) } || types.find { |_, t| t.match?(value) } raise ArgumentError, "cannot find type for value: #{value.inspect}" if type.nil? type end |
.hash?(value) ⇒ Boolean
44 |
# File 'lib/typed_params/types.rb', line 44 def self.hash?(value) = types[:hash].match?(value) |
.nil?(value) ⇒ Boolean
45 |
# File 'lib/typed_params/types.rb', line 45 def self.nil?(value) = types[:nil].match?(value) |
.register(type, match:, name: nil, coerce: nil, archetype: nil, abstract: false, scalar: true, accepts_block: false) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/typed_params/types.rb', line 12 def self.register(type, match:, name: nil, coerce: nil, archetype: nil, abstract: false, scalar: true, accepts_block: false) raise ArgumentError, "type is already registered: #{type.inspect}" if registry.include?(type) registry << type t = Type.new(type:, name:, match:, coerce:, archetype:, abstract:, scalar:, accepts_block:) case when abstract abstracts[type] = t when archetype subtypes[type] = t else types[type] = t end end |
.scalar?(value) ⇒ Boolean
46 |
# File 'lib/typed_params/types.rb', line 46 def self.scalar?(value) = self.for(value).scalar? |
.unregister(type) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/typed_params/types.rb', line 29 def self.unregister(type) return unless registry.include?(type) t = abstracts.delete(type) || subtypes.delete(type) || types.delete(type) registry.delete(type) if t.present? t end |