Module: Sqreen::Kit::Signals::DtoHelper::ClassMethods

Defined in:
lib/sqreen/kit/signals/dto_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_mandatory_attrs(*args) ⇒ Object



54
55
56
57
# 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_modulesObject

All the classes/modules in the ancestor chain including DtoHelper



33
34
35
36
# 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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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_hObject



38
39
40
41
42
43
44
45
# 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_selfObject



47
48
49
50
51
52
# 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) ⇒ Object



106
107
108
109
110
111
# 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_attrsObject



72
73
74
75
76
77
78
79
# 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) ⇒ Object



59
60
61
62
63
64
65
66
# 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_attrsObject



68
69
70
# File 'lib/sqreen/kit/signals/dto_helper.rb', line 68

def self_mandatory_attrs
  @self_mandatory_attrs ||= []
end

#validate_str_attr(attr, regex) ⇒ Object

method should have been defined initially in an ancestor



21
22
23
24
25
26
27
28
29
# 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