module Dsl::Attributes

Public Class Methods

attributes(*params) click to toggle source
# File lib/dsl/attributes.rb, line 26
def self.attributes(*params)
  raise ActiveFormObjects::DslError.new("[#{self.name}] attributes must not be empty") if params.empty?

  @@attributes[name] = (@@attributes[name] || []) + params
  attr_accessor(*params)
end
ensure_value(param, value) click to toggle source
# File lib/dsl/attributes.rb, line 43
def self.ensure_value(param, value)
  @@overrided_params[name] ||= {}
  @@overrided_params[name][param] = value

  send(:attributes, param)
end
prepare(*params) click to toggle source
# File lib/dsl/attributes.rb, line 59
def self.prepare(*params)
  raise ActiveFormObjects::DslError.new("[#{self.name}] prepare takes a lambda as second argument, not a #{params.last.class.name}") if !params.last.respond_to?(:call)

  lambda_to_call = params.last
  @@preparers[name] = (@@preparers[name] || []) + [{
    key: params.first,
    lambda: lambda_to_call
  }]
  send(:attributes, params.first)
end
remap(key, options) click to toggle source
# File lib/dsl/attributes.rb, line 50
def self.remap(key, options)
  @@remaped_params[name] = (@@remaped_params[name] || []) + [{
    key: key,
    to: options[:to]
  }]

  send(:attributes, options[:to])
end
set_default(params) click to toggle source
# File lib/dsl/attributes.rb, line 33
def self.set_default(params)
  raise ActiveFormObjects::DslError.new("[#{self.name}] set_default must be declared like { value: 'the default', to: :the_attribute }") if params[:to].nil? || params[:value].nil?

  [params[:to]].flatten.each do |param|
    @@default_params[name] ||= {}
    @@default_params[name][param] = params[:value]
  end
  send(:attributes, *params[:to])
end
validates(*params) click to toggle source
Calls superclass method
# File lib/dsl/attributes.rb, line 70
def self.validates(*params)
  send(:attributes, *params[0...-1])
  super(*params)
end

Public Instance Methods

attributes() click to toggle source
# File lib/dsl/attributes.rb, line 19
def attributes
  instance_values.with_indifferent_access.symbolize_keys
end
attrs_only(params) click to toggle source
# File lib/dsl/attributes.rb, line 15
def attrs_only(params)
  attributes.slice(*params)
end