module Sequent::Core::Helpers::ParamSupport
Class to support binding from a params hash like the one from Sinatra
You typically do not need to include this module in your classes. If you extend from Sequent::Core::ValueObject
, Sequent::Core::Event
or Sequent::Core::BaseCommand
you will get this functionality for free.
Public Class Methods
included(host_class)
click to toggle source
extend host class with class methods when we’re included
# File lib/sequent/core/helpers/param_support.rb, line 29 def self.included(host_class) host_class.extend(ClassMethods) end
Public Instance Methods
as_params()
click to toggle source
# File lib/sequent/core/helpers/param_support.rb, line 60 def as_params hash = HashWithIndifferentAccess.new self.class.types.each do |field| value = instance_variable_get("@#{field[0]}") next if field[0] == 'errors' hash[field[0]] = if value.is_a?(Array) next if value.blank? value.map { |v| value_to_string(v) } else value_to_string(value) end end hash end
from_params(params, strict_nil_check = true)
click to toggle source
# File lib/sequent/core/helpers/param_support.rb, line 33 def from_params(params, strict_nil_check = true) params = HashWithIndifferentAccess.new(params) self.class.types.each do |attribute, type| value = params[attribute] next if strict_nil_check && value.nil? next if !strict_nil_check && value.blank? if type.respond_to? :from_params value = type.from_params(value) elsif value.is_a?(Array) value = value.map do |v| if type.item_type.respond_to?(:from_params) type.item_type.from_params(v, strict_nil_check) else v end end end instance_variable_set(:"@#{attribute}", value) end end
to_params(root)
click to toggle source
# File lib/sequent/core/helpers/param_support.rb, line 56 def to_params(root) make_params root, as_params end
Private Instance Methods
make_params(key, enumerable, memo = {})
click to toggle source
# File lib/sequent/core/helpers/param_support.rb, line 93 def make_params(key, enumerable, memo = {}) case enumerable when Array enumerable.each_with_index do |object, index| make_params("#{key}[#{index}]", object, memo) end when Hash enumerable.each do |hash_key, object| make_params("#{key}[#{hash_key}]", object, memo) end else memo[key] = enumerable end memo end
value_to_string(val)
click to toggle source
# File lib/sequent/core/helpers/param_support.rb, line 79 def value_to_string(val) if val.is_a?(Sequent::Core::ValueObject) val.as_params elsif val.is_a? DateTime val.iso8601 elsif val.is_a? Date val.iso8601 elsif val.is_a? Time val.iso8601(Sequent.configuration.time_precision) else val end end