Module: Gourami::Attributes
- Included in:
- Form
- Defined in:
- lib/gourami/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(klass) ⇒ Object
Extend ClassMethods into including class.
Instance Method Summary collapse
-
#all_attributes ⇒ Hash<Symbol, Object>
Get the all attributes with its values of the current form.
- #attribute_provided?(attribute_name) ⇒ Boolean
-
#attributes ⇒ Hash<Symbol, Object>
Get the all attributes with its values of the current form except the attributes labeled with skip.
-
#attributes_hash_from_attributes_options(attributes_options) ⇒ Hash<Symbol, Object>
Get the all attributes given a hash of attributes with options.
-
#initialize(attrs = {}) ⇒ Object
Initialize a new Gourami::Form form.
- #provided_attributes ⇒ Object
- #provided_attributes_names ⇒ Object
-
#set_attributes(attrs) ⇒ Object
Set the attributes belonging to the form.
-
#setter_filter(attribute_name, value, options) ⇒ *
Offer descendants the opportunity to modify attribute values as they are set.
Class Method Details
.included(klass) ⇒ Object
Extend ClassMethods into including class.
119 120 121 |
# File 'lib/gourami/attributes.rb', line 119 def self.included(klass) klass.send(:extend, ClassMethods) end |
Instance Method Details
#all_attributes ⇒ Hash<Symbol, Object>
Get the all attributes with its values of the current form.
180 181 182 |
# File 'lib/gourami/attributes.rb', line 180 def all_attributes (self.class.attributes) end |
#attribute_provided?(attribute_name) ⇒ Boolean
194 195 196 |
# File 'lib/gourami/attributes.rb', line 194 def attribute_provided?(attribute_name) provided_attributes_names.key?(attribute_name.to_s) end |
#attributes ⇒ Hash<Symbol, Object>
Get the all attributes with its values of the current form except the attributes labeled with skip.
172 173 174 175 |
# File 'lib/gourami/attributes.rb', line 172 def attributes unskipped_attributes = self.class.attributes.reject { |_, opts| opts[:skip] } (unskipped_attributes) end |
#attributes_hash_from_attributes_options(attributes_options) ⇒ Hash<Symbol, Object>
Get the all attributes given a hash of attributes with options.
203 204 205 206 207 |
# File 'lib/gourami/attributes.rb', line 203 def () .each_with_object({}) do |(name, _), attrs| attrs[name] = send(name) end end |
#initialize(attrs = {}) ⇒ Object
Initialize a new Gourami::Form form.
127 128 129 |
# File 'lib/gourami/attributes.rb', line 127 def initialize(attrs = {}) set_attributes(attrs) end |
#provided_attributes ⇒ Object
184 185 186 187 188 |
# File 'lib/gourami/attributes.rb', line 184 def provided_attributes unskipped_attributes = self.class.attributes.reject { |_, opts| opts[:skip] } provided_attributes = unskipped_attributes.select { |name, _| attribute_provided?(name) } (provided_attributes) end |
#provided_attributes_names ⇒ Object
190 191 192 |
# File 'lib/gourami/attributes.rb', line 190 def provided_attributes_names @provided_attributes_names ||= {} end |
#set_attributes(attrs) ⇒ Object
Set the attributes belonging to the form.
Overrides ALL existing attributes,
including ones not provided in the attrs argument.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/gourami/attributes.rb', line 136 def set_attributes(attrs) return unless attrs.kind_of?(Hash) attrs = attrs.map { |k, v| [k.to_s, v] }.to_h self.class.attributes.each do |name, opts = {}| name = name.to_s if attrs.key?(name) value = attrs[name] provided_attributes_names[name] = opts end if value.nil? && opts[:required] && !opts[:default] # TODO: Consider raising this during validate or perform instead. raise RequiredAttributeError, "#{name.inspect} is a required attribute of #{self.class.to_s}" end send(:"_#{name}=", value) end end |
#setter_filter(attribute_name, value, options) ⇒ *
Offer descendants the opportunity to modify attribute values as they are set.
165 166 167 |
# File 'lib/gourami/attributes.rb', line 165 def setter_filter(attribute_name, value, ) value end |