class HaveAPI::Parameters::Typed
Constants
- ATTRIBUTES
Attributes
Public Class Methods
Source
# File lib/haveapi/parameters/typed.rb, line 9 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
Public Instance Methods
Source
# File lib/haveapi/parameters/typed.rb, line 59 def add_validator(k, v) @validators ||= HaveAPI::ValidatorChain.new({}) @validators.add_or_replace(k, v) end
Source
# File lib/haveapi/parameters/typed.rb, line 75 def clean(raw) return instance_exec(raw, &@clean) if @clean if raw.nil? @default elsif @type.nil? nil elsif @type == Integer raw.to_i elsif @type == Float raw.to_f elsif @type == Boolean Boolean.to_b(raw) elsif @type == ::Datetime begin DateTime.iso8601(raw).to_time rescue ArgumentError raise HaveAPI::ValidationError, "not in ISO 8601 format '#{raw}'" end else raw end end
Source
# File lib/haveapi/parameters/typed.rb, line 47 def describe(context) { required: required?, label: @label, description: @desc, type: @type ? @type.to_s : String.to_s, validators: @validators ? @validators.describe : {}, default: @default, protected: @protected || false } end
Source
# File lib/haveapi/parameters/typed.rb, line 109 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 v.to_s else v end end
Source
# File lib/haveapi/parameters/typed.rb, line 43 def load_validators? @load_validators.nil? || @load_validators end
Source
# File lib/haveapi/parameters/typed.rb, line 64 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
Source
# File lib/haveapi/parameters/typed.rb, line 31 def required? @validators ? @validators.required? : false end
Source
# File lib/haveapi/parameters/typed.rb, line 105 def validate(v, params) @validators ? @validators.validate(v, params) : true end