Class: HaveAPI::Parameters::Typed

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/parameters/typed.rb

Constant Summary collapse

ATTRIBUTES =
%i(label desc type db_name default fill clean)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, args = {}) ⇒ Typed

Returns a new instance of Typed.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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?
  fail "unused arguments #{myargs}" unless myargs.empty?
end

Instance Attribute Details

#default ⇒ Object (readonly)

Returns the value of attribute default.



7
8
9
# File 'lib/haveapi/parameters/typed.rb', line 7

def default
  @default
end

#desc ⇒ Object (readonly)

Returns the value of attribute desc.



7
8
9
# File 'lib/haveapi/parameters/typed.rb', line 7

def desc
  @desc
end

#label ⇒ Object (readonly)

Returns the value of attribute label.



7
8
9
# File 'lib/haveapi/parameters/typed.rb', line 7

def label
  @label
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/haveapi/parameters/typed.rb', line 7

def name
  @name
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/haveapi/parameters/typed.rb', line 7

def type
  @type
end

Instance Method Details

#add_validator(k, v) ⇒ Object



54
55
56
57
# File 'lib/haveapi/parameters/typed.rb', line 54

def add_validator(k, v)
  @validators ||= HaveAPI::ValidatorChain.new({})
  @validators.add_or_replace(k, v)
end

#clean(raw) ⇒ Object



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
# File 'lib/haveapi/parameters/typed.rb', line 70

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 == 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.new("not in ISO 8601 format '#{raw}'")
    end

  else
    raw
  end

  val
end

#db_name ⇒ Object



27
28
29
# File 'lib/haveapi/parameters/typed.rb', line 27

def db_name
  @db_name || @name
end

#describe(context) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/haveapi/parameters/typed.rb', line 43

def describe(context)
  {
      required: required?,
      label: @label,
      description: @desc,
      type: @type ? @type.to_s : String.to_s,
      validators: @validators ? @validators.describe : {},
      default: @default
  }
end

#fill? ⇒ Boolean

Returns:



39
40
41
# File 'lib/haveapi/parameters/typed.rb', line 39

def fill?
  @fill
end

#format_output(v) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/haveapi/parameters/typed.rb', line 107

def format_output(v)
  if @type == ::Datetime && v.is_a?(Time)
    v.iso8601

  else
    v
  end
end

#optional? ⇒ Boolean

Returns:



35
36
37
# File 'lib/haveapi/parameters/typed.rb', line 35

def optional?
  !@required
end

#patch(attrs) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/haveapi/parameters/typed.rb', line 59

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

Returns:



31
32
33
# File 'lib/haveapi/parameters/typed.rb', line 31

def required?
  @validators ? @validators.required? : false
end

#validate(v, params) ⇒ Object



103
104
105
# File 'lib/haveapi/parameters/typed.rb', line 103

def validate(v, params)
  @validators ? @validators.validate(v, params) : true
end