Module: Gourami::Attributes

Included in:
Form
Defined in:
lib/gourami/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Extend ClassMethods into including class.

Parameters:

  • klass (Class)


119
120
121
# File 'lib/gourami/attributes.rb', line 119

def self.included(klass)
  klass.send(:extend, ClassMethods)
end

Instance Method Details

#all_attributesHash<Symbol, Object>

Get the all attributes with its values of the current form.

Returns:

  • (Hash<Symbol, Object>)


180
181
182
# File 'lib/gourami/attributes.rb', line 180

def all_attributes
  attributes_hash_from_attributes_options(self.class.attributes)
end

#attribute_provided?(attribute_name) ⇒ Boolean

Returns:

  • (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

#attributesHash<Symbol, Object>

Get the all attributes with its values of the current form except the attributes labeled with skip.

Returns:

  • (Hash<Symbol, Object>)


172
173
174
175
# File 'lib/gourami/attributes.rb', line 172

def attributes
  unskipped_attributes = self.class.attributes.reject { |_, opts| opts[:skip] }
  attributes_hash_from_attributes_options(unskipped_attributes)
end

#attributes_hash_from_attributes_options(attributes_options) ⇒ Hash<Symbol, Object>

Get the all attributes given a hash of attributes with options.

Parameters:

  • attributes_options (Hash<Symbol, Hash>)

    attributes with options

Returns:

  • (Hash<Symbol, Object>)


203
204
205
206
207
# File 'lib/gourami/attributes.rb', line 203

def attributes_hash_from_attributes_options(attributes_options)
  attributes_options.each_with_object({}) do |(name, _), attrs|
    attrs[name] = send(name)
  end
end

#initialize(attrs = {}) ⇒ Object

Initialize a new Gourami::Form form.

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes values to use for the new instance.



127
128
129
# File 'lib/gourami/attributes.rb', line 127

def initialize(attrs = {})
  set_attributes(attrs)
end

#provided_attributesObject



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) }
  attributes_hash_from_attributes_options(provided_attributes)
end

#provided_attributes_namesObject



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.

Parameters:

  • attrs (Hash<[String, Symbol], Object>)


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.

Parameters:

  • attribute_name (Symbol)

    name of the attribute

  • value (*)

    the value as it is passed into the setter

  • options (Hash)

    attribute options

Returns:

  • (*)


165
166
167
# File 'lib/gourami/attributes.rb', line 165

def setter_filter(attribute_name, value, options)
  value
end