Class: Openapi3Parser::NodeFactory::FieldConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi3_parser/node_factory/field_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_type: nil, factory: nil, required: false, default: nil, validate: nil) ⇒ FieldConfig

Returns a new instance of FieldConfig.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 11

def initialize(
  input_type: nil,
  factory: nil,
  required: false,
  default: nil,
  validate: nil
)
  @given_input_type = input_type
  @given_factory = factory
  @given_required = required
  @given_default = default
  @given_validate = validate
end

Instance Attribute Details

#given_defaultObject (readonly)

Returns the value of attribute given_default.



8
9
10
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 8

def given_default
  @given_default
end

#given_factoryObject (readonly)

Returns the value of attribute given_factory.



8
9
10
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 8

def given_factory
  @given_factory
end

#given_input_typeObject (readonly)

Returns the value of attribute given_input_type.



8
9
10
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 8

def given_input_type
  @given_input_type
end

#given_requiredObject (readonly)

Returns the value of attribute given_required.



8
9
10
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 8

def given_required
  @given_required
end

#given_validateObject (readonly)

Returns the value of attribute given_validate.



8
9
10
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 8

def given_validate
  @given_validate
end

Instance Method Details

#default(factory) ⇒ Object



55
56
57
58
59
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 55

def default(factory)
  return given_default.call if given_default.is_a?(Proc)
  return factory.send(given_default) if given_default.is_a?(Symbol)
  given_default
end

#factory?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 25

def factory?
  !given_factory.nil?
end

#initialize_factory(context, factory) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 29

def initialize_factory(context, factory)
  if given_factory.is_a?(Class)
    given_factory.new(context)
  elsif given_factory.is_a?(Symbol)
    factory.send(given_factory, context)
  else
    given_factory.call(context)
  end
end

#input_type_error(input, factory) ⇒ Object



43
44
45
46
47
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 43

def input_type_error(input, factory)
  return if !given_input_type || input.nil?
  return boolean_error(input) if given_input_type == :boolean
  resolve_type_error(input, factory)
end

#required?(_factory) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 39

def required?(_factory)
  given_required
end

#validation_errors(input, factory) ⇒ Object



49
50
51
52
53
# File 'lib/openapi3_parser/node_factory/field_config.rb', line 49

def validation_errors(input, factory)
  return [] if !given_validate || input.nil?
  errors = resolve_validation_errors(input, factory)
  Array(errors)
end