Permalink
Browse files

rdoc fixes

  • Loading branch information...
1 parent c9addec commit f62f6f2f6cc905875a8bdb65e75e62ba9831fc3d @mynameisrufus committed Feb 28, 2011
Showing with 31 additions and 29 deletions.
  1. +7 −13 README.rdoc
  2. +10 −10 lib/theman/agency.rb
  3. +5 −5 lib/theman/agency/columns.rb
  4. +8 −0 lib/theman/object.rb
  5. +1 −1 lib/theman/version.rb
View
@@ -20,31 +20,27 @@ Say we have a csv file called <tt>sample.csv</tt> with 220 rows:
res = conn.exec("SELECT count(*) FROM #{agent.table_name}")
res.getvalue(0,0)
- => 220
-
== Basic usage with Active Record and a simple object
conn = ActiveRecord::Base.connection.raw_connection
agent = Theman::Agency.new(conn, 'sample.csv')
agent.create!
- model = Theman::Object(agent.table_name, ActiveRecord::Base)
+ model = Theman::Object.new(agent.table_name, ActiveRecord::Base)
model.count
- => 220
-
== Advanced usage with Active Record and an existing model
Theman will call the <tt>create!</tt> method if you pass in a block.
conn = ActiveRecord::Base.connection.raw_connection
- agent = Theman::Agency.new conn, 'ugly.csv' |ag|
- ag.nulls /"N"/, /"UNKNOWN"/, /""/
- ag.seds "-n -e :a -e '1,15!{P;N;D;};N;ba'"
- ag.delimiter "|"
- ag.table do |t|
+ agent = Theman::Agency.new conn, 'ugly.csv' do |smith|
+ smith.nulls /"N"/, /"UNKNOWN"/, /""/
+ smith.seds "-n -e :a -e '1,15!{P;N;D;};N;ba'"
+ smith.delimiter "|"
+ smith.table do |t|
t.string :name, :limit => 50
t.date :date
t.integer :ext_id
@@ -56,16 +52,14 @@ Theman will call the <tt>create!</tt> method if you pass in a block.
MyModel.table_name = agent.table_name
MyModel.where(:exited => true).count
- => 220
-
In the above example we omitted the last 15 rows, made some things null and
specified some column data types.
If you do not provide a table block your columns will be VARCHAR(255); you
can cherry pick the columns that you want to change the data types for.
The temp table has no id column by default, but you can add one by calling
-+add_primary_key!+, this will add the +agents_pkey+ column.
+<tt>add_primary_key!</tt>, this will add the <tt>agents_pkey</tt> column.
== Drop on commit
View
@@ -11,7 +11,7 @@ class Agency
# * +options+ - Additional options are <tt>:temporary</tt>,
# <tt>:on_commit</tt> and <tt>:headers</tt>
#
- # ==== Examples
+ # ==== Example
# # Update all customers with the given attributes
# conn = PGconn.open(:dbname => 'test')
# agent = Theman::Agency.new(conn, 'sample.csv')
@@ -40,14 +40,14 @@ def transaction(&block)
connection.exec "COMMIT;"
end
- def create_stream_columns #:nodoc
+ def create_stream_columns #:nodoc:
@stream_columns_set = true
headers.split(delimiter_regexp).each do |column|
@columns.string column
end
end
- def headers #:nodoc
+ def headers #:nodoc:
File.open(@stream, "r"){ |infile| infile.gets }
end
@@ -83,33 +83,33 @@ def delimiter(arg)
@delimiter = arg
end
- def psql_copy(psql = []) #:nodoc
+ def psql_copy(psql = []) #:nodoc:
psql << "COPY #{table_name} FROM STDIN WITH"
psql << "DELIMITER '#{@delimiter}'" unless @delimiter.nil?
psql << "CSV"
psql << "HEADER" unless @options[:headers] == false
psql
end
- def psql_command(psql = []) #:nodoc
+ def psql_command(psql = []) #:nodoc:
psql << "SET DATESTYLE TO #{@datestyle}" unless @datestyle.nil?
psql << psql_copy.join(" ")
psql
end
- def sed_command(sed = []) #:nodoc
+ def sed_command(sed = []) #:nodoc:
sed << nulls_to_sed unless @nulls.nil?
sed << @seds unless @seds.nil?
sed
end
- def nulls_to_sed #:nodoc
+ def nulls_to_sed #:nodoc:
@nulls.map do |regex|
"-e 's/#{regex.source}//g'"
end
end
- def delimiter_regexp #:nodoc
+ def delimiter_regexp #:nodoc:
@delimiter_regexp ||= Regexp.new(@delimiter.nil? ? "," : "\\#{@delimiter}")
end
@@ -143,15 +143,15 @@ def drop!
@table_name = nil
end
- def system_command #:nodoc
+ def system_command #:nodoc:
unless sed_command.empty?
"cat #{@stream} | sed #{sed_command.join(" | sed ")}"
else
"cat #{@stream}"
end
end
- def pipe_it(l = "") #:nodoc
+ def pipe_it(l = "") #:nodoc:
connection.exec psql_command.join("; ")
f = IO.popen(system_command)
begin
@@ -9,7 +9,7 @@ def initialize(conn)
@columns = []
end
- def to_sql #:nodoc
+ def to_sql #:nodoc:
@columns.map{|column| column_to_sql(*column)}.join(', ')
end
@@ -21,11 +21,11 @@ def #{type}(name, *args)
EOV
end
- def symbolize(name) #:nodoc
+ def symbolize(name) #:nodoc:
name.is_a?(Symbol) ? name : name.gsub(/ /,"_").gsub(/\W/, "").downcase.to_sym
end
- def column(name, type, *args) #:nodoc
+ def column(name, type, *args) #:nodoc:
sym_col = symbolize(name)
@columns.each_with_index do |column, index|
if column[0] == sym_col
@@ -40,7 +40,7 @@ def include?(sym_col)
@columns.map{|column| column[0] }.include?(sym_col)
end
- def column_to_sql(name, type, options = {}) #:nodoc
+ def column_to_sql(name, type, options = {}) #:nodoc:
sql = [quote_column_name(name)]
case type
when 'integer'
@@ -91,7 +91,7 @@ def column_to_sql(name, type, options = {}) #:nodoc
sql.join(' ')
end
- def quote_column_name(name) #:nodoc
+ def quote_column_name(name) #:nodoc:
@connection.quote_ident(name.to_s)
end
end
View
@@ -1,5 +1,13 @@
module Theman
class Object
+ # create a new basic model object
+ # ==== Parameters
+ # * +table_name+ - the name of the table created by Theman::Agency
+ # * +parent+ - optional parent object for the new basic model object
+ # usually ActiveRecord::Base
+ # * +conn+ - optional pg connection
+ # ==== Example
+ # my_model = Theman::Object.new(agent.table_name, ActiveRecord::Base)
def self.new(table_name, parent = ::Object, conn = nil)
Class.new(parent) do
unless conn.nil?
View
@@ -1,3 +1,3 @@
module Theman
- VERSION = "0.1.1"
+ VERSION = "0.1.2"
end

0 comments on commit f62f6f2

Please sign in to comment.