Class: BaggerTag

Inherits:
Object
  • Object
show all
Defined in:
lib/bagger_tag.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, required:, allowed_values: nil) ⇒ BaggerTag

Returns a new instance of BaggerTag


24
25
26
27
28
# File 'lib/bagger_tag.rb', line 24

def initialize(name:, required:, allowed_values: nil)
  @name = name
  @required = required
  @allowed_values = allowed_values
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


22
23
24
# File 'lib/bagger_tag.rb', line 22

def name
  @name
end

Class Method Details

.from_hash(hash) ⇒ Object

Raises:

  • (ArgumentError)

7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bagger_tag.rb', line 7

def self.from_hash(hash)
  raise ArgumentError, "exactly one key expected" unless hash.size == 1

  name = hash.keys.first
  params = hash.values.first

  raise ArgumentError, "fieldRequired is required for #{name} (given: #{params.keys})" if params["fieldRequired"].nil?

  new(
    name: name,
    required: params["fieldRequired"],
    allowed_values: (Set.new(params["valueList"]) if params["valueList"])
)
end

Instance Method Details

#value_valid?(value, errors: []) ⇒ Boolean

Returns:

  • (Boolean)

30
31
32
# File 'lib/bagger_tag.rb', line 30

def value_valid?(value, errors: [])
  present_if_required?(value, errors) && allowed_value?(value, errors)
end