Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit de9c4eaf191b7276e109c6b430ccdb73c5316f21 Nikolai Weibull committed
Showing with 280 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +5 −0 README
  3. +15 −0 Rakefile
  4. +63 −0 lib/value/yard-1.0.rb
  5. +24 −0 lib/value/yard/version.rb
  6. +164 −0 test/unit/value/yard-1.0.rb
  7. +4 −0 test/unit/value/yard/version.rb
5 .gitignore
@@ -0,0 +1,5 @@
+*.gem
+*.gemspec
+doc/
+.yardoc/
+.yardopts
5 README
@@ -0,0 +1,5 @@
+ Value-YARD
+
+ Value-YARD provides YARD handlers for Value¹ objects.
+
+¹ Check out the Value library at http://disu.se/software/value
15 Rakefile
@@ -0,0 +1,15 @@
+# -*- coding: utf-8 -*-
+
+require 'inventory/rake-1.0'
+require 'lookout/rake-3.0'
+
+load File.expand_path('../lib/value/yard/version.rb', __FILE__)
+
+Inventory::Rake::Tasks.define Value::YARD::Version, :gem => proc{ |_, s|
+ s.author = 'Nikolai Weibull'
+ s.email = 'now@bitwi.se'
+ s.homepage = 'https://github.com/now/value-yard'
+}
+
+# TODO: Silence warnings generated from YARD (remove this once we plug them)
+Lookout::Rake::Tasks::Test.new :options => []
63 lib/value/yard-1.0.rb
@@ -0,0 +1,63 @@
+# -*- coding: utf-8 -*-
+
+# Value namespace.
+module Value
+end
+
+# Namespace for YARD extensions for Value.
+module Value::YARD
+ load File.expand_path('../yard/version.rb', __FILE__)
+ Version.load
+end
+
+class YARD::Handlers::Ruby::ValuesHandler < YARD::Handlers::Ruby::Base
+ handles method_call('Value')
+ namespace_only
+
+ process do
+ parameters = statement.parameters(false)
+ if YARD::Parser::Ruby::AstNode === parameters[-1][0] and
+ parameters[-1][0].type == :assoc and
+ comparable = parameters[-1].find{ |e| e.jump(:ident).source == 'comparable' }
+ parameters.pop
+ comparables = (comparable[1].type == :array and comparable[1][0].map{ |e| e.jump(:ident).source })
+ ancestor 'Comparable'
+ ancestor 'Value::Comparable'
+ end
+ ancestor 'Value'
+ initialize = define('initialize', parameters.map{ |e| [e.jump(:ident).source, e.type == :array ? e[0][1].source : nil] })
+ initialize.docstring.tags(:param).select{ |e| e.text and not e.text.empty? }.each do |e|
+ define e.name.sub(/\A[&*]/, ''), [],
+ '@return [%s] %s' % [e.types ? e.types.join(', ') : '', e.text], :protected
+ e.text = ''
+ end
+ initialize.docstring.add_tag(YARD::Tags::Tag.new(:note, 'Comparisons between instances are made between %s.' % join(comparables))) if comparables
+ end
+
+ def ancestor(name)
+ modul = Proxy.new(:root, name).tap{ |m| m.type = :module }
+ namespace.mixins(scope).unshift(modul) unless namespace.mixins(scope).include? modul
+ end
+
+ def define(name, parameters, docstring = nil, visibility = :public)
+ YARD::CodeObjects::MethodObject.new(namespace, name).tap{ |m|
+ register(m)
+ m.signature = 'def %s%s' %
+ [name,
+ parameters.empty? ?
+ '' :
+ '(%s)' % parameters.map{ |n, d| d ? '%s = %s' % [n, d] : n }.join(', ')]
+ m.parameters = parameters
+ m.docstring = docstring if docstring
+ m.visibility = visibility
+ }
+ end
+
+ def join(items)
+ return items.join('') if items.size < 2
+ return items.join(' and ') if items.size < 3
+ return '%s, and %s' % [items[0..-2].join(', '), items[-1]]
+ end
+end
+
+YARD::Handlers::Ruby::MacroHandler::IGNORE_METHODS['Value'] = true
24 lib/value/yard/version.rb
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+
+require 'inventory-1.0'
+
+module Value end
+
+module Value::YARD
+ Version = Inventory.new(1, 1, 0){
+ def dependencies
+ super + Inventory::Dependencies.new{
+ development 'inventory-rake', 1, 2, 0
+ development 'lookout', 3, 0, 0
+ development 'lookout-rake', 3, 0, 0
+ optional 'yard', 0, 7, 0
+ }
+ end
+
+ def requires
+ %w'
+ yard
+ '
+ end
+ }
+end
164 test/unit/value/yard-1.0.rb
@@ -0,0 +1,164 @@
+# -*- coding: utf-8 -*-
+
+Expectations do
+ expect [YARD::CodeObjects::Proxy.new(:root, 'Value')] do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # @param [String] a
+ # @param [Integer] b The b
+ Value(:a, :b)
+end
+EOS
+ YARD::Registry.at('A').mixins(:instance)
+ end
+
+ expect 'Represents As.' do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # Represents As.
+ Value(:a, :b)
+end
+EOS
+ YARD::Registry.at('A#initialize').docstring
+ end
+
+ expect [['a', nil], ['b', '3']] do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ Value(:a, [:b, 3])
+end
+EOS
+ YARD::Registry.at('A#initialize').parameters
+ end
+
+ expect [:b, :initialize] do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # @param [String] a
+ # @param [Integer] b The b
+ Value(:a, :b)
+end
+EOS
+ YARD::Registry.all(:method).map(&:name).sort
+ end
+
+ expect [:b, :initialize] do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # @param [String] a
+ # @param [Integer] *b The b
+ Value(:a, :'*b')
+end
+EOS
+ YARD::Registry.all(:method).map(&:name).sort
+ end
+
+ expect [:b, :initialize] do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # @param [String] a
+ # @param [Integer] &b The b
+ Value(:a, :'&b')
+end
+EOS
+ YARD::Registry.all(:method).map(&:name).sort
+ end
+
+ expect nil do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # @param [String] a
+ # @param [Integer] b The b
+ Value(:a, :b)
+end
+EOS
+ YARD::Registry.at('A#a')
+ end
+
+ expect '@return [Integer] The b' do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # @param [String] a
+ # @param [Integer] b The b
+ Value(:a, :b)
+end
+EOS
+ YARD::Registry.at('A#b').docstring.to_raw
+ end
+
+ expect %w'Value Value::Comparable Comparable' do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # @param [String] a
+ Value(:a, :comparable => true)
+end
+EOS
+ YARD::Registry.at('A').inheritance_tree(true)[1..3].map(&:path)
+ end
+
+ expect %w'Value Value::Comparable Comparable' do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+module A
+ # @param [String] a
+ Value(:a, :comparable => true)
+end
+class B
+ include A
+ # @param [String] a
+ # @param [String] b
+ Value(:a, :b, :comparable => true)
+end
+EOS
+ YARD::Registry.at('B').inheritance_tree(true)[1..3].map(&:path)
+ end
+
+ expect 'Comparisons between instances are made between a.' do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # This is A.
+ # @param [String] a
+ # @param [String] b
+ Value(:a, :b, :other => true, :comparable => [:a])
+end
+EOS
+ YARD::Registry.at('A#initialize').docstring.tag(:note).text
+ end
+
+ expect 'Comparisons between instances are made between a and b.' do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # @param [String] a
+ # @param [String] b
+ # @param [String] c
+ Value(:a, :b, :c, :comparable => [:a, :b])
+end
+EOS
+ YARD::Registry.at('A#initialize').docstring.tag(:note).text
+ end
+
+ expect 'Comparisons between instances are made between a, b, and c.' do
+ YARD::Registry.clear
+ YARD::Parser::SourceParser.parse_string(<<EOS)
+class A
+ # @param [String] a
+ # @param [String] b
+ # @param [String] c
+ # @param [String] d
+ Value(:a, :b, :c, :d, :comparable => [:a, :b, :c])
+end
+EOS
+ YARD::Registry.at('A#initialize').docstring.tag(:note).text
+ end
+end
4 test/unit/value/yard/version.rb
@@ -0,0 +1,4 @@
+# -*- coding: utf-8 -*-
+
+Expectations do
+end

0 comments on commit de9c4ea

Please sign in to comment.
Something went wrong with that request. Please try again.