Class: Svelte::ModelFactory::Parameter
- Inherits:
-
Object
- Object
- Svelte::ModelFactory::Parameter
- 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
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Json representation of the parameter.
-
#initialize(type, permitted_values: [], required: false) ⇒ Parameter
constructor
Creates a new Parameter.
-
#present? ⇒ Boolean
True if and only if the parameter has been set.
-
#valid? ⇒ Boolean
True if and only if the parameter is valid.
-
#validate ⇒ String
String representing the validation errors of the parameter.
Constructor Details
#initialize(type, permitted_values: [], required: false) ⇒ Parameter
Creates a new Parameter
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
#type ⇒ Object (readonly)
Returns the value of attribute type.
16 17 18 |
# File 'lib/svelte/model_factory/parameter.rb', line 16 def type @type end |
#value ⇒ Object
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_json ⇒ Hash
Returns 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.
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.
32 33 34 |
# File 'lib/svelte/model_factory/parameter.rb', line 32 def valid? validate.empty? end |
#validate ⇒ String
Returns 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) = .any? ? 'Invalid parameter: ' + .join(', ') : '' end |