lib/kamerling/uuid_entity.rb in kamerling-0.0.2 vs lib/kamerling/uuid_entity.rb in kamerling-0.0.3
- old
+ new
@@ -1,15 +1,29 @@
+require 'equalizer'
require 'virtus'
+require_relative 'uuid'
-module Kamerling class UUIDEntity
- include Equalizer.new :uuid
+module Kamerling
+ class UUIDEntity
+ include Equalizer.new(:uuid)
- include Virtus.model
+ include Virtus.model
- attribute :uuid, String, default: -> * { UUID.new }
+ attribute :uuid, String, default: -> (*) { UUID.new }
- class << self
- alias_method :from_h, :new
- end
+ def self.attrs(hash = {})
+ hash.each { |name, klass| attribute name, klass }
+ end
- alias_method :to_h, :attributes
-end end
+ def self.defaults(hash = {})
+ hash.each do |name, default|
+ warn_off { attribute name, attribute_set[name].type, default: default }
+ end
+ end
+
+ def to_h
+ attributes.map do |key, value|
+ value.is_a?(UUIDEntity) ? [key, value.to_h] : [key, value]
+ end.to_h
+ end
+ end
+end