(PDOC-23) Add JSON Registry back end
This commit is contained in:
		
							parent
							
								
									d033183002
								
							
						
					
					
						commit
						0b0d3c9587
					
				|  | @ -0,0 +1,52 @@ | |||
| module YARD | ||||
| 
 | ||||
|   class JsonRegistryStore < RegistryStore | ||||
|     def save(merge=true, file=nil) | ||||
|       super | ||||
| 
 | ||||
|       # FIXME: do we need this? | ||||
|       if file && file != @file | ||||
|         @file = file | ||||
|         @serializer = Serializers::JsonSerializer.new(@file) | ||||
|       end | ||||
|       @serializer = Serializers::JsonSerializer.new(@file) | ||||
| 
 | ||||
|       sdb = Registry.single_object_db | ||||
|       original_extension = @serializer.extension | ||||
|       @serializer.extension = 'json' | ||||
|       @serializer.basepath = 'yardoc_json' | ||||
|       interesting_entries = proc { |key, val| | ||||
|         [:puppetnamespace, :hostclass,].include? val.type or | ||||
|         (val.type == :method and (val['puppet_4x_function'] or | ||||
|         val['puppet_3x_function'])) | ||||
|       } | ||||
|       rename_methods = proc { |key, value| | ||||
|         [value.type == :method ? value.name.to_sym : key, | ||||
|         value] | ||||
|       } | ||||
|       if sdb == true || sdb == nil | ||||
|         @serializer.serialize(Hash[@store.select(&interesting_entries).map(&rename_methods)].to_json) | ||||
|       else | ||||
|         values(false).each do |object| | ||||
|           @serializer.serialize(Hash[object.select(&interesting_entries).map(&rename_methods)].to_json) | ||||
|         end | ||||
|       end | ||||
|       @serializer.extension = original_extension | ||||
|       true | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   # Override the serializer because it puts the data at a whacky path and, more | ||||
|   # importantly, mashals the data with a bunch of non-printable characters. | ||||
|   module Serializers | ||||
|     class JsonSerializer < YardocSerializer | ||||
|       def serialize(data) | ||||
|         path = File.join(basepath, "registry_dump.#{extension}") | ||||
|         require 'pry'; binding.pry | ||||
|         log.debug "Serializing json to #{path}" | ||||
|         File.open!(path, "wb") {|f| f.write data } | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
| end | ||||
|  | @ -1,4 +1,5 @@ | |||
| require 'yard' | ||||
| require File.join(File.dirname(__FILE__), './json_registry_store') | ||||
| 
 | ||||
| # TODO: As far as I can tell, monkeypatching is the officially recommended way | ||||
| # to extend these tools to cover custom usecases. Follow up on the YARD mailing | ||||
|  | @ -49,3 +50,19 @@ class YARD::Logger | |||
|     f.close() | ||||
|   end | ||||
| end | ||||
| 
 | ||||
| 
 | ||||
| # 15:04:42       radens | lsegal: where would you tell yard to use your custom RegistryStore? | ||||
| # 15:09:54      @lsegal | https://github.com/lsegal/yard/blob/master/lib/yard/registry.rb#L428-L435 | ||||
| # 15:09:54      @lsegal | you would set that attr on Registry | ||||
| # 15:09:54      @lsegal | it might be worth expanding that API to swap out the store class used | ||||
| # 15:10:49      @lsegal | specifically | ||||
| #                       | https://github.com/lsegal/yard/blob/master/lib/yard/registry.rb#L190 and | ||||
| #                       | replace RegistryStore there with a storage_class attr | ||||
| module YARD::Registry | ||||
|   class << self | ||||
|   def clear | ||||
|     self.thread_local_store = YARD::JsonRegistryStore.new | ||||
|   end | ||||
|   end | ||||
| end | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	 Ian Kronquist
						Ian Kronquist