ruport 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,93 @@
1
+ #!/usr/local/bin/ruby -w
2
+ # sql.rb:
3
+ # Generates queries from simple ruby syntax
4
+ #
5
+ # Created by Gregory Thomas Brown on 2005-08-02
6
+ # Copyright 2005 (Gregory Brown) All rights reserved.
7
+ #
8
+ # This product is free software, you may distribute it as such
9
+ # under your choice of the Ruby license or the GNU GPL
10
+ # See LICENSE for details
11
+ #
12
+ # Special thanks and acknowledgement go to James Edward Gray II
13
+ # for providing the original source code for this application
14
+ class SQL
15
+
16
+ # Generates an SQL object which can generate queries.
17
+ def initialize( statement, &init )
18
+ @statement = statement
19
+
20
+ @fields = Array.new
21
+ @tables = Array.new
22
+ @conditions = Array.new
23
+ @order_by = Array.new
24
+
25
+ instance_eval(&init) unless init.nil?
26
+ end
27
+
28
+ #Specifies the fields to select from
29
+ def columns( *fields )
30
+ fields.each { |field| @fields << "'#{field}'" }
31
+ end
32
+
33
+ #generates an FROM clause for the given tables
34
+ def from( *tables )
35
+ tables.each { |table| @tables << table }
36
+ end
37
+
38
+ # generates a conditional expression.
39
+ # condition :name, :LIKE, :jim -> "name LIKE 'jim'"
40
+ def condition( field, operator, match )
41
+ @conditions << "#{field} #{operator.to_s.upcase} '#{match}'"
42
+ end
43
+
44
+ # generates an ORDER BY clause for list +order+
45
+ def order( *order )
46
+ order.each { |table| @order_by << table }
47
+ end
48
+
49
+ # Converts SQL object into a query string
50
+ def to_s
51
+ sql = "#{@statement} "
52
+
53
+ sql << if @fields.empty?
54
+ "* "
55
+ else
56
+ @fields.join(", ") + " "
57
+ end
58
+
59
+ sql << "FROM #{@tables.join(', ')} "
60
+
61
+ unless @conditions.empty?
62
+ sql << "WHERE #{@conditions.join(' AND ')} "
63
+ end
64
+
65
+ unless @order_by.empty?
66
+ sql << "ORDER BY #{@order_by.join(', ')} "
67
+ end
68
+
69
+ sql.sub!(/ \Z/, "")
70
+ sql << ";"
71
+
72
+ sql
73
+ end
74
+ end
75
+
76
+ class String
77
+ def with( other_table, field = nil )
78
+ if field.nil?
79
+ "#{self} NATURAL JOIN #{other_table}"
80
+ else
81
+ "#{self} LEFT JOIN #{other_table} " +
82
+ "ON #{self}.#{field} = #{other_table}.#{field}"
83
+ end
84
+ end
85
+ end
86
+
87
+ class Symbol
88
+ def with( other_table, field = nil )
89
+ to_s.with(other_table, field)
90
+ end
91
+ end
92
+
93
+
@@ -0,0 +1 @@
1
+ # Do me!
@@ -0,0 +1,7 @@
1
+ require "ruport/db/report"
2
+ require "ruport/db/mailer"
3
+ require "ruport/db/sql"
4
+ require "ruport/db/mock_db"
5
+ require "ruport/db/mock_report"
6
+ require "ruport/db/data_set"
7
+ require "ruport/db/data_row"
@@ -0,0 +1,53 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require "test/unit"
4
+ require "ruportlib"
5
+
6
+ class TestDataRow < Test::Unit::TestCase
7
+ def setup
8
+ @rows = DataSet.new
9
+ @rows.fields = %w[ foo bar ]
10
+ @rows << [ 1 , 2 ]
11
+ @rows << [ 3 , 4 ]
12
+ @rows << [ 5 , 6 ]
13
+ @rows << { "foo" => 7, "bar" => 8 }
14
+ end
15
+
16
+ def test_first?
17
+ assert(@rows[0].first?)
18
+ @rows[1..-1].each do |row|
19
+ assert(!row.first?)
20
+ end
21
+ end
22
+
23
+ def test_middle?
24
+ assert(!@rows[0].middle?)
25
+ assert(!@rows[-1].middle?)
26
+ @rows[1..-2].each do |row|
27
+ assert(row.middle?)
28
+ end
29
+ end
30
+
31
+ def test_last?
32
+ @rows[0..-2].each do |row|
33
+ assert(!row.last?)
34
+ end
35
+ assert(@rows[-1].last?)
36
+ end
37
+
38
+ def test_oddity
39
+ @rows[0..-1].each_with_index do |row,index|
40
+ if (index % 2 == 0)
41
+ assert(row.even?)
42
+ else
43
+ assert(row.odd?)
44
+ end
45
+ end
46
+ end
47
+
48
+ def test_to_s
49
+ assert_equal("[1,2]",@rows[0].to_s)
50
+ end
51
+
52
+ end
53
+
@@ -0,0 +1,67 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require "test/unit"
4
+ require "ruportlib"
5
+
6
+ class TestDataSet < Test::Unit::TestCase
7
+ def setup
8
+ @data = DataSet.new
9
+ @data.fields = %w[ col1 col2 col3 ]
10
+ @data.default = ""
11
+ @data << %w[ a b c ]
12
+ @data << { "col1" => "d", "col3" => "e"}
13
+ end
14
+
15
+ def test_fields
16
+ assert_equal(%w[ col1 col2 col3 ], @data.fields )
17
+ end
18
+
19
+ def test_default
20
+ @data.default = "x"
21
+ @data << { }
22
+ assert_equal( ['x','x','x'],
23
+ @data[2].to_a )
24
+ end
25
+
26
+ def test_brackets
27
+ row0 = { "col1" => "a", "col2" => "b", "col3" => "c" }
28
+ row1 = { "col1" => "d", "col2" => "", "col3" => "e" }
29
+ row0.each do |key,value|
30
+ assert_equal( value, @data[0][key] )
31
+ end
32
+ row1.each do |key,value|
33
+ assert_equal( value, @data[1][key] )
34
+ end
35
+ end
36
+
37
+ def test_eql?
38
+ data2 = DataSet.new
39
+ data2.fields = %w[ col1 col2 col3 ]
40
+ data2.default = ""
41
+ data2 << %w[ a b c ]
42
+ data2 << { "col1" => "d", "col3" => "e" }
43
+
44
+ #FIXME: This looks like some shady discrete math assignment
45
+ assert(@data.eql?(data2) && data2.eql?(@data))
46
+ data2 << [2, 3, 4]
47
+ assert(!( @data.eql?(data2) || data2.eql?(@data) ))
48
+ @data << [2, 3, 4]
49
+ assert(@data.eql?(data2) && data2.eql?(@data))
50
+ @data << [8, 9, 10]
51
+ assert(!( @data.eql?(data2) || data2.eql?(@data) ))
52
+ data2 << [8, 9, 10]
53
+ assert(@data.eql?(data2) && data2.eql?(@data))
54
+ end
55
+
56
+ def test_load
57
+ loaded_data = DataSet.load("test/data.csv")
58
+ assert(@data.eql?(loaded_data))
59
+ end
60
+
61
+ def test_to_csv
62
+ loaded_data = DataSet.load("test/data.csv" )
63
+ csv = loaded_data.to_csv
64
+ assert_equal("col1,col2,col3\na,b,c\nd,\"\",e\n",csv)
65
+ end
66
+
67
+ end
@@ -0,0 +1,104 @@
1
+ #tc_ruport.rb
2
+ #
3
+ # Created by Gregory Thomas Brown on 2005-08-09
4
+ # Copyright 2005 (Gregory Brown) All rights reserved.
5
+
6
+ require "test/unit"
7
+ require "lib/ruportlib"
8
+ class TestRuport < Test::Unit::TestCase
9
+
10
+ def setup
11
+ @report = MockReport.new( "DBI:mysql:ruport:localhost", "test", "123")
12
+ @report.query_table = "ruport_queries"
13
+
14
+ @report.fake_db = MockDB.new([ "DBI:mysql","ruport",
15
+ "localhost", "test", "123"])
16
+
17
+ @report.fake_db["SELECT * FROM ruport_test"] = @data =
18
+ [ { "a" => "a column, row 1", "b" => "b column, row 1",
19
+ "c" => "c column, row 1", "d" => "d column, row 1" },
20
+ { "a" => "a column, row 2", "b" => "b column, row 2",
21
+ "c" => "c column, row 2", "d" => "d column, row 2" },
22
+ { "a" => "a column, row 3", "b" => "b column, row 3",
23
+ "c" => "c column, row 3", "d" => "d column, row 3" } ]
24
+
25
+ @report.fake_db[ "SELECT query FROM #{@report.query_table} " +
26
+ "WHERE label LIKE 'sql_stored_test';"] =
27
+ [ "query" => "SELECT * FROM ruport_test" ]
28
+ end
29
+
30
+ def test_load_file
31
+ contents = "SELECT * FROM ruport_test"
32
+
33
+ #relative to working dir
34
+ assert_equal(contents, @report.load_file("test/test.sql"));
35
+
36
+ #absolute path
37
+ assert_equal(contents,
38
+ @report.load_file(File.expand_path("test/test.sql")))
39
+ end
40
+
41
+ def test_sql_generation
42
+ sql = @report.fetch do
43
+ from :ruport_test
44
+ end
45
+ assert_equal("SELECT * FROM ruport_test;", sql)
46
+ sql = @report.fetch do
47
+ from :ruport_test
48
+ condition :last_name, :"=", "Brown"
49
+ order :first_name
50
+ end
51
+ assert_equal("SELECT * FROM ruport_test WHERE last_name = 'Brown' " +
52
+ "ORDER BY first_name;",sql)
53
+ end
54
+
55
+ def test_load_yaml
56
+ config_hash = { :name => "Greg",
57
+ :friend => "Rob",
58
+ :job => "Programmer" }
59
+ @report.load_yaml "test/test.yaml"
60
+ config_hash.each do |key, value|
61
+ assert_equal(value,@report.config[key])
62
+ end
63
+ end
64
+
65
+ def test_sql
66
+ row_i = 0
67
+ @report.select("* FROM ruport_test") do |row|
68
+ assert_equal(@data[row_i]["a"], row["a"].to_s)
69
+ assert_equal(@data[row_i]["b"], row["b"].to_s)
70
+ assert_equal(@data[row_i]["c"], row["c"].to_s)
71
+ assert_equal(@data[row_i]["d"], row["d"].to_s)
72
+ row_i += 1
73
+ end
74
+ end
75
+
76
+ def test_sql_file
77
+ row_i = 0
78
+ @report.select("test/test.sql",:file) do |row|
79
+ assert_equal(@data[row_i]["a"], row["a"].to_s)
80
+ assert_equal(@data[row_i]["b"], row["b"].to_s)
81
+ assert_equal(@data[row_i]["c"], row["c"].to_s)
82
+ assert_equal(@data[row_i]["d"], row["d"].to_s)
83
+ row_i += 1
84
+ end
85
+ end
86
+
87
+ def test_sql_stored
88
+ row_i = 0
89
+ @report.select("sql_stored_test",:db) do |row|
90
+ assert_equal(@data[row_i]["a"], row["a"].to_s)
91
+ assert_equal(@data[row_i]["b"], row["b"].to_s)
92
+ assert_equal(@data[row_i]["c"], row["c"].to_s)
93
+ assert_equal(@data[row_i]["d"], row["d"].to_s)
94
+ row_i += 1
95
+ end
96
+ end
97
+
98
+ def test_query
99
+ @report.query("SELECT * FROM ruport_test") do |result|
100
+ assert(@data,result)
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,4 @@
1
+ require "test/unit"
2
+ require "test/tc_report"
3
+ require "test/tc_data_set"
4
+ require "test/tc_data_row"
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: ruport
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2005-08-28
6
+ version: 0.2.0
7
+ date: 2005-11-08
8
8
  summary: A generalized Ruby report generation and templating engine.
9
9
  require_paths:
10
10
  - lib
@@ -27,16 +27,25 @@ platform: ruby
27
27
  authors:
28
28
  - Gregory Brown
29
29
  files:
30
- - bin/ruport.rb
31
- - lib/mailer.rb
32
- - lib/query.rb
33
- - lib/sql.rb
34
- - tests/tc_ruport.rb
30
+ - lib/ruportlib.rb
31
+ - lib/ruport/db/data_row.rb
32
+ - lib/ruport/db/data_set.rb
33
+ - lib/ruport/db/mailer.rb
34
+ - lib/ruport/db/mock_db.rb
35
+ - lib/ruport/db/mock_report.rb
36
+ - lib/ruport/db/report.rb
37
+ - lib/ruport/db/sql.rb
38
+ - lib/ruport/format/chart.rb
39
+ - test/tc_data_row.rb
40
+ - test/tc_data_set.rb
41
+ - test/tc_report.rb
42
+ - test/ts_all.rb
35
43
  - Rakefile
36
- - setup.rb
37
44
  - README
38
45
  - LICENSE
39
46
  - TODO
47
+ - AUTHORS
48
+ - CHANGELOG
40
49
  test_files: []
41
50
  rdoc_options:
42
51
  - "--title"
@@ -47,19 +56,10 @@ extra_rdoc_files:
47
56
  - README
48
57
  - LICENSE
49
58
  - TODO
59
+ - AUTHORS
60
+ - CHANGELOG
50
61
  executables:
51
62
  - ruport
52
- - ruport.rb
53
63
  extensions: []
54
64
  requirements: []
55
- dependencies:
56
- - !ruby/object:Gem::Dependency
57
- name: parseinput
58
- version_requirement:
59
- version_requirements: !ruby/object:Gem::Version::Requirement
60
- requirements:
61
- -
62
- - ">="
63
- - !ruby/object:Gem::Version
64
- version: 0.0.1
65
- version:
65
+ dependencies: []
@@ -1,4 +0,0 @@
1
- # THIS IS A NASTY HACK TO GET RUPORT TO WORK IN CMD.EXE
2
- # BETTER SOLUTION IS A MUST.
3
-
4
- eval(File.read("C:/ruby/bin/ruport"))
@@ -1,44 +0,0 @@
1
- # mailer.rb
2
-
3
- # Created by Gregory Brown on 2005-08-16
4
- # Copyright 2005 (Gregory Brown) All Rights Reserved.
5
-
6
- # This product is free software, you may distribute it as such
7
- # under your choice of the Ruby license or the GNU GPL
8
- # See LICENSE for details
9
-
10
- require "net/smtp"
11
- class Mailer
12
-
13
- # Creates a new Mailer object. User must specify their mail host, email
14
- # address, and account. Password, port, and authentication method are all
15
- # optional.
16
- def initialize(host, address, account, password=nil, port=25, authentication=nil)
17
- @host = host
18
- @account = account
19
- @password = password
20
- @address = address
21
- @port = port
22
- @auth = authentication
23
- @recipients = []
24
- @body = ""
25
- end
26
-
27
- #A list of email addresses to send the message to.
28
- attr_accessor :recipients
29
-
30
- #The body of the message to be sent
31
- attr_accessor :body
32
-
33
- # This takes _report_name_ as argument and sends the contents of @body to
34
- # @recipients
35
- def send_report(report_name)
36
- return if @body.empty?
37
- Net::SMTP.start(@host,@port,@host,@account,@password,@auth) do |smtp|
38
- smtp.send_message( "Subject: #{report_name}\n\n#{@body}",
39
- @address,
40
- @recipients )
41
- end
42
- end
43
-
44
- end