Class: ShafClient::Form

Inherits:
Resource show all
Defined in:
lib/shaf_client/form.rb

Direct Known Subclasses

HalForm, ShafForm

Constant Summary

Constants included from MimeTypes

MimeTypes::DEFAULT_ADAPTER, MimeTypes::MIME_TYPE_HAL, MimeTypes::MIME_TYPE_JSON

Instance Attribute Summary

Attributes inherited from Resource

#headers, #http_status

Attributes inherited from BaseResource

#attributes, #curies, #embedded_resources, #links

Instance Method Summary collapse

Methods inherited from Resource

build, content_type, #destroy!, #get, #get_doc, #get_hal_form, #initialize, #inspect, profile, #reload!

Methods inherited from BaseResource

#actions, #attribute, #curie, #embedded, #initialize, #inspect, #link, #rel?, #to_h, #to_s

Constructor Details

This class inherits a constructor from ShafClient::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ShafClient::BaseResource

Instance Method Details

#[](key) ⇒ Object



19
20
21
# File 'lib/shaf_client/form.rb', line 19

def [](key)
  values.fetch(key.to_sym)
end

#[]=(key, value) ⇒ Object



23
24
25
26
# File 'lib/shaf_client/form.rb', line 23

def []=(key, value)
  values.fetch(key.to_sym) # Raise KeyError unless key exist!
  values[key.to_sym] = value
end

#content_typeObject

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/shaf_client/form.rb', line 40

def content_type
  raise NotImplementedError
end

#fieldsObject

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/shaf_client/form.rb', line 44

def fields
  raise NotImplementedError
end

#http_methodObject

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/shaf_client/form.rb', line 36

def http_method
  raise NotImplementedError
end

#submitObject



48
49
50
51
52
53
54
55
# File 'lib/shaf_client/form.rb', line 48

def submit
  client.send(
    http_method,
    target,
    payload: encoded_payload,
    headers: {'Content-Type' => content_type}
  )
end

#targetObject

Raises:

  • (NotImplementedError)


32
33
34
# File 'lib/shaf_client/form.rb', line 32

def target
  raise NotImplementedError
end

#titleObject

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/shaf_client/form.rb', line 28

def title
  raise NotImplementedError
end

#valid?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
# File 'lib/shaf_client/form.rb', line 57

def valid?
  field_value_mapping

  fields.all? do |field|
    value = values[field.name.to_sym]
    field.valid? value
  end
end

#valuesObject



11
12
13
14
15
16
17
# File 'lib/shaf_client/form.rb', line 11

def values
  return @values if defined? @values

  @values = fields.each_with_object({}) do |field, values|
    values[field.name] = field.value
  end
end