module Gourami::Attributes

Public Class Methods

included(klass) click to toggle source

Extend ClassMethods into including class.

@param klass [Class]

# File lib/gourami/attributes.rb, line 119
def self.included(klass)
  klass.send(:extend, ClassMethods)
end
new(attrs = {}) click to toggle source

Initialize a new Gourami::Form form.

@param attrs [Hash]

The attributes values to use for the new instance.
# File lib/gourami/attributes.rb, line 127
def initialize(attrs = {})
  set_attributes(attrs)
end

Public Instance Methods

all_attributes() click to toggle source

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

@return [Hash<Symbol, Object>]

# File lib/gourami/attributes.rb, line 180
def all_attributes
  attributes_hash_from_attributes_options(self.class.attributes)
end
attribute_provided?(attribute_name) click to toggle source
# File lib/gourami/attributes.rb, line 194
def attribute_provided?(attribute_name)
  provided_attributes_names.key?(attribute_name.to_s)
end
attributes() click to toggle source

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

@return [Hash<Symbol, Object>]

# 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) click to toggle source

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

@param attributes_options [Hash<Symbol, Hash>] attributes with options

@return [Hash<Symbol, Object>]

# 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
provided_attributes() click to toggle source
# 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_names() click to toggle source
# File lib/gourami/attributes.rb, line 190
def provided_attributes_names
  @provided_attributes_names ||= {}
end
set_attributes(attrs) click to toggle source

Set the attributes belonging to the form. Overrides ALL existing attributes,

including ones not provided in the `attrs` argument.

@param attrs [Hash<[String, Symbol], Object>]

# 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) click to toggle source

Offer descendants the opportunity to modify attribute values as they are set.

@param attribute_name [Symbol] name of the attribute @param value [*] the value as it is passed into the setter @param options [Hash] attribute options

@return [*]

# File lib/gourami/attributes.rb, line 165
def setter_filter(attribute_name, value, options)
  value
end