class Relevium::Form

Public Class Methods

from_model(model) click to toggle source
# File lib/relevium/form.rb, line 15
def self.from_model(model)
  new(model.attributes)
rescue StandardError => _e
  nil
end
i18n_scope() click to toggle source
# File lib/relevium/form.rb, line 65
def self.i18n_scope
  :activerecord
end
new(hash) click to toggle source
# File lib/relevium/form.rb, line 10
def initialize(hash)
  set_attributes(hash)
  self
end

Private Class Methods

attribute(attribute, type = nil, remove_from_hash: false) click to toggle source
# File lib/relevium/form.rb, line 75
def self.attribute(attribute, type = nil, remove_from_hash: false)
  attributes << Attribute.new(attribute, type, remove_from_hash)
  self.class_eval { attr_reader attribute }
end
attributes() click to toggle source
# File lib/relevium/form.rb, line 80
def self.attributes
  @attributes ||= Set.new
end
attributes_to_serialize() click to toggle source
# File lib/relevium/form.rb, line 96
def self.attributes_to_serialize
  @attributes_to_serialize ||= []
end
include_in_hash(*methods) click to toggle source
# File lib/relevium/form.rb, line 84
def self.include_in_hash(*methods)
  @methods_for_hash = methods
end
methods_for_hash() click to toggle source
# File lib/relevium/form.rb, line 88
def self.methods_for_hash
  @methods_for_hash ||= []
end
serialize_attributes(*attributes) click to toggle source
# File lib/relevium/form.rb, line 92
def self.serialize_attributes(*attributes)
  @attributes_to_serialize = attributes
end
serialize_relation(relation) click to toggle source
# File lib/relevium/form.rb, line 100
def self.serialize_relation(relation)
  relation.map { |model| from_model(model).serialize }
end

Public Instance Methods

errors_to_string() click to toggle source
# File lib/relevium/form.rb, line 61
def errors_to_string
  errors.full_messages.to_sentence
end
serialize() click to toggle source
# File lib/relevium/form.rb, line 35
def serialize
  hash = {}
  self.class.attributes_to_serialize.each do |attribute|
    hash[attribute] = self.send(attribute) rescue instance_variable_get("@#{attribute}")
  end
  hash.default_proc = proc { |h, k| h.key?(k.to_s) ? h[k.to_s] : nil }
  hash
end
set(name, value) click to toggle source
# File lib/relevium/form.rb, line 44
def set(name, value)
  attribute = self.class.attributes.find { |attr| attr.attribute_name.to_s == name.to_s }
  if attribute.present?
    set_attribute(attribute, value)
  else
    instance_variable_set("@#{name}", value)
  end
  self
end
set_attributes(hash) click to toggle source
# File lib/relevium/form.rb, line 54
def set_attributes(hash)
  hash.to_h.to_a.each do |attr_array|
    set(attr_array[0], attr_array[1])
  end
  self
end
to_h() click to toggle source
# File lib/relevium/form.rb, line 21
def to_h
  hash = {}
  self.instance_variables.each do |var|
    var_name = var.to_s.delete('@')
    attribute = self.class.attributes.find { |attr| attr.attribute_name == var_name.to_sym }
    hash[var_name] = instance_variable_get(var) if attribute&.remove_from_hash == false
  end
  self.class.methods_for_hash.each do |method|
    hash[method] = self.send(method)
  end
  hash.default_proc = proc { |h, k| h.key?(k.to_s) ? h[k.to_s] : nil }
  hash
end

Private Instance Methods

set_attribute(attribute, value) click to toggle source
# File lib/relevium/form.rb, line 71
def set_attribute(attribute, value)
  instance_variable_set(attribute.name_to_instance_variable, attribute.normalized_value(value))
end