module Sqreen::Kit::Signals::DtoHelper
Provides a helper constructor, default to_h
, and mandatory field checking
Constants
- DO_NOT_CONVERT_TYPES
- RFC_3339_FMT
Public Class Methods
included(mod)
click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 114 def self.included(mod) mod.extend(ClassMethods) end
new(values = {})
click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 118 def initialize(values = {}) values.each do |attr, val| public_send("#{attr}=", val) end end
Public Instance Methods
append_to_h_filter(proc)
click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 144 def append_to_h_filter(proc) @filters ||= [] @filters << proc end
compact_hash(h)
click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 124 def compact_hash(h) h.delete_if { |_k, v| v.nil? } end
to_h()
click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 128 def to_h check_mandatories res = {} self.class.attributes_for_to_h.each do |attr| value = public_send(attr) if (value.class.ancestors & DO_NOT_CONVERT_TYPES).empty? && \ value.respond_to?(:to_h) value = value.to_h end res[attr] = value unless value.nil? end res end
to_json()
click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 149 def to_json return to_h.to_json unless instance_variable_defined?(:@filters) res = @filters.reduce(to_h) { |accum, filter| filter[accum] } res.to_json end
Private Instance Methods
check_mandatories()
click to toggle source
# File lib/sqreen/kit/signals/dto_helper.rb, line 158 def check_mandatories self.class.mandatory_attrs.each do |attr| if public_send(attr).nil? raise "The attribute #{attr} is not set in #{inspect}" end end end