Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/purple/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def callback(&block)
def path(name, method: :get, is_param: false)
path = Path.new(name:, parent: @parent_path, method:, client: self, is_param:)

caller_location = caller_locations(1, 1).first
definition_file = @current_dsl_file || caller_location.absolute_path || caller_location.path
path.instance_variable_set(:@definition_file, definition_file)

@paths ||= []
@paths << path

Expand All @@ -69,6 +73,27 @@ def path(name, method: :get, is_param: false)
@parent_path = path.parent
end

def draw(path)
base_file = @parent_path&.instance_variable_get(:@definition_file)

if base_file.nil?
caller_location = caller_locations(1, 1).first
base_file = @current_dsl_file || caller_location.absolute_path || caller_location.path
end

file_path = path.to_s.end_with?('.rb') ? path.to_s : "#{path}.rb"
absolute_path = File.expand_path(file_path, File.dirname(base_file))

previous_parent_path = @parent_path
previous_current_dsl_file = @current_dsl_file
@current_dsl_file = absolute_path

class_eval(File.read(absolute_path), absolute_path, 1)
ensure
@parent_path = previous_parent_path
@current_dsl_file = previous_current_dsl_file
end

def root_method(method_name)
current_path = @parent_path

Expand Down
11 changes: 11 additions & 0 deletions spec/purple/draw_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

RSpec.describe '.draw' do
it 'loads nested paths from external file into current path scope' do
api_path = TimeMagic::Client.api

expect(api_path.children.map(&:name)).to include(:v1)
expect(api_path.children.map(&:name)).not_to include(:api)
expect(api_path.v1.children.map(&:name)).to include(:status)
end
end
Loading