module Sqreen::Kit::Signals::DtoHelper::ClassMethods

Public Instance Methods

add_mandatory_attrs(*args) click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 54
def add_mandatory_attrs(*args)
  @self_mandatory_attrs ||= []
  @self_mandatory_attrs += args
end
all_implementing_modules() click to toggle source

All the classes/modules in the ancestor chain including DtoHelper

# File lib/sqreen/kit/signals/dto_helper.rb, line 33
def all_implementing_modules
  ancestors
    .select { |c| c != DtoHelper && c.ancestors.include?(DtoHelper) }
end
attr_accessor_time(attr) click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 81
def attr_accessor_time(attr)
  define_method :"#{attr}=" do |value|
    unless value.is_a?(Time)
      unless value.is_a?(String)
        raise ArgumentError, 'expected Time or String object'
      end
      unless value =~ /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/
        raise ArgumentError, "Invalid time format for #{value}"
      end
    end
    instance_variable_set("@#{attr}", value)
  end

  define_method attr do
    var = "@#{attr}"
    return nil unless instance_variable_defined?(var)

    cur_val = instance_variable_get(var)

    return nil if cur_val.nil?
    return cur_val if cur_val.is_a?(String)
    cur_val.strftime(RFC_3339_FMT)
  end
end
attributes_for_to_h() click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 38
def attributes_for_to_h
  @all_attributes ||= begin
    all_implementing_modules
      .map(&:attributes_for_to_h_self)
      .reduce(:+)
      .uniq
  end
end
attributes_for_to_h_self() click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 47
def attributes_for_to_h_self
  methods = public_instance_methods(false)

  methods.reject { |m| m.to_s.end_with? '=' }
         .select { |m| methods.include?(:"#{m}=") }
end
included(mod) click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 106
def included(mod)
  # make sure that classes/modules indirectly including DtoHelper
  # also have their singleton class including ClassMethods
  return if mod.singleton_class.ancestors.include?(ClassMethods)
  mod.extend(ClassMethods)
end
mandatory_attrs() click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 72
def mandatory_attrs
  @mandatory_attrs ||= begin
    all_implementing_modules
      .map(&:self_mandatory_attrs)
      .reduce(:+)
      .uniq
  end
end
readonly_attrs(*attrs) click to toggle source
Calls superclass method
# File lib/sqreen/kit/signals/dto_helper.rb, line 59
def readonly_attrs(*attrs)
  attrs.each do |attr|
    define_method :"#{attr}=" do |value|
      raise "Attribute #{attr} is read-only" unless public_send(attr).nil?
      super(value)
    end
  end
end
self_mandatory_attrs() click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 68
def self_mandatory_attrs
  @self_mandatory_attrs ||= []
end
validate_str_attr(attr, regex) click to toggle source

method should have been defined initially in an ancestor

Calls superclass method
# File lib/sqreen/kit/signals/dto_helper.rb, line 21
def validate_str_attr(attr, regex)
  define_method(:"#{attr}=") do |val|
    unless val =~ regex
      raise "Unexpected format for attribute #{attr}: " \
            "'#{val}' does not match #{regex}"
    end
    super(val)
  end
end