diff --git a/examples/plugins/config.ru b/examples/plugins/config.ru new file mode 100644 index 0000000..d7fa571 --- /dev/null +++ b/examples/plugins/config.ru @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib") +require 'hyde' + +app = Hyde::Server.new do + plugin "helloworld.ru" +end + +run app diff --git a/examples/plugins/helloworld.ru b/examples/plugins/helloworld.ru new file mode 100644 index 0000000..31205e9 --- /dev/null +++ b/examples/plugins/helloworld.ru @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib") +require 'hyde' + +app = Hyde::Server.new do + get "/hello" do + header "content-type", "text/plain" + "Hello World!" + end +end + +run app diff --git a/examples/plugins/lib b/examples/plugins/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/plugins/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/lib/hyde/dsl/methods_path.rb b/lib/hyde/dsl/methods_path.rb index d0b727b..279d3c3 100644 --- a/lib/hyde/dsl/methods_path.rb +++ b/lib/hyde/dsl/methods_path.rb @@ -56,6 +56,20 @@ module Hyde @origin.filter(&block) block end + + # Include an application as a child of path. + # @param filename [String] + def plugin(filename) + self.define_singleton_method(:run) do |object| + unless object.is_a? Hyde::Node + raise StandardError, "not a node instance or subclass instance" + end + + object + end + @origin.children.append(self.instance_eval(File.read(filename))) + self.singleton_class.undef_method :run + end end end end