Class: HalClient::Form::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/hal_client/form/field.rb

Overview

A single field in a form.

Current implementation is very basic. It only understands hidden and string field types. All other field types are treated as string per the spec.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed_json) ⇒ Field

Initializes a new field.

parsed_json - the parsed JSON of the field



13
14
15
16
17
18
# File 'lib/hal_client/form/field.rb', line 13

def initialize(parsed_json)
  @aliases = extract_aliases(parsed_json)
  @value = extract_value(parsed_json)
  @type = extract_type(parsed_json)
  @path = extract_path(parsed_json)
end

Instance Attribute Details

#pathObject (readonly)

Returns the path to which this field should be encoded in JSON documents, if any.



21
22
23
# File 'lib/hal_client/form/field.rb', line 21

def path
  @path
end

Instance Method Details

#extract_answer(answers) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/hal_client/form/field.rb', line 23

def extract_answer(answers)
  return value if :hidden == type

  key = aliases.find{|maybe_key| answers.has_key?(maybe_key) }

  coerce_value(answers.fetch(key, value))
end