Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
annotate
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
annotate
Commits
280a9e56
Commit
280a9e56
authored
Oct 15, 2010
by
miyucy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add format option
see
http://github.com/branch14/annotate_models/commit/e51c3418ca67fc4e833d31db52f440f8d0e0d3b5
parent
8b50c96b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
5 deletions
+43
-5
annotate
bin/annotate
+4
-0
annotate_models.rb
lib/annotate/annotate_models.rb
+19
-5
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+20
-0
No files found.
bin/annotate
View file @
280a9e56
...
...
@@ -61,6 +61,10 @@ OptionParser.new do |opts|
exclusions
.
each
{
|
exclusion
|
ENV
[
"exclude_
#{
exclusion
}
"
]
=
"yes"
}
end
opts
.
on
(
'-f'
,
'--format [bare|rdoc]'
,
[
'bare'
,
'rdoc'
],
'rdoc: render Schema Infomation as RDoc'
)
do
|
fmt
|
ENV
[
'format_#{fmt}'
]
=
'yes'
end
end
.
parse!
if
Annotate
.
load_tasks
...
...
lib/annotate/annotate_models.rb
View file @
280a9e56
...
...
@@ -2,6 +2,8 @@ module AnnotateModels
# Annotate Models plugin use this header
COMPAT_PREFIX
=
"== Schema Info"
PREFIX
=
"== Schema Information"
END_MARK
=
"== Schema Information End"
PATTERN
=
/^\n?#
#{
COMPAT_PREFIX
}
.*?\n(#.*\n)*\n/
# File.join for windows reverse bar compat?
# I dont use windows, can`t test
...
...
@@ -51,10 +53,12 @@ module AnnotateModels
# each column. The line contains the column name,
# the type (and length), and any optional attributes
def
get_schema_info
(
klass
,
header
,
options
=
{})
info
=
"#
#{
header
}
\n
#
\n
"
info
<<
"# Table name:
#{
klass
.
table_name
}
\n
#
\n
"
info
=
"#
#{
header
}
\n
"
info
<<
"#
\n
"
info
<<
"# Table name:
#{
klass
.
table_name
}
\n
"
info
<<
"#
\n
"
max_size
=
klass
.
column_names
.
collect
{
|
name
|
name
.
size
}.
max
+
1
max_size
=
klass
.
column_names
.
map
{
|
name
|
name
.
size
}.
max
+
(
ENV
[
'format_rdoc'
]
?
5
:
1
)
klass
.
columns
.
each
do
|
col
|
attrs
=
[]
attrs
<<
"default(
#{
quote
(
col
.
default
)
}
)"
unless
col
.
default
.
nil?
...
...
@@ -88,15 +92,25 @@ module AnnotateModels
end
end
if
ENV
[
'format_rdoc'
]
info
<<
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s<tt>%s</tt>"
,
"*
#{
col
.
name
}
*::"
,
attrs
.
unshift
(
col_type
).
join
(
", "
)).
rstrip
+
"
\n
"
else
info
<<
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s:%-15.15s %s"
,
col
.
name
,
col_type
,
attrs
.
join
(
", "
)).
rstrip
+
"
\n
"
end
end
if
options
[
:show_indexes
]
info
<<
get_index_info
(
klass
)
end
if
ENV
[
'format_rdoc'
]
info
<<
"#--
\n
"
info
<<
"#
#{
END_MARK
}
\n
"
info
<<
"#++
\n\n
"
else
info
<<
"#
\n\n
"
end
end
def
get_index_info
(
klass
)
index_info
=
"#
\n
# Indexes
\n
#
\n
"
...
...
@@ -138,7 +152,7 @@ module AnnotateModels
false
else
# Remove old schema info
old_content
.
sub!
(
/^\n?#
#{
COMPAT_PREFIX
}
.*?\n(#.*\n)*\n/
,
''
)
old_content
.
sub!
(
PATTERN
,
''
)
# Write it back
new_content
=
options
[
:position
].
to_s
==
'after'
?
(
old_content
+
"
\n
"
+
info_block
)
:
(
info_block
+
old_content
)
...
...
@@ -153,7 +167,7 @@ module AnnotateModels
if
File
.
exist?
(
file_name
)
content
=
File
.
read
(
file_name
)
content
.
sub!
(
/^\n?#
#{
COMPAT_PREFIX
}
.*?\n(#.*\n)*\n/
,
''
)
content
.
sub!
(
PATTERN
,
''
)
File
.
open
(
file_name
,
"wb"
)
{
|
f
|
f
.
puts
content
}
end
...
...
spec/annotate/annotate_models_spec.rb
View file @
280a9e56
...
...
@@ -59,6 +59,26 @@ describe AnnotateModels do
EOS
end
it
"should get schema info as RDoc"
do
klass
=
mock_class
(
:users
,
:id
,
[
mock_column
(
:id
,
:integer
),
mock_column
(
:name
,
:string
,
:limit
=>
50
)
])
ENV
.
stub!
(
:[]
).
with
(
'format_rdoc'
).
and_return
(
true
)
AnnotateModels
.
get_schema_info
(
klass
,
AnnotateModels
::
PREFIX
).
should
eql
(
<<-
EOS
)
#
#{
AnnotateModels
::
PREFIX
}
#
# Table name: users
#
# *id*:: <tt>integer, not null, primary key</tt>
# *name*:: <tt>string(50), not null</tt>
#--
#
#{
AnnotateModels
::
END_MARK
}
#++
EOS
end
describe
"#get_model_class"
do
def
create
(
file
,
body
=
"hi"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment