adacosta-labilerecord 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2009-04-14
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/labilerecord.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ test/test_helper.rb
11
+ test/test_labilerecord.rb
@@ -0,0 +1,2 @@
1
+
2
+ For more information on labilerecord, see http://github.com/adacosta/labilerecord
@@ -0,0 +1,64 @@
1
+ = labilerecord
2
+
3
+ * git://github.com/adacosta/labilerecord.git
4
+
5
+ == DESCRIPTION:
6
+
7
+ * Simple data access for dynamic data sets through postgres with ruby
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Access data by row[x].column
12
+ * Access field types
13
+ * Access types which ActiveRecord won't (e.g. _int4)
14
+ * Easy to use on tables, views, and functions
15
+
16
+ == SYNOPSIS:
17
+
18
+ require 'rubygems'
19
+ require 'labilerecord'
20
+
21
+ LabileRecord::Base.connection = {:dbname => 'postgres', :user => user,
22
+ :pass => pass, :host => host, :port => port}
23
+
24
+ q = LabileRecord::Query.new("SELECT * FROM pg_database")
25
+ # execute the query
26
+ q.exec
27
+ # inspect data
28
+ q.data.inspect
29
+ #inspect fields (columns)
30
+ q.fields.inspect
31
+
32
+ == REQUIREMENTS:
33
+
34
+ * pg gem (gem install pg)
35
+
36
+ == INSTALL:
37
+
38
+ * gem sources -a http://gems.github.com
39
+ * sudo gem install adacosta-labilerecord
40
+
41
+ == LICENSE:
42
+
43
+ (The MIT License)
44
+
45
+ Copyright (c) 2009 Alan Da Costa
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining
48
+ a copy of this software and associated documentation files (the
49
+ 'Software'), to deal in the Software without restriction, including
50
+ without limitation the rights to use, copy, modify, merge, publish,
51
+ distribute, sublicense, and/or sell copies of the Software, and to
52
+ permit persons to whom the Software is furnished to do so, subject to
53
+ the following conditions:
54
+
55
+ The above copyright notice and this permission notice shall be
56
+ included in all copies or substantial portions of the Software.
57
+
58
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
59
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
60
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
61
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
62
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
63
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
64
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/labilerecord'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('labilerecord', LabileRecord::VERSION) do |p|
7
+ p.developer('Alan Da Costa', 'alandacosta@gmail.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ # p.extra_deps = [
12
+ # ['activesupport','>= 2.0.2'],
13
+ # ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
@@ -0,0 +1,94 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module LabileRecord
5
+ VERSION = '0.0.1'
6
+ require 'pg'
7
+
8
+ class Base
9
+ class << self
10
+ def connection(*args)
11
+ @connection
12
+ end
13
+
14
+ def connection=(*args)
15
+ @connection = PGconn.open(*args)
16
+ end
17
+ end
18
+ end
19
+
20
+ class Query < LabileRecord::Base
21
+ attr_reader :data
22
+ attr_reader :result
23
+ attr_reader :fields
24
+ attr_reader :string
25
+
26
+ def initialize(query_string)
27
+ @string = query_string
28
+ end
29
+
30
+ def exec
31
+ @result = connection.exec(@string)
32
+ parse_fields
33
+ parse_result_data
34
+ end
35
+
36
+ def parse_result_data
37
+ columns = @result.fields
38
+ row_count = @result.num_tuples
39
+ field_names = @fields.map {|field| field.name}
40
+ rows = []
41
+ # iterate rows
42
+ (0..(row_count-1)).each do |row_index|
43
+ row = Row.new(field_names)
44
+ columns.each do |column_name|
45
+ row << @result[row_index][column_name]
46
+ end
47
+ rows << row
48
+ end
49
+ @data = rows
50
+ end
51
+
52
+ def parse_fields
53
+ @fields = @field_names = []
54
+ @result.fields.each_with_index do |field_name, i|
55
+ pg_field_type_id = @result.ftype(i)
56
+ type = connection.exec("SELECT typname FROM
57
+ pg_type WHERE oid = #{pg_field_type_id}")
58
+ field_type_name = type[0][type.fields[0]].to_s
59
+ @fields << Field.new( field_name, field_type_name, pg_field_type_id)
60
+ end
61
+ @fields
62
+ end
63
+
64
+ def connection
65
+ self.class.superclass.connection
66
+ end
67
+ end
68
+
69
+ class Field
70
+ attr_reader :name
71
+ attr_reader :type
72
+ attr_reader :type_id
73
+
74
+ def initialize(name, type, type_id)
75
+ @name = name
76
+ @type = type
77
+ @type_id = type
78
+ end
79
+ end
80
+
81
+ class Row < Array
82
+ def initialize(fields)
83
+ @fields = fields
84
+ end
85
+
86
+ def method_missing(meth,*args)
87
+ if ( field_index = @fields.index(meth.id2name) )
88
+ at field_index
89
+ else
90
+ super
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/labilerecord.rb'}"
9
+ puts "Loading labilerecord gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/labilerecord'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestLabilerecord < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adacosta-labilerecord
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Alan Da Costa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.3.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: pg
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.8.0
44
+ version:
45
+ description: "* Simple data access for dynamic data sets through postgres with ruby"
46
+ email:
47
+ - alandacosta@gmail.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - History.txt
54
+ - Manifest.txt
55
+ - PostInstall.txt
56
+ - README.rdoc
57
+ files:
58
+ - History.txt
59
+ - Manifest.txt
60
+ - PostInstall.txt
61
+ - README.rdoc
62
+ - Rakefile
63
+ - lib/labilerecord.rb
64
+ - script/console
65
+ - script/destroy
66
+ - script/generate
67
+ - test/test_helper.rb
68
+ - test/test_labilerecord.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/adacosta/labilerecord
71
+ post_install_message: PostInstall.txt
72
+ rdoc_options:
73
+ - --main
74
+ - README.rdoc
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project: labilerecord
92
+ rubygems_version: 1.2.0
93
+ signing_key:
94
+ specification_version: 2
95
+ summary: "* Simple data access for dynamic data sets through postgres with ruby"
96
+ test_files:
97
+ - test/test_helper.rb
98
+ - test/test_labilerecord.rb