Class: HaveAPI::Parameters::Param
- Inherits:
-
Object
- Object
- HaveAPI::Parameters::Param
- Defined in:
- lib/haveapi/params/param.rb
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(v) ⇒ Object
- #clean(raw) ⇒ Object
- #db_name ⇒ Object
- #describe(context) ⇒ Object
- #fill? ⇒ Boolean
- #format_output(v) ⇒ Object
-
#initialize(name, required: nil, label: nil, desc: nil, type: nil, choices: nil, db_name: nil, default: :_nil, fill: false, clean: nil) ⇒ Param
constructor
A new instance of Param.
- #optional? ⇒ Boolean
- #patch(attrs) ⇒ Object
- #required? ⇒ Boolean
- #validators ⇒ Object
Constructor Details
#initialize(name, required: nil, label: nil, desc: nil, type: nil, choices: nil, db_name: nil, default: :_nil, fill: false, clean: nil) ⇒ Param
Returns a new instance of Param.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/haveapi/params/param.rb', line 5 def initialize(name, required: nil, label: nil, desc: nil, type: nil, choices: nil, db_name: nil, default: :_nil, fill: false, clean: nil) @required = required @name = name @label = label || name.to_s.capitalize @desc = desc @type = type @choices = choices @db_name = db_name @default = default @fill = fill @layout = :custom @validators = {} @clean = clean end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
3 4 5 |
# File 'lib/haveapi/params/param.rb', line 3 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
3 4 5 |
# File 'lib/haveapi/params/param.rb', line 3 def desc @desc end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
3 4 5 |
# File 'lib/haveapi/params/param.rb', line 3 def label @label end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/haveapi/params/param.rb', line 3 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/haveapi/params/param.rb', line 3 def type @type end |
Instance Method Details
#add_validator(v) ⇒ Object
38 39 40 |
# File 'lib/haveapi/params/param.rb', line 38 def add_validator(v) @validators.update(v) end |
#clean(raw) ⇒ Object
62 63 64 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 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/haveapi/params/param.rb', line 62 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 if @choices if @choices.is_a?(Array) unless @choices.include?(val) || @choices.include?(val.to_s.to_sym) raise HaveAPI::ValidationError.new("invalid choice '#{raw}'") end elsif @choices.is_a?(Hash) unless @choices.has_key?(val) || @choices.has_key?(val.to_s.to_sym) raise HaveAPI::ValidationError.new("invalid choice '#{raw}'") end end 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
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/haveapi/params/param.rb', line 46 def describe(context) { required: required?, label: @label, description: @desc, type: @type ? @type.to_s : String.to_s, choices: @choices, validators: @validators, default: @default } end |
#format_output(v) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/haveapi/params/param.rb', line 105 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
58 59 60 |
# File 'lib/haveapi/params/param.rb', line 58 def patch(attrs) attrs.each { |k, v| instance_variable_set("@#{k}", v) } end |
#required? ⇒ Boolean
26 27 28 |
# File 'lib/haveapi/params/param.rb', line 26 def required? @required end |
#validators ⇒ Object
42 43 44 |
# File 'lib/haveapi/params/param.rb', line 42 def validators @validators end |