Class: Plumb::Schema::Field
- Inherits:
-
Object
- Object
- Plumb::Schema::Field
- Includes:
- Callable
- Defined in:
- lib/plumb/schema.rb
Instance Attribute Summary collapse
-
#_type ⇒ Object
readonly
Returns the value of attribute _type.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #call(result) ⇒ Object
- #default(v, &block) ⇒ Object
-
#initialize(key, type = nil, &block) ⇒ Field
constructor
A new instance of Field.
- #inspect ⇒ Object
- #match(matcher) ⇒ Object
- #metadata(data = Undefined) ⇒ Object
- #nullable ⇒ Object
- #options(opts) ⇒ Object
- #policy ⇒ Object
- #present ⇒ Object
- #required ⇒ Object
Methods included from Callable
Constructor Details
#initialize(key, type = nil, &block) ⇒ Field
Returns a new instance of Field.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/plumb/schema.rb', line 116 def initialize(key, type = nil, &block) @key = key.to_sym @_type = case type when ArrayClass, Array block_given? ? ArrayClass.new(element_type: Schema.new(&block)) : type when nil block_given? ? Schema.new(&block) : Types::Any when Composable type when Class if type == Array && block_given? ArrayClass.new(element_type: Schema.new(&block)) else Types::Any[type] end else raise ArgumentError, "expected a Plumb type, but got #{type.inspect}" end end |
Instance Attribute Details
#_type ⇒ Object (readonly)
Returns the value of attribute _type.
114 115 116 |
# File 'lib/plumb/schema.rb', line 114 def _type @_type end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
114 115 116 |
# File 'lib/plumb/schema.rb', line 114 def key @key end |
Instance Method Details
#call(result) ⇒ Object
136 |
# File 'lib/plumb/schema.rb', line 136 def call(result) = _type.call(result) |
#default(v, &block) ⇒ Object
138 139 140 141 |
# File 'lib/plumb/schema.rb', line 138 def default(v, &block) @_type = @_type.default(v, &block) self end |
#inspect ⇒ Object
181 182 183 |
# File 'lib/plumb/schema.rb', line 181 def inspect "#{self.class}[#{@_type.inspect}]" end |
#match(matcher) ⇒ Object
171 172 173 174 |
# File 'lib/plumb/schema.rb', line 171 def match(matcher) @_type = @_type.match(matcher) self end |
#metadata(data = Undefined) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/plumb/schema.rb', line 143 def (data = Undefined) if data == Undefined @_type. else @_type = @_type.(data) end end |
#nullable ⇒ Object
156 157 158 159 |
# File 'lib/plumb/schema.rb', line 156 def nullable @_type = @_type.nullable self end |
#options(opts) ⇒ Object
151 152 153 154 |
# File 'lib/plumb/schema.rb', line 151 def (opts) @_type = @_type.(opts) self end |
#policy ⇒ Object
176 177 178 179 |
# File 'lib/plumb/schema.rb', line 176 def policy(...) @_type = @_type.policy(...) self end |
#present ⇒ Object
161 162 163 164 |
# File 'lib/plumb/schema.rb', line 161 def present @_type = @_type.present self end |