yard-mongoid 0.0.3.pre → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -6,4 +6,8 @@ gemspec
6
6
  group :development do
7
7
  gem 'rake'
8
8
  gem 'mongoid', '~> 3.0'
9
+ gem 'yard-activerecord' # Needed for some code examples
10
+ gem 'yard-activerecord2' # Needed for some code examples
11
+ gem 'yard-rails' # Needed for some code examples
12
+ gem 'yard-delegate' # Needed for some code examples
9
13
  end
@@ -1,4 +1,4 @@
1
- module YARD
1
+ module YARD::Handlers::Ruby
2
2
  module Mongoid
3
3
  module Extensions
4
4
  protected
@@ -0,0 +1,149 @@
1
+ require 'yard'
2
+ require 'yard/handlers/ruby/mongoid/extensions'
3
+ require 'yard/handlers/ruby/mongoid/helpers'
4
+
5
+ module YARD::Handlers::Ruby
6
+ module Mongoid
7
+ class FieldHandler < YARD::Handlers::Ruby::AttributeHandler
8
+ include Extensions
9
+ include YARD::Handlers::Ruby::Mongoid::Helpers
10
+
11
+ MONGOID_FIELDS = 'Fields'
12
+
13
+ namespace_only
14
+
15
+ handles method_call(:field)
16
+
17
+ def process
18
+ name = statement.parameters[0].last
19
+
20
+ if name.type == :symbol
21
+ name = name.source[1..-1]
22
+
23
+ add_field_getter(effected_namespace, name, class_name, default_value)
24
+ add_field_setter(effected_namespace, name, class_name)
25
+ #register_field_presence(effected_namespace, name, scope)
26
+ #register_field_change(effected_namespace, name, scope)
27
+ #register_field_changed(effected_namespace, name, scope)
28
+ #register_field_was(effected_namespace, name, scope)
29
+ #register_field_reset(effected_namespace, name, scope)
30
+ end
31
+ end
32
+
33
+ protected
34
+
35
+ def class_name
36
+ return @class_name if instance_variable_defined?(:@class_name)
37
+
38
+ if statement.parameters.size > 2
39
+ statement.parameters[1].source.split(/,\s*/).each do |key_and_value|
40
+ if key_and_value =~ hash_args[:type]
41
+ @class_name = $2
42
+ break
43
+ end
44
+ end
45
+ @class_name = 'true, false' if @class_name == 'Boolean'
46
+ end
47
+ @class_name ||= 'String'
48
+ end
49
+
50
+ def default_value
51
+ return @default_value if instance_variable_defined?(:@default_value)
52
+
53
+ if statement.parameters.size > 2
54
+ statement.parameters[1].source.split(/,\s*/).each do |key_and_value|
55
+ if key_and_value =~ /(:default\s*=>|default:)\s*(.+)\s*$/
56
+ @default_value = $2
57
+ break
58
+ end
59
+ end
60
+ end
61
+ @default_value ||= nil
62
+ end
63
+
64
+ protected
65
+
66
+ def hash_args
67
+ @hash_args ||= Hash.new do |hash, key|
68
+ hash[key] = /(:#{key.to_s}\s*=>|#{key.to_s}:\s)\s*([A-Z][A-Za-z0-9]*)/
69
+ end
70
+ end
71
+
72
+ # @yieldparam [YARD::CodeObjects::MethodObject]
73
+ def add_field_method(namespace, name, &block)
74
+ register YARD::CodeObjects::MethodObject.new(namespace, name, :instance) do |mo|
75
+ mo.group = MONGOID_FIELDS
76
+ mo.visibility ||= :public
77
+ mo.source = statement.source
78
+ mo.dynamic = true
79
+ mo.signature = "def #{name}"
80
+ mo.docstring = '' if mo.docstring.empty?
81
+ block.call(mo) if block_given?
82
+ end
83
+ end
84
+
85
+ # Creates and registers a new +name+ method in +namespace+
86
+ # with an instance or class +scope+
87
+ # @param [NamespaceObject] namespace the namespace
88
+ # @param [String, Symbol] name the method name
89
+ def add_field_getter(namespace, name, type = 'Object', default_value = nil)
90
+ add_field_method(namespace, name) do |o|
91
+ default_value = "(defaults to: +#{default_value}+) " if default_value
92
+ o.docstring.add_tag get_tag(:return, default_value, type)
93
+ end
94
+ end
95
+
96
+ # Creates and registers a new +name+= method in +namespace+
97
+ # with an instance or class +scope+
98
+ # @param [NamespaceObject] namespace the namespace
99
+ # @param [String, Symbol] name the method name
100
+ def add_field_setter(namespace, name, type = 'Object')
101
+ add_field_method(namespace, "#{name}=") do |o|
102
+ o.parameters = [[name, nil]]
103
+ o.docstring.add_tag get_tag(:return, nil, type)
104
+ o.docstring.add_tag get_tag(:param, "new #{name}", type, name)
105
+ end
106
+ end
107
+
108
+ # Creates and registers a new +name+? method in +namespace+
109
+ # with an instance or class +scope+
110
+ # @param [NamespaceObject] namespace the namespace
111
+ # @param [String, Symbol] name the method name
112
+ def add_field_presence(namespace, name)
113
+ add_field_method(namespace, "#{name}?")
114
+ end
115
+
116
+ # Creates and registers a new +name+_change method in +namespace+
117
+ # with an instance or class +scope+
118
+ # @param [NamespaceObject] namespace the namespace
119
+ # @param [String, Symbol] name the method name
120
+ def add_field_change(namespace, name)
121
+ add_field_method(namespace, "#{name}_change")
122
+ end
123
+
124
+ # Creates and registers a new +name+_changed? method in +namespace+
125
+ # with an instance or class +scope+
126
+ # @param [NamespaceObject] namespace the namespace
127
+ # @param [String, Symbol] name the method name
128
+ def add_field_changed(namespace, name)
129
+ add_field_method(namespace, "#{name}_changed?")
130
+ end
131
+
132
+ # Creates and registers a new +name+_was method in +namespace+
133
+ # with an instance or class +scope+
134
+ # @param [NamespaceObject] namespace the namespace
135
+ # @param [String, Symbol] name the method name
136
+ def register_field_was(namespace, name)
137
+ add_field_method(namespace, "#{name}_was")
138
+ end
139
+
140
+ # Creates and registers a new reset_+name+! method in +namespace+
141
+ # with an instance or class +scope+
142
+ # @param [NamespaceObject] namespace the namespace
143
+ # @param [String, Symbol] name the method name
144
+ def register_field_reset(namespace, name)
145
+ add_field_method(namespace, "reset_#{name}!")
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,18 @@
1
+ require 'yard/tags/tag'
2
+
3
+ module YARD
4
+ module Handlers
5
+ module Ruby
6
+ module Mongoid
7
+ module Helpers
8
+ # @param [Symbol] tag
9
+ # @param [String] text
10
+ # @param [String] return_classes
11
+ def get_tag(tag, text, return_classes = [], name=nil)
12
+ YARD::Tags::Tag.new(tag, text, [return_classes].flatten, name)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,56 @@
1
+ require 'yard'
2
+ require 'active_support/inflector'
3
+ require 'yard/handlers/ruby/mongoid/helpers'
4
+
5
+ module YARD::Handlers::Ruby::Mongoid
6
+ module Relations
7
+ class Base < YARD::Handlers::Ruby::MethodHandler
8
+ include YARD::Handlers::Ruby::Mongoid::Helpers
9
+ namespace_only
10
+
11
+ def process
12
+ namespace.groups << group_name unless namespace.groups.include? group_name
13
+
14
+ object = YARD::CodeObjects::MethodObject.new(namespace, method_name)
15
+ object.group = group_name
16
+ object.docstring = return_description
17
+ object.docstring.add_tag get_tag(:return, '', class_name)
18
+ object.docstring.add_tag get_tag(:see, "http://mongoid.org/en/mongoid/docs/relations.html##{statement.method_name(true).to_s}")
19
+ object.dynamic = true
20
+ register object
21
+ end
22
+
23
+ def group_name
24
+ 'Mongoid Relations'
25
+ end
26
+
27
+ private
28
+ def method_name
29
+ call_params[0]
30
+ end
31
+
32
+ def class_name(singularize = false)
33
+ param_size = statement.parameters.size
34
+ if param_size > 2
35
+ i = 1
36
+ while i < param_size - 1
37
+ # May want to evaluate doing it this way
38
+ statement.parameters[i].jump(:hash).source =~ /(:class_name\s*=>|class_name:)\s*["']([^"']+)["']/
39
+ return $2 if $2
40
+ i += 1
41
+ end
42
+ end
43
+
44
+ if singularize
45
+ method_name.camelize.singularize
46
+ else
47
+ method_name.camelize
48
+ end
49
+ end
50
+
51
+ def return_description
52
+ "An array of associated #{method_name}."
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,16 @@
1
+ require 'yard/handlers/ruby/mongoid/relations/singular_handler'
2
+
3
+ module YARD::Handlers::Ruby::Mongoid::Relations
4
+ class BelongsToHandler < SingularHandler
5
+ handles method_call(:belongs_to)
6
+
7
+ def group_name
8
+ 'Belongs to'
9
+ end
10
+
11
+ private
12
+ def return_description
13
+ ''
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'yard/handlers/ruby/mongoid/relations/plural_handler'
2
+
3
+ module YARD::Handlers::Ruby::Mongoid::Relations
4
+ class HasManyHandler < PluralHandler
5
+ handles method_call(:has_and_belongs_to_many)
6
+
7
+ def group_name
8
+ 'Has and belongs to many'
9
+ end
10
+
11
+ private
12
+ def return_description
13
+ ''
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'yard/handlers/ruby/mongoid/relations/plural_handler'
2
+
3
+ module YARD::Handlers::Ruby::Mongoid::Relations
4
+ class HasManyHandler < PluralHandler
5
+ handles method_call(:has_many)
6
+
7
+ def group_name
8
+ 'Has many'
9
+ end
10
+
11
+ private
12
+ def return_description
13
+ ''
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'yard/handlers/ruby/mongoid/relations/singular_handler'
2
+
3
+ module YARD::Handlers::Ruby::Mongoid::Relations
4
+ class HasOneHandler < SingularHandler
5
+ handles method_call(:has_one)
6
+
7
+ def group_name
8
+ 'Has one'
9
+ end
10
+
11
+ private
12
+ def return_description
13
+ ''
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ require 'yard/handlers/ruby/mongoid/relations/base'
2
+
3
+ module YARD::Handlers::Ruby::Mongoid::Relations
4
+ class PluralHandler < Base
5
+ def class_name
6
+ "Array<#{super(true)}>"
7
+ end
8
+
9
+ private
10
+ def return_description
11
+ "An array of associated #{method_name.humanize}"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'yard/handlers/ruby/mongoid/relations/base'
2
+
3
+ module YARD::Handlers::Ruby::Mongoid::Relations
4
+ class SingularHandler < Base
5
+ def class_name
6
+ super(false)
7
+ end
8
+
9
+ private
10
+ def return_description
11
+ "An associated {#{method_name.humanize}}"
12
+ end
13
+ end
14
+ end
data/lib/yard/mongoid.rb CHANGED
@@ -1,9 +1,17 @@
1
+ require 'yard'
1
2
  require 'yard/mongoid/version'
3
+
2
4
  module YARD
3
- module Mongoid
4
- MONGOID_FIELDS = 'Mongoid Fields'
5
+ module Handlers
6
+ module Ruby
7
+ module Mongoid
8
+ end
9
+ end
5
10
  end
6
11
  end
7
12
 
8
- require 'yard/mongoid/field_handler'
9
- require 'yard/mongoid/legacy/field_handler'
13
+ require 'yard/handlers/ruby/mongoid/field_handler'
14
+ require 'yard/handlers/ruby/mongoid/relations/belongs_to_handler'
15
+ require 'yard/handlers/ruby/mongoid/relations/has_one_handler'
16
+ require 'yard/handlers/ruby/mongoid/relations/has_many_handler'
17
+ require 'yard/handlers/ruby/mongoid/relations/has_and_belongs_to_many_handler'
@@ -1,5 +1,5 @@
1
1
  module Yard
2
2
  module Mongoid
3
- VERSION = '0.0.3.pre'
3
+ VERSION = '0.0.3'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yard-mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3.pre
5
- prerelease: 6
4
+ version: 0.0.3
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alexander Semyonov
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-07 00:00:00.000000000 Z
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yard
@@ -40,11 +40,17 @@ files:
40
40
  - MIT-LICENSE
41
41
  - Rakefile
42
42
  - lib/yard-mongoid.rb
43
+ - lib/yard/handlers/ruby/mongoid/extensions.rb
44
+ - lib/yard/handlers/ruby/mongoid/field_handler.rb
45
+ - lib/yard/handlers/ruby/mongoid/helpers.rb
46
+ - lib/yard/handlers/ruby/mongoid/relations/base.rb
47
+ - lib/yard/handlers/ruby/mongoid/relations/belongs_to_handler.rb
48
+ - lib/yard/handlers/ruby/mongoid/relations/has_and_belongs_to_many_handler.rb
49
+ - lib/yard/handlers/ruby/mongoid/relations/has_many_handler.rb
50
+ - lib/yard/handlers/ruby/mongoid/relations/has_one_handler.rb
51
+ - lib/yard/handlers/ruby/mongoid/relations/plural_handler.rb
52
+ - lib/yard/handlers/ruby/mongoid/relations/singular_handler.rb
43
53
  - lib/yard/mongoid.rb
44
- - lib/yard/mongoid/extensions.rb
45
- - lib/yard/mongoid/field_handler.rb
46
- - lib/yard/mongoid/helpers.rb
47
- - lib/yard/mongoid/legacy/field_handler.rb
48
54
  - lib/yard/mongoid/version.rb
49
55
  - yard-mongoid.gemspec
50
56
  homepage: http://rubygems.org/gems/yard-mongoid
@@ -62,9 +68,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
68
  required_rubygems_version: !ruby/object:Gem::Requirement
63
69
  none: false
64
70
  requirements:
65
- - - ! '>'
71
+ - - ! '>='
66
72
  - !ruby/object:Gem::Version
67
- version: 1.3.1
73
+ version: '0'
68
74
  requirements: []
69
75
  rubyforge_project: yard-mongoid
70
76
  rubygems_version: 1.8.23
@@ -1,66 +0,0 @@
1
- require 'yard/mongoid/extensions'
2
- require 'yard/mongoid/helpers'
3
-
4
- module YARD
5
- module Mongoid
6
- class FieldHandler < YARD::Handlers::Ruby::AttributeHandler
7
- include Extensions
8
- include Helpers
9
-
10
- handles method_call(:field)
11
-
12
- def process
13
- name = statement.parameters[0].last
14
-
15
- if name.type == :symbol
16
- name = name.source[1..-1]
17
-
18
- register_field_getter(effected_namespace, name, scope, class_name, default_value)
19
- register_field_setter(effected_namespace, name, scope)
20
- register_field_presence(effected_namespace, name, scope)
21
- register_field_change(effected_namespace, name, scope)
22
- register_field_changed(effected_namespace, name, scope)
23
- register_field_was(effected_namespace, name, scope)
24
- register_field_reset(effected_namespace, name, scope)
25
- super
26
- end
27
- end
28
-
29
- protected
30
-
31
- def class_name
32
- return @class_name if instance_variable_defined?(:@class_name)
33
-
34
- if statement.parameters.size > 2
35
- statement.parameters[1].source.split(/,\s*/).each do |key_and_value|
36
- if key_and_value =~ hash_args[:type]
37
- @class_name = $2
38
- break
39
- end
40
- end
41
- end
42
- @class_name ||= 'String'
43
- end
44
-
45
- def default_value
46
- return @default_value if instance_variable_defined?(:@default_value)
47
-
48
- if statement.parameters.size > 2
49
- statement.parameters[1].source.split(/,\s*/).each do |key_and_value|
50
- if key_and_value =~ /(:default\s*=>|default:)\s*(.+)\s*$/
51
- @default_value = $2
52
- break
53
- end
54
- end
55
- end
56
- @default_value ||= nil
57
- end
58
-
59
- protected
60
-
61
- def hash_args
62
- @hash_args ||= Hash.new { |hash, key| hash[key] = /(:#{key.to_s}\s*=>|#{key.to_s}:)\s*([A-Z][A-Za-z0-9]*)/ }
63
- end
64
- end
65
- end
66
- end
@@ -1,114 +0,0 @@
1
- module YARD
2
- module Mongoid
3
- module Helpers
4
- def register_new_method_object(namespace, name, scope = :instance, &block)
5
- register self.class.const_get(:MethodObject).new(namespace, name, scope, &block)
6
- end
7
-
8
- # Creates and registers a new +name+ method in +namespace+
9
- # with an instance or class +scope+
10
- # @param [NamespaceObject] namespace the namespace
11
- # @param [String, Symbol] name the method name
12
- # @param [Symbol] scope +:instance+ or +:class+
13
- def register_field_getter(namespace, name, scope = :instance, type = 'Object', default_value = nil)
14
- register_new_method_object(namespace, name, scope) do |o|
15
- o.group = MONGOID_FIELDS
16
- o.visibility = :public
17
- o.source = statement.source
18
- o.signature = "def #{name}"
19
- end.tap do |o|
20
- docstring = o.docstring.empty? ? "Field #{name}" : o.docstring
21
- docstring += "\n@return [#{type}] "
22
- docstring += "(defaults to: +#{default_value}+) " if default_value
23
- o.docstring = docstring
24
- end
25
- end
26
-
27
- # Creates and registers a new +name+= method in +namespace+
28
- # with an instance or class +scope+
29
- # @param [NamespaceObject] namespace the namespace
30
- # @param [String, Symbol] name the method name
31
- # @param [Symbol] scope +:instance+ or +:class+
32
- def register_field_setter(namespace, name, scope = :instance)
33
- register_new_method_object(namespace, "#{name}=", scope) do |o|
34
- o.group = MONGOID_FIELDS
35
- o.visibility = :public
36
- o.source = statement.source
37
- o.signature = "def #{name}=(value)"
38
- o.parameters = [['value', nil]]
39
- o.docstring = "Sets the attribute #{name}\n@param value the value to set the attribute #{name} to."
40
- end
41
- end
42
-
43
- # Creates and registers a new +name+? method in +namespace+
44
- # with an instance or class +scope+
45
- # @param [NamespaceObject] namespace the namespace
46
- # @param [String, Symbol] name the method name
47
- # @param [Symbol] scope +:instance+ or +:class+
48
- def register_field_presence(namespace, name, scope = :instance)
49
- register_new_method_object(namespace, "#{name}?", scope) do |o|
50
- o.group = MONGOID_FIELDS
51
- o.visibility = :public
52
- o.source = statement.source
53
- o.signature = "def #{name}?"
54
- end
55
- end
56
-
57
- # Creates and registers a new +name+_change method in +namespace+
58
- # with an instance or class +scope+
59
- # @param [NamespaceObject] namespace the namespace
60
- # @param [String, Symbol] name the method name
61
- # @param [Symbol] scope +:instance+ or +:class+
62
- def register_field_change(namespace, name, scope = :instance)
63
- register_new_method_object(namespace, "#{name}_change", scope) do |o|
64
- o.group = MONGOID_FIELDS
65
- o.visibility = :public
66
- o.source = statement.source
67
- o.signature = "def #{name}_change"
68
- end
69
- end
70
-
71
- # Creates and registers a new +name+_changed? method in +namespace+
72
- # with an instance or class +scope+
73
- # @param [NamespaceObject] namespace the namespace
74
- # @param [String, Symbol] name the method name
75
- # @param [Symbol] scope +:instance+ or +:class+
76
- def register_field_changed(namespace, name, scope = :instance)
77
- register_new_method_object(namespace, "#{name}_changed?", scope) do |o|
78
- o.group = MONGOID_FIELDS
79
- o.visibility = :public
80
- o.source = statement.source
81
- o.signature = "def #{name}_changed?"
82
- end
83
- end
84
-
85
- # Creates and registers a new +name+_was method in +namespace+
86
- # with an instance or class +scope+
87
- # @param [NamespaceObject] namespace the namespace
88
- # @param [String, Symbol] name the method name
89
- # @param [Symbol] scope +:instance+ or +:class+
90
- def register_field_was(namespace, name, scope = :instance)
91
- register_new_method_object(namespace, "#{name}_was", scope) do |o|
92
- o.group = MONGOID_FIELDS
93
- o.visibility = :public
94
- o.source = statement.source
95
- o.signature = "def #{name}_was"
96
- end
97
- end
98
-
99
- # Creates and registers a new reset_+name+! method in +namespace+
100
- # with an instance or class +scope+
101
- # @param [NamespaceObject] namespace the namespace
102
- # @param [String, Symbol] name the method name
103
- # @param [Symbol] scope +:instance+ or +:class+
104
- def register_field_reset(namespace, name, scope = :instance)
105
- register_new_method_object(namespace, "reset_#{name}!", scope) do |o|
106
- o.group = MONGOID_FIELDS
107
- o.visibility = :public
108
- o.source = statement.source
109
- o.signature = "def reset_#{name}!"
110
- end
111
- end
112
- end
113
- end
114
- end
@@ -1,26 +0,0 @@
1
- require 'yard/mongoid/helpers'
2
-
3
- module YARD
4
- module Mongoid
5
- module Legacy
6
- class FieldHandler < YARD::Handlers::Ruby::Legacy::AttributeHandler
7
- include YARD::Mongoid::Helpers
8
-
9
- handles /\Afield\s+:/
10
-
11
- def process
12
- name = statement.tokens[2,1].to_s[1..-1]
13
-
14
- register_field_getter(namespace, name, scope)
15
- #register_field_setter(namespace, name, scope)
16
- #register_field_presence(namespace, name, scope)
17
- #register_field_change(namespace, name, scope)
18
- #register_field_changed(namespace, name, scope)
19
- #register_field_was(namespace, name, scope)
20
- #register_field_reset(namespace, name, scope)
21
- #super
22
- end
23
- end
24
- end
25
- end
26
- end