class HalClient::Form::Field
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.
Attributes
aliases[R]
path[R]
Returns the path to which this field should be encoded in JSON documents, if any.
type[R]
value[R]
Public Class Methods
new(parsed_json)
click to toggle source
Initializes a new field.
parsed_json - the parsed JSON of the field
# 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
Public Instance Methods
extract_answer(answers)
click to toggle source
# 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
Protected Instance Methods
coerce_value(val)
click to toggle source
# File lib/hal_client/form/field.rb, line 35 def coerce_value(val) return nil if val.nil? val.to_s end
extract_aliases(parsed_json)
click to toggle source
# File lib/hal_client/form/field.rb, line 41 def extract_aliases(parsed_json) name = parsed_json.fetch("name") { raise ArgumentError, "field doesn't have a name" } [name, name.to_sym] end
extract_path(parsed_json)
click to toggle source
# File lib/hal_client/form/field.rb, line 62 def extract_path(parsed_json) parsed_json["path"] end
extract_type(parsed_json)
click to toggle source
# File lib/hal_client/form/field.rb, line 53 def extract_type(parsed_json) case parsed_json["type"] when /hidden/i :hidden else :string end end
extract_value(parsed_json)
click to toggle source
# File lib/hal_client/form/field.rb, line 49 def extract_value(parsed_json) parsed_json.fetch("value", nil) end