Module: Sequent::Core::Helpers::ParamSupport
- Included in:
- BaseCommand, ValueObject
- Defined in:
- lib/sequent/core/helpers/param_support.rb
Overview
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.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(host_class) ⇒ Object
extend host class with class methods when we’re included.
Instance Method Summary collapse
Class Method Details
.included(host_class) ⇒ Object
extend host class with class methods when we’re included
20 21 22 |
# File 'lib/sequent/core/helpers/param_support.rb', line 20 def self.included(host_class) host_class.extend(ClassMethods) end |
Instance Method Details
#as_params ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sequent/core/helpers/param_support.rb', line 43 def as_params hash = HashWithIndifferentAccess.new self.class.types.each do |field| value = self.instance_variable_get("@#{field[0]}") next if field[0] == "errors" if value.respond_to?(:as_params) && value.kind_of?(ValueObject) value = value.as_params elsif value.kind_of?(Array) value = value.map { |val| val.kind_of?(ValueObject) ? val.as_params : val } elsif value.is_a? DateTime value = value.iso8601 elsif value.is_a? Date value = value.strftime("%d-%m-%Y") # TODO Remove to TypeConverter end hash[field[0]] = value end hash end |
#from_params(params) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sequent/core/helpers/param_support.rb', line 24 def from_params(params) params = HashWithIndifferentAccess.new(params) self.class.types.each do |attribute, type| value = params[attribute] next if value.blank? if type.respond_to? :from_params value = type.from_params(value) elsif type.is_a? Sequent::Core::Helpers::ArrayWithType value = value.map { |v| type.item_type.from_params(v) } end instance_variable_set(:"@#{attribute}", value) end end |
#to_params(root) ⇒ Object
39 40 41 |
# File 'lib/sequent/core/helpers/param_support.rb', line 39 def to_params(root) make_params root, as_params end |