lib/evil/client/model.rb in evil-client-0.2.1 vs lib/evil/client/model.rb in evil-client-0.2.2
- old
+ new
@@ -8,40 +8,44 @@
#
class Evil::Client
class Model
class << self
include Dry::Initializer::Mixin
- alias_method :attribute, :option
- alias_method :param, :option
def new(value)
return value if value.is_a? self
value = value.to_h.each_with_object({}) do |(key, val), obj|
obj[key.to_sym] = val
end
super value
end
+ alias_method :call, :new
+ alias_method :[], :new
- def call(value)
- new(value).to_h
+ def attributes
+ @attributes ||= []
end
- alias_method :[], :call
- end
- tolerant_to_unknown_options
+ def option(name, type = nil, **opts)
+ super.tap { attributes << name.to_sym }
+ end
+ alias_method :attribute, :option
+ alias_method :param, :option
+ private
+
+ def inherited(klass)
+ super
+ klass.instance_variable_set :@attributes, attributes.dup
+ end
+ end
+
def ==(other)
- return false unless other.respond_to? :to_h
- to_h == other.to_h
+ other.respond_to?(:to_h) ? to_h == other.to_h : false
end
def to_h
- attributes = method(:initialize)
- .parameters
- .map { |item| item[1] unless item[0] == :keyrest }
- .compact
-
- attributes.each_with_object({}) do |key, hash|
+ self.class.attributes.each_with_object({}) do |key, hash|
val = send(key)
hash[key] = hashify(val) unless val == Dry::Initializer::UNDEFINED
end
end
alias_method :[], :send