From b1dc1506d21fbd9c8a95aef5c00a3845dced87bf Mon Sep 17 00:00:00 2001 From: Glenn Sarti Date: Sat, 8 Jun 2019 21:28:55 +0800 Subject: [PATCH] (maint) Refactor inmemory cache objects Previously the inmemory cache objects duplicated the object properties of the Sidecar protocol but this is not necessary. This commit changes the inmemory cache object to instead inherits from the sidecar and extends it. The from_sidecar! method is modified to dynamically copy the object instead of hardcoded values. --- .../puppet_helper/cache_objects.rb | 65 ++++--------------- .../puppet_helper/cache_objects_spec.rb | 10 +-- 2 files changed, 19 insertions(+), 56 deletions(-) diff --git a/lib/puppet-languageserver/puppet_helper/cache_objects.rb b/lib/puppet-languageserver/puppet_helper/cache_objects.rb index 8897d393..cf4a564c 100644 --- a/lib/puppet-languageserver/puppet_helper/cache_objects.rb +++ b/lib/puppet-languageserver/puppet_helper/cache_objects.rb @@ -2,61 +2,31 @@ module PuppetLanguageServer module PuppetHelper - # key => Unique name of the object - # calling_source => The file that was invoked to create the object - # source => The file that _actually_ created the object - # line => The line number in the source file where the object was created - # char => The character number in the source file where the object was created - # length => The length of characters from `char` in the source file where the object was created - class BasePuppetObject - attr_accessor :key - attr_accessor :calling_source - attr_accessor :source - attr_accessor :line - attr_accessor :char - attr_accessor :length + module CacheExtensions + # origin is used to store where this cache entry came from, for example, workspace or + # default environment attr_accessor :origin def from_sidecar!(value) - @key = value.key - @calling_source = value.calling_source - @source = value.source - @line = value.line - @char = value.char - @length = value.length + (value.class.instance_methods - Object.instance_methods).reject { |name| name.to_s.end_with?('=') || name.to_s.end_with?('!') } + .reject { |name| %i[to_h to_json].include?(name) } + .each do |method_name| + send("#{method_name}=", value.send(method_name)) + end self end end - class PuppetClass < BasePuppetObject - attr_accessor :doc - attr_accessor :parameters - - def from_sidecar!(value) - super - @doc = value.doc - @parameters = value.parameters - self - end + class PuppetClass < PuppetLanguageServer::Sidecar::Protocol::PuppetClass + include CacheExtensions end - class PuppetFunction < BasePuppetObject - attr_accessor :doc - attr_accessor :arity - attr_accessor :type - - def from_sidecar!(value) - super - @doc = value.doc - @arity = value.arity - @type = value.type - self - end + class PuppetFunction < PuppetLanguageServer::Sidecar::Protocol::PuppetFunction + include CacheExtensions end - class PuppetType < BasePuppetObject - attr_accessor :doc - attr_accessor :attributes + class PuppetType < PuppetLanguageServer::Sidecar::Protocol::PuppetType + include CacheExtensions def allattrs @attributes.keys @@ -73,13 +43,6 @@ def properties def meta_parameters @attributes.select { |_name, data| data[:type] == :meta } end - - def from_sidecar!(value) - super - @doc = value.doc - @attributes = value.attributes - self - end end end end diff --git a/spec/languageserver/unit/puppet-languageserver/puppet_helper/cache_objects_spec.rb b/spec/languageserver/unit/puppet-languageserver/puppet_helper/cache_objects_spec.rb index 40cdcb06..4826723e 100644 --- a/spec/languageserver/unit/puppet-languageserver/puppet_helper/cache_objects_spec.rb +++ b/spec/languageserver/unit/puppet-languageserver/puppet_helper/cache_objects_spec.rb @@ -19,11 +19,11 @@ it_should_behave_like 'a base Puppet object' # No additional methods to test - # [:doc].each do |testcase| - # it "instance should respond to #{testcase}" do - # expect(subject).to respond_to(testcase) - # end - # end + [:doc, :parameters].each do |testcase| + it "instance should respond to #{testcase}" do + expect(subject).to respond_to(testcase) + end + end describe '#from_sidecar!' do it 'should populate from a sidecar function object' do