lib/truck.rb in truck-0.8.6 vs lib/truck.rb in truck-0.8.7

- old
+ new

@@ -10,12 +10,18 @@ attr :contexts @contexts = {} attr_accessor :debug_mode - def define_context(name, **params) - contexts[name] = Context.new(name, **params) + def define_context(name, params = {}) + root, parent, autoload_paths = extract_args!( + params, + :root, + parent: nil, + autoload_paths: ['.'], + ) + contexts[name] = Context.new(name, root, parent, autoload_paths) end def boot! contexts.each_value &:boot! end @@ -35,9 +41,20 @@ private def each_booted_context(&block) return to_enum(:each_booted_context) unless block_given? contexts.each_value.select(&:booted?).each(&block) + end + + def extract_args!(hsh, *mandatory) + optional = mandatory.pop if mandatory.last.is_a? Hash + args = mandatory.map do |key| + hsh.fetch key do raise ArgumentError, "missing keyword: #{key}" end + end + optional.each do |key, default| + args.<< hsh.fetch key, default + end + args end end # Load this last so that when truck itself has unresolvable constants, we throw # up vanilla ruby NameErrors.