| 278 | | |
|---|
| 279 | | end |
|---|
| 280 | | end |
|---|
| | 291 | README = <<END_README |
|---|
| | 292 | |
|---|
| | 293 | == rope : A Code Generation Tool for Ruby Reports == |
|---|
| | 294 | |
|---|
| | 295 | # Overview |
|---|
| | 296 | |
|---|
| | 297 | Rope provides you with a number of simple utilities that script away |
|---|
| | 298 | much of your boilerplate code, and also provide useful tools for |
|---|
| | 299 | development, such as automatic test running and a way to check your |
|---|
| | 300 | SQL queries for validity. Additionally, you'll get logging for free |
|---|
| | 301 | and you can share a common configuration file between applications |
|---|
| | 302 | in the same project. |
|---|
| | 303 | |
|---|
| | 304 | Though each tool on it's own isn't complicated, having them all |
|---|
| | 305 | working together can be a major win. |
|---|
| | 306 | |
|---|
| | 307 | # The Basics |
|---|
| | 308 | |
|---|
| | 309 | -- Starting a new rope project |
|---|
| | 310 | |
|---|
| | 311 | $ rope labyrith |
|---|
| | 312 | creating directories.. |
|---|
| | 313 | labyrith/test |
|---|
| | 314 | labyrith/config |
|---|
| | 315 | labyrith/output |
|---|
| | 316 | labyrith/data |
|---|
| | 317 | labyrith/data/models |
|---|
| | 318 | labyrith/lib |
|---|
| | 319 | labyrith/lib/reports |
|---|
| | 320 | labyrith/lib/renderers |
|---|
| | 321 | labyrith/templates |
|---|
| | 322 | labyrith/sql |
|---|
| | 323 | labyrith/log |
|---|
| | 324 | labyrith/util |
|---|
| | 325 | creating files.. |
|---|
| | 326 | labyrith/lib/reports.rb |
|---|
| | 327 | labyrith/lib/helpers.rb |
|---|
| | 328 | labyrith/lib/renderers.rb |
|---|
| | 329 | labyrith/lib/init.rb |
|---|
| | 330 | labyrith/config/environment.rb |
|---|
| | 331 | labyrith/util/build |
|---|
| | 332 | labyrith/util/sql_exec |
|---|
| | 333 | labyrith/Rakefile |
|---|
| | 334 | labyrith/README |
|---|
| | 335 | |
|---|
| | 336 | Successfully generated project: labyrith |
|---|
| | 337 | |
|---|
| | 338 | Once this is complete, you'll have a large number of mostly empty |
|---|
| | 339 | folders laying around, along with some helpful tools at your disposal. |
|---|
| | 340 | |
|---|
| | 341 | -- utilities |
|---|
| | 342 | |
|---|
| | 343 | * build : A tool for generating reports and formatting system extensions |
|---|
| | 344 | * sql_exec: A simple tool for getting a result set from a SQL file |
|---|
| | 345 | (possibly with ERb) |
|---|
| | 346 | * Rakefile: Script for project automation tasks. |
|---|
| | 347 | |
|---|
| | 348 | -- directories |
|---|
| | 349 | |
|---|
| | 350 | * test : unit tests stored here can be auto-run |
|---|
| | 351 | * config : holds a configuration file which is shared across your applications |
|---|
| | 352 | * reports : when reports are autogenerated, they are stored here |
|---|
| | 353 | * renderers : autogenerated formatting system extensions are stored here |
|---|
| | 354 | * models : stores autogenerated activerecord models |
|---|
| | 355 | * templates : erb templates may be stored here |
|---|
| | 356 | * sql : SQL files can be stored here, which are pre-processed by erb |
|---|
| | 357 | * log : The logger will automatically store your logfiles here by default |
|---|
| | 358 | * util : contains rope related tools |
|---|
| | 359 | |
|---|
| | 360 | # Generating a Report definition with rope |
|---|
| | 361 | |
|---|
| | 362 | $ ./util/build report ghosts |
|---|
| | 363 | report file: lib/reports/ghosts.rb |
|---|
| | 364 | test file: test/test_ghosts.rb |
|---|
| | 365 | class name: Ghosts |
|---|
| | 366 | |
|---|
| | 367 | $ rake |
|---|
| | 368 | (in /home/sandal/labyrinth) |
|---|
| | 369 | /usr/bin/ruby -Ilib:test |
|---|
| | 370 | "/usr/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb" |
|---|
| | 371 | "test/test_ghosts.rb" |
|---|
| | 372 | Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader |
|---|
| | 373 | Started |
|---|
| | 374 | F |
|---|
| | 375 | Finished in 0.001119 seconds. |
|---|
| | 376 | |
|---|
| | 377 | 1) Failure: |
|---|
| | 378 | test_flunk(TestGhosts) [./test/test_ghosts.rb:6]: |
|---|
| | 379 | Write your real tests here or in any test/test_* file. |
|---|
| | 380 | |
|---|
| | 381 | 1 tests, 1 assertions, 1 failures, 0 errors |
|---|
| | 382 | rake aborted! |
|---|
| | 383 | Command failed with status (1): [/usr/bin/ruby -Ilib:test |
|---|
| | 384 | "/usr/lib/ruby/ge...] |
|---|
| | 385 | |
|---|
| | 386 | (See full trace by running task with --trace) |
|---|
| | 387 | |
|---|
| | 388 | You can now edit lib/reports/ghosts.rb as needed and write tests for |
|---|
| | 389 | it in test/test_ghosts.rb without having to hook up any underplumbing. |
|---|
| | 390 | |
|---|
| | 391 | # Rope's Auto-generated Configuration File |
|---|
| | 392 | |
|---|
| | 393 | -- Basic Details |
|---|
| | 394 | |
|---|
| | 395 | roped projects will automatically make use of the configuration details in |
|---|
| | 396 | config/environment.rb , which can be used to set up database |
|---|
| | 397 | connections, Ruport's mailer, and other project information. |
|---|
| | 398 | |
|---|
| | 399 | The default file is shown below. |
|---|
| | 400 | |
|---|
| | 401 | require "ruport" |
|---|
| | 402 | |
|---|
| | 403 | # Uncomment and modify the lines below if you want to use query.rb |
|---|
| | 404 | # |
|---|
| | 405 | # Ruport::Query.add_source :default, :user => "root", |
|---|
| | 406 | # :dsn => "dbi:mysql:mydb" |
|---|
| | 407 | |
|---|
| | 408 | # Uncomment and modify the lines below if you want to use AAR |
|---|
| | 409 | # |
|---|
| | 410 | # require "active_record" |
|---|
| | 411 | # require "ruport/acts_as_reportable" |
|---|
| | 412 | # ActiveRecord::Base.establish_connection( |
|---|
| | 413 | # :adapter => 'mysql', |
|---|
| | 414 | # :host => 'localhost', |
|---|
| | 415 | # :username => 'name', |
|---|
| | 416 | # :password => 'password', |
|---|
| | 417 | # :database => 'mydb' |
|---|
| | 418 | # ) |
|---|
| | 419 | |
|---|
| | 420 | You'll need to tweak this as needed to fit your database configuration needs. |
|---|
| | 421 | If you need to require any third party libraries which are shared across your |
|---|
| | 422 | project, you should do it in this file. |
|---|
| | 423 | |
|---|
| | 424 | # Custom rendering with rope generators. |
|---|
| | 425 | |
|---|
| | 426 | -- By Example |
|---|
| | 427 | |
|---|
| | 428 | $ rope my_reverser |
|---|
| | 429 | $ cd my_reverser |
|---|
| | 430 | $ rake build renderer=reverser |
|---|
| | 431 | |
|---|
| | 432 | Edit test/test_reverser.rb to look like the code below: |
|---|
| | 433 | |
|---|
| | 434 | require "test/unit" |
|---|
| | 435 | require "lib/renderers/reverser" |
|---|
| | 436 | |
|---|
| | 437 | class TestReverser < Test::Unit::TestCase |
|---|
| | 438 | def test_reverser |
|---|
| | 439 | assert_equal "baz", Reverser.render_text("zab") |
|---|
| | 440 | end |
|---|
| | 441 | end |
|---|
| | 442 | |
|---|
| | 443 | Now edit lib/renderers/reverser.rb to look like this: |
|---|
| | 444 | |
|---|
| | 445 | require "lib/init" |
|---|
| | 446 | |
|---|
| | 447 | class Reverser < Ruport::Renderer |
|---|
| | 448 | stage :reverser |
|---|
| | 449 | end |
|---|
| | 450 | |
|---|
| | 451 | class ReverserFormatter < Ruport::Formatter |
|---|
| | 452 | |
|---|
| | 453 | renders :text, :for => Reverser |
|---|
| | 454 | |
|---|
| | 455 | def build_reverser |
|---|
| | 456 | output << data.reverse |
|---|
| | 457 | end |
|---|
| | 458 | |
|---|
| | 459 | end |
|---|
| | 460 | |
|---|
| | 461 | The tests should pass. You can now generate a quick report using this renderer |
|---|
| | 462 | |
|---|
| | 463 | $ rake build report=reversed_report |
|---|
| | 464 | |
|---|
| | 465 | Edit test/test_reversed_report.rb as such: |
|---|
| | 466 | |
|---|
| | 467 | require "test/unit" |
|---|
| | 468 | require "lib/reports/reversed_report" |
|---|
| | 469 | |
|---|
| | 470 | class TestReversedReport < Test::Unit::TestCase |
|---|
| | 471 | def test_reversed_report |
|---|
| | 472 | report = ReversedReport.new |
|---|
| | 473 | report.message = "hello" |
|---|
| | 474 | assert_equal "olleh", report.to_text |
|---|
| | 475 | end |
|---|
| | 476 | end |
|---|
| | 477 | |
|---|
| | 478 | edit lib/reports/reversed_report.rb as below and run the tests. |
|---|
| | 479 | |
|---|
| | 480 | require "lib/init" |
|---|
| | 481 | require "lib/renderers/reverser" |
|---|
| | 482 | class ReversedReport < Ruport::Report |
|---|
| | 483 | |
|---|
| | 484 | renders_with Reverser |
|---|
| | 485 | attr_accessor :message |
|---|
| | 486 | |
|---|
| | 487 | def generate |
|---|
| | 488 | message |
|---|
| | 489 | end |
|---|
| | 490 | |
|---|
| | 491 | end |
|---|
| | 492 | |
|---|
| | 493 | # ActiveRecord integration the lazy way. |
|---|
| | 494 | |
|---|
| | 495 | Ruport now has built in support for acts_as_reportable, which provides |
|---|
| | 496 | ActiveRecord integration with ruport. |
|---|
| | 497 | |
|---|
| | 498 | -- Setup details |
|---|
| | 499 | |
|---|
| | 500 | Edit the following code in config/environment.rb |
|---|
| | 501 | (change as needed to match your config information) |
|---|
| | 502 | |
|---|
| | 503 | ActiveRecord::Base.establish_connection( |
|---|
| | 504 | :adapter => 'mysql', |
|---|
| | 505 | :host => 'localhost', |
|---|
| | 506 | :username => 'name', |
|---|
| | 507 | :password => 'password', |
|---|
| | 508 | :database => 'mydb' |
|---|
| | 509 | ) |
|---|
| | 510 | |
|---|
| | 511 | -- Generating a model |
|---|
| | 512 | |
|---|
| | 513 | Here is an example of generating the model file: |
|---|
| | 514 | |
|---|
| | 515 | $ util/build model my_model |
|---|
| | 516 | model file: data/models/my_model.rb |
|---|
| | 517 | class name: MyModel |
|---|
| | 518 | |
|---|
| | 519 | This will create a barebones model that looks like this: |
|---|
| | 520 | |
|---|
| | 521 | class MyModel < ActiveRecord::Base |
|---|
| | 522 | |
|---|
| | 523 | acts_as_reportable |
|---|
| | 524 | |
|---|
| | 525 | end |
|---|
| | 526 | |
|---|
| | 527 | The data/models.rb file will require all generated models, |
|---|
| | 528 | but you can of course require specific models in your reports. |
|---|
| | 529 | |
|---|
| | 530 | This should be enought to get you started, but for more complex needs, |
|---|
| | 531 | check the appropriate acts_as_reportable / ActiveRecord documentation. |
|---|
| | 532 | |
|---|
| | 533 | # Getting Help / Reporting Problems |
|---|
| | 534 | |
|---|
| | 535 | rope is an officially supported utility for the Ruby Reports project, in |
|---|
| | 536 | the ruport-util package. If you run into problems or have feature requests, |
|---|
| | 537 | please contact us at http://list.rubyreports.org |
|---|
| | 538 | END_README |
|---|
| | 539 | |
|---|
| | 540 | end |
|---|
| | 541 | end |
|---|