Class: HaveAPI::Parameters::Param
- Inherits:
-
Object
- Object
- HaveAPI::Parameters::Param
- Defined in:
- lib/haveapi/params/param.rb
Constant Summary collapse
- ATTRIBUTES =
%i(label desc type db_name default fill clean)
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) ⇒ Object
- #fill? ⇒ Boolean
- #format_output(v) ⇒ Object
-
#initialize(name, args = {}) ⇒ Param
constructor
A new instance of Param.
- #optional? ⇒ Boolean
- #patch(attrs) ⇒ Object
- #required? ⇒ Boolean
- #validate(v, params) ⇒ Object
Constructor Details
#initialize(name, args = {}) ⇒ Param
Returns a new instance of Param.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/haveapi/params/param.rb', line 7 def initialize(name, args = {}) @name = name @label = args.delete(:label) || name.to_s.capitalize @layout = :custom ATTRIBUTES.each do |attr| instance_variable_set("@#{attr}", args.delete(attr)) end @type ||= String @validators = HaveAPI::ValidatorChain.new(args) unless args.empty? fail "unused arguments #{args}" unless args.empty? end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
5 6 7 |
# File 'lib/haveapi/params/param.rb', line 5 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
5 6 7 |
# File 'lib/haveapi/params/param.rb', line 5 def desc @desc end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
5 6 7 |
# File 'lib/haveapi/params/param.rb', line 5 def label @label end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/haveapi/params/param.rb', line 5 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/haveapi/params/param.rb', line 5 def type @type end |
Instance Method Details
#add_validator(k, v) ⇒ Object
49 50 51 52 |
# File 'lib/haveapi/params/param.rb', line 49 def add_validator(k, v) @validators ||= HaveAPI::ValidatorChain.new({}) @validators.add_or_replace(k, v) end |
#clean(raw) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/haveapi/params/param.rb', line 65 def clean(raw) return instance_exec(raw, &@clean) if @clean val = if raw.nil? @default elsif @type.nil? nil elsif @type == Integer raw.to_i elsif @type == Boolean Boolean.to_b(raw) elsif @type == ::Datetime begin Time.iso8601(raw) rescue ArgumentError raise HaveAPI::ValidationError.new("not in ISO 8601 format '#{raw}'") end else raw end val end |
#db_name ⇒ Object
22 23 24 |
# File 'lib/haveapi/params/param.rb', line 22 def db_name @db_name || @name end |
#describe(context) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/haveapi/params/param.rb', line 38 def describe(context) { required: required?, label: @label, description: @desc, type: @type ? @type.to_s : String.to_s, validators: @validators ? @validators.describe : {}, default: @default } end |
#format_output(v) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/haveapi/params/param.rb', line 99 def format_output(v) if @type == ::Datetime && v.is_a?(Time) v.iso8601 else v end end |
#optional? ⇒ Boolean
30 31 32 |
# File 'lib/haveapi/params/param.rb', line 30 def optional? !@required end |
#patch(attrs) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/haveapi/params/param.rb', line 54 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
26 27 28 |
# File 'lib/haveapi/params/param.rb', line 26 def required? @validators ? @validators.required? : false end |
#validate(v, params) ⇒ Object
95 96 97 |
# File 'lib/haveapi/params/param.rb', line 95 def validate(v, params) @validators ? @validators.validate(v, params) : true end |