Class: Redress::Form

Inherits:
Dry::Struct
  • Object
show all
Includes:
Hooks::ActiveModel
Defined in:
lib/redress/form.rb

Constant Summary collapse

DEFAULT_NAME =
'Form'
SPLITTER =
'::'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks::ActiveModel

included

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



16
17
18
# File 'lib/redress/form.rb', line 16

def context
  @context
end

Class Method Details

.define_schema(options = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/redress/form.rb', line 44

def self.define_schema(options = nil)
  options.each { |key, value| public_send(key, value) } if options

  yield

  attribute_names.each do |name|
    method_name = :"#{name}="
    next if instance_methods.include?(method_name)

    define_method(method_name) do |value|
      write_attribute(name, value)
    end
  end
end

.from_model(model) ⇒ Object



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

def self.from_model(model)
  Redress::Utils::BuildFormFromModel.new(self, model).build
end

.from_params(params, options = nil) ⇒ Object



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

def self.from_params(params, options = nil)
  new(Redress::Utils::ParseAttributesFromParams.new(self, params, options).to_h)
end

.infer_model_nameObject



29
30
31
32
33
34
# File 'lib/redress/form.rb', line 29

def self.infer_model_name
  class_name = name.split(SPLITTER).last
  return :form if class_name == DEFAULT_NAME

  Redress::Utils::ModelNameString.new(class_name.chomp(DEFAULT_NAME)).to_sym
end

.mimic(model_name) ⇒ Object



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

def self.mimic(model_name)
  @model_name = Redress::Utils::ModelNameString.new(model_name).to_sym
end

.mimicked_model_nameObject



25
26
27
# File 'lib/redress/form.rb', line 25

def self.mimicked_model_name
  @model_name || infer_model_name
end

Instance Method Details

#map_model(model) ⇒ Object



72
73
# File 'lib/redress/form.rb', line 72

def map_model(model)
end

#persisted?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/redress/form.rb', line 59

def persisted?
  respond_to?(:id) && id.present?
end

#propertiesObject



68
69
70
# File 'lib/redress/form.rb', line 68

def properties
  @properties ||= Redress::Utils::AttributesHash.new(to_hash)
end

#with_context(options) ⇒ Object



63
64
65
66
# File 'lib/redress/form.rb', line 63

def with_context(options)
  @context = Hashie::Mash.new(options)
  self
end