Class: Redress::Form
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- Redress::Form
- Includes:
- Hooks::ActiveModel
- Defined in:
- lib/redress/form.rb
Constant Summary collapse
- DEFAULT_NAME =
'Form'
- SPLITTER =
'::'
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
- .define_schema(options = nil) ⇒ Object
- .from_model(model) ⇒ Object
- .from_params(params, options = nil) ⇒ Object
- .infer_model_name ⇒ Object
- .mimic(model_name) ⇒ Object
- .mimicked_model_name ⇒ Object
Instance Method Summary collapse
- #map_model(model) ⇒ Object
- #persisted? ⇒ Boolean
- #properties ⇒ Object
- #with_context(options) ⇒ Object
Methods included from Hooks::ActiveModel
Instance Attribute Details
#context ⇒ Object (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( = nil) .each { |key, value| public_send(key, value) } if 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, = nil) new(Redress::Utils::ParseAttributesFromParams.new(self, params, ).to_h) end |
.infer_model_name ⇒ Object
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_name ⇒ Object
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
59 60 61 |
# File 'lib/redress/form.rb', line 59 def persisted? respond_to?(:id) && id.present? end |
#properties ⇒ Object
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() @context = Hashie::Mash.new() self end |