module Dystruct::InstanceMethods
Public Class Methods
new(hash = {})
click to toggle source
# File lib/dystruct/instance_methods.rb, line 3 def initialize(hash = {}) check_input_errors(hash) hash = hash.select{|k, v| _permitted.include?(k.to_sym) } if _only_permitted? @attrs = _defaults.merge(hash) attrs.each do |k, v| define_dystruct_method(k, v) end end
Public Instance Methods
_defaults()
click to toggle source
# File lib/dystruct/instance_methods.rb, line 65 def _defaults _get_config[:defaults] || {} end
_equivalents()
click to toggle source
# File lib/dystruct/instance_methods.rb, line 53 def _equivalents _get_config[:equivalents] || [] end
_from_equivalents(ary)
click to toggle source
# File lib/dystruct/instance_methods.rb, line 36 def _from_equivalents(ary) out = nil ary.each do |method| out = attrs[method.to_sym] break if out end out end
_get_config()
click to toggle source
# File lib/dystruct/instance_methods.rb, line 73 def _get_config self.class.settings end
_no_method_error()
click to toggle source
# File lib/dystruct/instance_methods.rb, line 61 def _no_method_error _get_config[:no_method_error].nil? ? true : _get_config[:no_method_error] end
_only_permitted?()
click to toggle source
# File lib/dystruct/instance_methods.rb, line 45 def _only_permitted? _permitted.any? end
_permitted()
click to toggle source
# File lib/dystruct/instance_methods.rb, line 49 def _permitted _get_config[:permitted] || [] end
_presence_required()
click to toggle source
# File lib/dystruct/instance_methods.rb, line 57 def _presence_required _get_config[:presence_required] || [] end
_required_args()
click to toggle source
# File lib/dystruct/instance_methods.rb, line 69 def _required_args _get_config[:required] || [] end
check_input_errors(hash)
click to toggle source
# File lib/dystruct/instance_methods.rb, line 77 def check_input_errors(hash) unless hash.class <= Hash fail WrongArgument, "[Dystruct ERROR]: `#{self.class}` expects to receive a `Hash` or and object having `Hash` as ancestor." end _required_args.map(&:to_sym).each do |r| unless hash.keys.map(&:to_sym).include?(r) fail RequiredFieldNotPresent, "[Dystruct ERROR]: `#{self.class}` expect to be initialized with `#{r}` as an attribute." end end _presence_required.map(&:to_sym).each do |r| if hash[r].nil? fail PresenceRequired, "[Dystruct ERROR]: `#{self.class}` expects to receive an attribute named `#{r}` not beeing `nil`" end end end
find_in_equivalents(name)
click to toggle source
# File lib/dystruct/instance_methods.rb, line 27 def find_in_equivalents(name) found = nil _equivalents.each do |ary| found = ary if ary.include?(name.to_sym) break if found end found end
method_missing(name, *args, &block)
click to toggle source
Calls superclass method
# File lib/dystruct/instance_methods.rb, line 12 def method_missing(name, *args, &block) if ary = find_in_equivalents(name) _from_equivalents(ary) elsif name =~ /\A\w+=\z/ set_attribute_macro(name, *args, &block) else out = provided_macro(name) return out[:out] if out if _no_method_error super # raise NoMethodError, "Method not found for #{self.class}: `#{name}`" end end end