module PayuZa::Structs::StructModel

Public Class Methods

included(base) click to toggle source
# File lib/payu_za/structs/struct_model.rb, line 5
def self.included(base)
  base.send(:extend, ClassMethods)
end

Public Instance Methods

operation_name() click to toggle source
# File lib/payu_za/structs/struct_model.rb, line 25
def operation_name
  @operation_name ||= self.class.name.split('::').last.underscore.to_sym
end
to_hash() click to toggle source
# File lib/payu_za/structs/struct_model.rb, line 21
def to_hash
  vars_to_hash
end
valid?() click to toggle source
# File lib/payu_za/structs/struct_model.rb, line 9
def valid?
  validate! rescue false
end
validate!() click to toggle source
# File lib/payu_za/structs/struct_model.rb, line 13
def validate!
  self.class.fields.each do |name, options|
    if options[:required] && send(name).nil?
      raise StructNotValidError.new(name, 'is required')
    end
  end
end

Private Instance Methods

default_for(field) click to toggle source
# File lib/payu_za/structs/struct_model.rb, line 53
def default_for(field)
  default = self.class.fields[field][:default]
  if default.respond_to?(:call)
    default.arity > 0 ?  default.call(self) : default.call
  else
    default
  end
end
hash_value(value) click to toggle source
# File lib/payu_za/structs/struct_model.rb, line 41
def hash_value(value)
  if value.respond_to?(:to_hash)
    value.to_hash
  elsif value.is_a?(Array)
    value.map do |v|
      hash_value(v)
    end
  else
    value
  end
end
vars_to_hash() click to toggle source
# File lib/payu_za/structs/struct_model.rb, line 31
def vars_to_hash
  hash = {}
  self.class.fields.each do |name, _options|
    value = send(name)
    next if value.nil?
    hash[name.to_s] = hash_value(value)
  end
  hash
end