Module: PuppetStrings::Json
- Defined in:
- lib/puppet-strings/json.rb
Overview
The module for JSON related functionality.
Class Method Summary collapse
-
.docstring_to_hash(docstring, select_tags = nil) ⇒ Hash
Converts a YARD::Docstring (or String) to a docstring hash for JSON output.
-
.render(file = nil) ⇒ void
Renders the current YARD registry as JSON to the given file (or STDOUT if nil).
-
.tags_to_hashes(tags) ⇒ Array
Converts a list of tags into an array of hashes.
Class Method Details
.docstring_to_hash(docstring, select_tags = nil) ⇒ Hash
Converts a YARD::Docstring (or String) to a docstring hash for JSON output.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/puppet-strings/json.rb', line 48 def self.docstring_to_hash(docstring, =nil) hash = {} hash[:text] = docstring if docstring.is_a? YARD::Docstring = (docstring..select { |t| .nil? || .include?(t.tag_name.to_sym) }) hash[:tags] = unless .empty? end hash end |
.render(file = nil) ⇒ void
This method returns an undefined value.
Renders the current YARD registry as JSON to the given file (or STDOUT if nil).
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/puppet-strings/json.rb', line 8 def self.render(file = nil) document = { puppet_classes: YARD::Registry.all(:puppet_class).sort_by!(&:name).map!(&:to_hash), defined_types: YARD::Registry.all(:puppet_defined_type).sort_by!(&:name).map!(&:to_hash), resource_types: YARD::Registry.all(:puppet_type).sort_by!(&:name).map!(&:to_hash), providers: YARD::Registry.all(:puppet_provider).sort_by!(&:name).map!(&:to_hash), puppet_functions: YARD::Registry.all(:puppet_function).sort_by!(&:name).map!(&:to_hash), # TODO: Need Ruby documentation? } if file File.open(file, 'w') do |f| f.write(JSON.pretty_generate(document)) f.write("\n") end else puts JSON.pretty_generate(document) end end |
.tags_to_hashes(tags) ⇒ Array
Converts a list of tags into an array of hashes.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/puppet-strings/json.rb', line 31 def self.() # Skip over the API tags that are public .select { |t| (t.tag_name != 'api' || t.text != 'public') }.map do |t| next t.to_hash if t.respond_to?(:to_hash) tag = { tag_name: t.tag_name } tag[:text] = t.text if t.text tag[:types] = t.types if t.types tag[:name] = t.name if t.name tag end end |