From 4312d31268bf7fd3c75dbd4b4fd1156dfdceb68d Mon Sep 17 00:00:00 2001 From: Yessiest Date: Thu, 14 Sep 2023 12:56:29 +0400 Subject: [PATCH] Added application inclusion --- examples/plugins/config.ru | 10 ++++++++++ examples/plugins/helloworld.ru | 13 +++++++++++++ examples/plugins/lib | 1 + lib/hyde/dsl/methods_path.rb | 14 ++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 examples/plugins/config.ru create mode 100644 examples/plugins/helloworld.ru create mode 120000 examples/plugins/lib 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