Class: Sipity::Forms::UpdateSipForm

Inherits:
BaseForm
  • Object
show all
Defined in:
app/forms/sipity/forms/update_sip_form.rb

Overview

Responsible for exposing attributes for editing a given sip.

Since a Sip is a composite of many attributes from numerous sources this object is a bit different.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseForm

#persisted?, #submit, #to_key, #to_param

Constructor Details

#initialize(sip:, exposed_attribute_names: [], attributes: {}) ⇒ UpdateSipForm

Returns a new instance of UpdateSipForm


19
20
21
22
23
# File 'app/forms/sipity/forms/update_sip_form.rb', line 19

def initialize(sip:, exposed_attribute_names: [], attributes: {})
  @sip = sip
  @attributes = attributes.stringify_keys
  self.exposed_attribute_names = exposed_attribute_names
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args, &_block) ⇒ Object


35
36
37
38
39
40
41
# File 'app/forms/sipity/forms/update_sip_form.rb', line 35

def method_missing(method_name, *_args, &_block)
  if exposes?(method_name)
    @attributes[method_name.to_s]
  else
    super
  end
end

Instance Attribute Details

#sipObject (readonly)

Returns the value of attribute sip


25
26
27
# File 'app/forms/sipity/forms/update_sip_form.rb', line 25

def sip
  @sip
end

Class Method Details

.model_nameObject


15
16
17
# File 'app/forms/sipity/forms/update_sip_form.rb', line 15

def self.model_name
  Models::Sip.model_name
end

Instance Method Details

#exposed_attribute_names=(names) ⇒ Object (private)


53
54
55
56
57
# File 'app/forms/sipity/forms/update_sip_form.rb', line 53

def exposed_attribute_names=(names)
  method_names = names.map(&:to_s)
  guard_against_existing_method_names!(method_names)
  @exposed_attribute_names = method_names
end

#exposes?(method_name) ⇒ Boolean

Returns:

  • (Boolean)

43
44
45
# File 'app/forms/sipity/forms/update_sip_form.rb', line 43

def exposes?(method_name)
  @exposed_attribute_names.include?(method_name.to_s)
end

#guard_against_existing_method_names!(method_names) ⇒ Object (private)


59
60
61
62
63
64
65
66
67
# File 'app/forms/sipity/forms/update_sip_form.rb', line 59

def guard_against_existing_method_names!(method_names)
  return true unless method_names.present?
  intersecting_methods = self.class.instance_methods.grep(/^(#{method_names.join('|')})/)
  if intersecting_methods.any?
    fail Exceptions::ExistingMethodsAlreadyDefined.new(self, intersecting_methods)
  else
    return true
  end
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

47
48
49
# File 'app/forms/sipity/forms/update_sip_form.rb', line 47

def respond_to_missing?(method_name, _include_private = false)
  exposes?(method_name) || super
end

#to_modelObject


28
29
30
# File 'app/forms/sipity/forms/update_sip_form.rb', line 28

def to_model
  sip
end