Class: Plumb::Schema::Field

Inherits:
Object
  • Object
show all
Includes:
Callable
Defined in:
lib/plumb/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callable

#parse, #resolve

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

#_typeObject (readonly)

Returns the value of attribute _type.



114
115
116
# File 'lib/plumb/schema.rb', line 114

def _type
  @_type
end

#keyObject (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

#inspectObject



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

#nullableObject



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 options(opts)
  @_type = @_type.options(opts)
  self
end

#policyObject



176
177
178
179
# File 'lib/plumb/schema.rb', line 176

def policy(...)
  @_type = @_type.policy(...)
  self
end

#presentObject



161
162
163
164
# File 'lib/plumb/schema.rb', line 161

def present
  @_type = @_type.present
  self
end

#requiredObject



166
167
168
169
# File 'lib/plumb/schema.rb', line 166

def required
  @_type = Types::Undefined.invalid(errors: 'is required') >> @_type
  self
end