Class: PureForm::Base
- Inherits:
-
Object
- Object
- PureForm::Base
- Includes:
- ActiveModel::Model
- Defined in:
- lib/pure_form/base.rb
Defined Under Namespace
Classes: Boolean
Class Method Summary collapse
- .attribute(name, **options) ⇒ Object
- .copy_attributes_from(model, **options) ⇒ Object
- .default_values ⇒ Object
- .form_name(name) ⇒ Object
- .model_name ⇒ Object
Instance Method Summary collapse
- #assign_attributes(attributes) ⇒ Object (also: #attributes=)
- #assign_defined_attributes(attributes) ⇒ Object
- #attributes ⇒ Object
-
#initialize(attributes = nil) ⇒ Base
constructor
A new instance of Base.
- #update(updates) ⇒ Object
Constructor Details
#initialize(attributes = nil) ⇒ Base
Returns a new instance of Base.
62 63 64 65 |
# File 'lib/pure_form/base.rb', line 62 def initialize(attributes=nil) assign_defaults assign_attributes attributes if attributes end |
Class Method Details
.attribute(name, **options) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/pure_form/base.rb', line 16 def attribute(name, **) attribute = Attribute.new(self, name, ) self.defined_attributes ||= HashWithIndifferentAccess.new self.defined_attributes = defined_attributes.merge(attribute.name => attribute) attribute.define end |
.copy_attributes_from(model, **options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pure_form/base.rb', line 34 def copy_attributes_from(model, **) raise ArgumentError unless model < ActiveRecord::Base included = Array.wrap(.fetch(:only){ model.column_names }).map(&:to_s) excluded = Array.wrap([:except]).map(&:to_s) model.columns.each do |column| next if !column.name.in?(included) || column.name.in?(excluded) attribute column.name, type: column.type end end |
.default_values ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/pure_form/base.rb', line 46 def default_values return {} unless defined_attributes @default_values ||= defined_attributes.each_with_object({}) do |(name, attribute), defaults| if attribute..key?(:default) defaults[name] = attribute..fetch(:default) end end end |
.form_name(name) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/pure_form/base.rb', line 27 def form_name(name) new_name = build_model_name(name) singleton_class.instance_eval do define_method(:model_name){ new_name } end end |
.model_name ⇒ Object
23 24 25 |
# File 'lib/pure_form/base.rb', line 23 def model_name @model_name ||= build_model_name(to_s.remove(/^(\w+::)+/).remove(/Form$/)) end |
Instance Method Details
#assign_attributes(attributes) ⇒ Object Also known as: attributes=
67 68 69 |
# File 'lib/pure_form/base.rb', line 67 def assign_attributes(attributes) Assignment.new(self, attributes).perform end |
#assign_defined_attributes(attributes) ⇒ Object
71 72 73 |
# File 'lib/pure_form/base.rb', line 71 def assign_defined_attributes(attributes) Assignment.new(self, attributes, ignore_undefined: true).perform end |
#attributes ⇒ Object
77 78 79 80 81 |
# File 'lib/pure_form/base.rb', line 77 def attributes self.class.defined_attributes.each_with_object({}) do |(name, _), attributes| attributes[name] = public_send(name) end.with_indifferent_access end |
#update(updates) ⇒ Object
83 84 85 86 |
# File 'lib/pure_form/base.rb', line 83 def update(updates) assign_attributes updates valid? end |