module Aggro::AttributeDSL

Public: Adds a DSL defining attributes and validations.

Public Class Methods

new(attrs = {}) click to toggle source
Calls superclass method
# File lib/aggro/attribute_dsl.rb, line 9
def initialize(attrs = {})
  if Thread.current[:causation_id] && respond_to?(:causation_id=)
    attrs.merge! causation_id: Thread.current[:causation_id]
  end

  if Thread.current[:correlation_id] && respond_to?(:correlation_id=)
    attrs.merge! correlation_id: Thread.current[:correlation_id]
  end

  super
end

Public Instance Methods

attribute(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 38
def attribute(name)
  create_attrs name, Transform::NOOP
end
attributes() click to toggle source
# File lib/aggro/attribute_dsl.rb, line 21
def attributes
  self.class.attributes.keys.reduce({}) do |hash, name|
    hash.merge name => send(name)
  end
end
boolean(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 42
def boolean(name)
  create_attrs name, Transform::Boolean
end
create_attrs(name, transformer) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 93
def create_attrs(name, transformer)
  attr_reader name
  attributes[name] = transformer

  define_method("#{name}=") do |value|
    transformed = self.class.attributes[name].deserialize value
    instance_variable_set "@#{name}", transformed
  end
end
date(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 46
def date(name)
  create_attrs name, Transform::Date
end
email(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 50
def email(name)
  create_attrs name, Transform::Email
end
generate_id(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 54
def generate_id(name)
  create_attrs name, Transform::ID.new(generate: true)
end
id(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 58
def id(name)
  create_attrs name, Transform::ID.new
end
integer(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 62
def integer(name)
  create_attrs name, Transform::Integer
end
money(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 66
def money(name)
  require 'money'
  require 'monetize'

  create_attrs name, Transform::Money
rescue LoadError
  puts '`money` and `monetize` gems must be present to use money type'
end
serialized_attributes() click to toggle source
# File lib/aggro/attribute_dsl.rb, line 27
def serialized_attributes
  self.class.attributes.reduce({}) do |hash, (name, transform)|
    hash.merge name => transform.serialize(send(name))
  end
end
string(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 75
def string(name)
  create_attrs name, Transform::String
end
time(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 79
def time(name)
  create_attrs name, Transform::Time
end
time_interval(name) click to toggle source
# File lib/aggro/attribute_dsl.rb, line 83
def time_interval(name)
  require 'time-interval'

  create_attrs name, Transform::TimeInterval
rescue LoadError
  puts '`time-interval` gem must be present to use time_interval type'
end