Class: Svelte::ModelFactory::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/svelte/model_factory/parameter.rb

Overview

Helper class to wrap around all parameters

Constant Summary collapse

UNSET =

Constant to represent an unset parameter

Class.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, permitted_values: [], required: false) ⇒ Parameter

Creates a new Parameter

Parameters:

  • type (String)

    : Type of the parameter, i.e. 'integer'

  • permitted_values (Array) (defaults to: [])

    : array of allowed values for the parameter

  • required (Boolean) (defaults to: false)

    : is the parameter required?



24
25
26
27
28
29
# File 'lib/svelte/model_factory/parameter.rb', line 24

def initialize(type, permitted_values: [], required: false)
  @type = type
  @permitted_values = permitted_values
  @required = required
  @value = UNSET
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/svelte/model_factory/parameter.rb', line 16

def type
  @type
end

#valueObject

Returns the value of attribute value.



17
18
19
# File 'lib/svelte/model_factory/parameter.rb', line 17

def value
  @value
end

Instance Method Details

#as_jsonHash

Returns json representation of the parameter.

Returns:

  • (Hash)

    json representation of the parameter



55
56
57
# File 'lib/svelte/model_factory/parameter.rb', line 55

def as_json
  value.respond_to?(:as_json) ? value.as_json : value if present?
end

#present?Boolean

Returns true if and only if the parameter has been set.

Returns:

  • (Boolean)

    true if and only if the parameter has been set



50
51
52
# File 'lib/svelte/model_factory/parameter.rb', line 50

def present?
  !unset?
end

#valid?Boolean

Returns true if and only if the parameter is valid.

Returns:

  • (Boolean)

    true if and only if the parameter is valid



32
33
34
# File 'lib/svelte/model_factory/parameter.rb', line 32

def valid?
  validate.empty?
end

#validateString

Returns String representing the validation errors of the parameter.

Returns:

  • (String)

    String representing the validation errors of the parameter



38
39
40
41
42
43
44
45
46
47
# File 'lib/svelte/model_factory/parameter.rb', line 38

def validate
  # We are not a required parameter, so being unset is fine.
  return '' if validate_blank

  # if we have a nested model
  return value.validate if value.respond_to?(:validate)

  messages = validate_messages
  messages.any? ? 'Invalid parameter: ' + messages.join(', ') : ''
end