lib/smart_kv/meat.rb in smart_kv-0.2.6 vs lib/smart_kv/meat.rb in smart_kv-0.2.7
- old
+ new
@@ -5,31 +5,41 @@
# The meat of the gem
include SmartKv::Convert
def required(*args)
- @required ||= superclass == SmartKv ? Set.new : superclass.required_keys.dup
+ init_required
@required += args
@optional -= @required if @optional
@required
end
def required_keys
+ init_required
@required.to_a
end
+ def init_required
+ @required ||= superclass == SmartKv ? Set.new : superclass.required.dup
+ end
+
def optional(*args)
- @optional ||= superclass == SmartKv ? Set.new : superclass.optional_keys.dup
+ init_optional
@optional += args
@required -= @optional if @required
@optional
end
def optional_keys
+ init_optional
@optional.to_a
end
+ def init_optional
+ @optional ||= superclass == SmartKv ? Set.new : superclass.optional.dup
+ end
+
def keys
Array(@required) + Array(@optional)
end
def check(kv={})
@@ -49,11 +59,11 @@
end
unrecognized_keys = hash.keys - required_keys - optional_keys
unless unrecognized_keys.empty?
key = unrecognized_keys.first
- raise SmartKv::KeyError.new("key not found: #{key.inspect}.", key: key, receiver: (keys - hash.keys).map {|k| [k, nil] }.to_h)
+ raise SmartKv::KeyError.new("unrecognized key: #{key.inspect} for #{self}.", key: key, receiver: (keys - hash.keys).map {|k| [k, nil] }.to_h)
end
end
return to_callable_object(object_class, kv)
end
@@ -69,9 +79,9 @@
private
def prevent_direct_instantiation
if self == SmartKv
- raise SmartKv::InitializationError, "only subclass of SmartConfig can be instantiated".freeze
+ raise SmartKv::CheckError, "only subclass of SmartKv is meant to be used".freeze
end
end
end