Class: HaveAPI::Parameters::Typed
- Inherits:
-
Object
- Object
- HaveAPI::Parameters::Typed
- Includes:
- MetadataI18n
- Defined in:
- lib/haveapi/parameters/typed.rb
Constant Summary collapse
- ATTRIBUTES =
%i[ label desc type db_name default fill clean protected load_validators nullable symbolize_keys label_key desc_key ].freeze
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #add_validator(k, v) ⇒ Object
- #clean(raw) ⇒ Object
- #db_name ⇒ Object
- #describe(context, i18n_path: nil) ⇒ Object
- #fill? ⇒ Boolean
- #format_output(v) ⇒ Object
-
#initialize(name, args = {}) ⇒ Typed
constructor
A new instance of Typed.
- #load_validators? ⇒ Boolean
- #nullable? ⇒ Boolean
- #optional? ⇒ Boolean
- #patch(attrs) ⇒ Object
- #required? ⇒ Boolean
- #validate(v, params) ⇒ Object
Methods included from MetadataI18n
Constructor Details
#initialize(name, args = {}) ⇒ Typed
Returns a new instance of Typed.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/haveapi/parameters/typed.rb', line 16 def initialize(name, args = {}) # The hash values are deleted and it shouldn't affect the received hash myargs = args.clone @name = name @label = myargs.delete(:label) || name.to_s.capitalize @layout = :custom (ATTRIBUTES - %i[label]).each do |attr| instance_variable_set("@#{attr}", myargs.delete(attr)) end @type ||= String @validators = HaveAPI::ValidatorChain.new(myargs) unless myargs.empty? raise "unused arguments #{myargs}" unless myargs.empty? end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
14 15 16 |
# File 'lib/haveapi/parameters/typed.rb', line 14 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
14 15 16 |
# File 'lib/haveapi/parameters/typed.rb', line 14 def desc @desc end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
14 15 16 |
# File 'lib/haveapi/parameters/typed.rb', line 14 def label @label end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/haveapi/parameters/typed.rb', line 14 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
14 15 16 |
# File 'lib/haveapi/parameters/typed.rb', line 14 def type @type end |
Instance Method Details
#add_validator(k, v) ⇒ Object
71 72 73 74 |
# File 'lib/haveapi/parameters/typed.rb', line 71 def add_validator(k, v) @validators ||= HaveAPI::ValidatorChain.new({}) @validators.add_or_replace(k, v) end |
#clean(raw) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/haveapi/parameters/typed.rb', line 87 def clean(raw) clean_raw = custom? ? normalize_custom_keys(raw) : raw return validate_cleaned_value(instance_exec(clean_raw, &@clean)) if @clean if raw.nil? return nil if nullable? raise validation_error('haveapi.validation.cannot_be_null') end if raw.is_a?(String) stripped = strip_string(raw) return nil if stripped.empty? && nullable? end if @type.nil? nil elsif @type == Integer coerce_integer(raw) elsif @type == Float coerce_float(raw) elsif @type == Boolean coerce_boolean(raw) elsif @type == ::Datetime coerce_datetime(raw) elsif @type == String || @type == Text coerce_string(raw) elsif custom? clean_raw else raw end end |
#db_name ⇒ Object
34 35 36 |
# File 'lib/haveapi/parameters/typed.rb', line 34 def db_name @db_name || @name end |
#describe(context, i18n_path: nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/haveapi/parameters/typed.rb', line 58 def describe(context, i18n_path: nil) { required: required?, nullable: nullable?, label: localized_label(context, i18n_path), description: localized_description(context, i18n_path), type: @type ? @type.to_s : String.to_s, validators: @validators ? localized_validators(context, i18n_path, @validators.describe) : {}, default: @default, protected: @protected || false } end |
#format_output(v) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/haveapi/parameters/typed.rb', line 132 def format_output(v) if v.nil? nil elsif @type == ::Datetime && v.is_a?(Time) v.iso8601 elsif @type == Boolean v ? true : false elsif @type == Integer v.to_i elsif @type == Float v.to_f elsif @type == String || @type == Text v.to_s else v end end |
#load_validators? ⇒ Boolean
54 55 56 |
# File 'lib/haveapi/parameters/typed.rb', line 54 def load_validators? @load_validators.nil? || @load_validators end |
#nullable? ⇒ Boolean
46 47 48 |
# File 'lib/haveapi/parameters/typed.rb', line 46 def nullable? @nullable == true && optional? end |
#optional? ⇒ Boolean
42 43 44 |
# File 'lib/haveapi/parameters/typed.rb', line 42 def optional? !required? end |
#patch(attrs) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/haveapi/parameters/typed.rb', line 76 def patch(attrs) attrs.each do |k, v| if ATTRIBUTES.include?(k) instance_variable_set("@#{k}", v) else add_validator(k, v) end end end |
#required? ⇒ Boolean
38 39 40 |
# File 'lib/haveapi/parameters/typed.rb', line 38 def required? @validators ? @validators.required? : false end |
#validate(v, params) ⇒ Object
128 129 130 |
# File 'lib/haveapi/parameters/typed.rb', line 128 def validate(v, params) @validators ? @validators.validate(v, params) : true end |