Class: WebFunction::Argument

Inherits:
Object
  • Object
show all
Includes:
Flaggable
Defined in:
lib/web_function/argument.rb

Overview

Arguments are used to define Web Function Endpoint request parameters.

See the [arguments section] on the Web Function website for more details.

[0]: webfunction.org/package#arguments

Instance Attribute Summary collapse

Attributes included from Flaggable

#flags

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Flaggable

#flag?

Constructor Details

#initialize(name:, type:, hint: nil, group: nil, choices: [], flags: [], docs: nil) ⇒ Argument

Returns a new instance of Argument.



13
14
15
16
17
18
19
20
21
# File 'lib/web_function/argument.rb', line 13

def initialize(name:, type:, hint: nil, group: nil, choices: [], flags: [], docs: nil)
  @name = name
  @type = type
  @hint = hint
  @group = group
  @choices = choices
  @flags = flags
  @docs = docs.to_s
end

Instance Attribute Details

#choicesArray (readonly)

An array specifying the exact, case-sensitive values that are permitted for this argument. Each value in the choices array must conform to the data type specified in the argument’s type key.

Note that if the argument type is array, choices may contain strings or numbers representing the allowed values that can be included in the array.

Returns:

  • (Array)


111
112
113
# File 'lib/web_function/argument.rb', line 111

def choices
  @choices
end

#docsString (readonly)

Description of the argument. It must be formatted as markdown.

Returns:

  • (String)


117
118
119
# File 'lib/web_function/argument.rb', line 117

def docs
  @docs
end

#groupString (readonly)

A name used to categorize or group similar arguments together. This should be used by documentation tools to organize related arguments.

Returns:

  • (String)


101
102
103
# File 'lib/web_function/argument.rb', line 101

def group
  @group
end

#hintString (readonly)

A hint that further defines what kind of value to expect for an argument.

See the [hints section] on the Web Function website for the full list of possible hints.

[1]: webfunction.org/package#hints

Returns:

  • (String)


94
95
96
# File 'lib/web_function/argument.rb', line 94

def hint
  @hint
end

#nameString (readonly)

The name of the argument.

Returns:

  • (String)


72
73
74
# File 'lib/web_function/argument.rb', line 72

def name
  @name
end

#typeString (readonly)

The type of the argument. It must be one of:

  • object

  • array

  • string

  • number

  • boolean

Returns:

  • (String)


84
85
86
# File 'lib/web_function/argument.rb', line 84

def type
  @type
end

Class Method Details

.from_array(arguments) ⇒ Array<Argument>

Instantiate a collection of Argument from an array of hash, typically coming from a Package. Uses from_hash under the hood.

Parameters:

  • arguments (Array<Hash>)

Returns:



61
62
63
64
65
# File 'lib/web_function/argument.rb', line 61

def from_array(arguments)
  Utils.normalize_array arguments do |argument|
    from_hash(argument)
  end
end

.from_hash(argument) ⇒ Argument?

Instantiate a new Argument from a hash, typically coming from a Package.

Parameters:

  • argument (Hash)

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/web_function/argument.rb', line 30

def from_hash(argument)
  unless argument.is_a?(Hash)
    return
  end

  unless argument["name"]
    return
  end

  unless argument["type"]
    return
  end

  new(
    name: argument["name"],
    type: argument["type"],
    hint: argument["hint"],
    group: argument["group"],
    choices: [*argument["choices"]],
    flags: Utils.normalize_array_of_strings(argument["flags"]),
    docs: argument["docs"],
  )
end

Instance Method Details

#optional?Boolean

Whether the argument is optional.

Returns:

  • (Boolean)


131
132
133
# File 'lib/web_function/argument.rb', line 131

def optional?
  !required?
end

#required?Boolean

Whether the argument is required.

Returns:

  • (Boolean)


123
124
125
# File 'lib/web_function/argument.rb', line 123

def required?
  flag?("required")
end