Skip to content

Commit

Permalink
rdoc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mynameisrufus committed Feb 28, 2011
1 parent c9addec commit f62f6f2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
20 changes: 7 additions & 13 deletions README.rdoc
Expand Up @@ -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 = conn.exec("SELECT count(*) FROM #{agent.table_name}")
res.getvalue(0,0) res.getvalue(0,0)


=> 220

== Basic usage with Active Record and a simple object == Basic usage with Active Record and a simple object


conn = ActiveRecord::Base.connection.raw_connection conn = ActiveRecord::Base.connection.raw_connection


agent = Theman::Agency.new(conn, 'sample.csv') agent = Theman::Agency.new(conn, 'sample.csv')
agent.create! agent.create!


model = Theman::Object(agent.table_name, ActiveRecord::Base) model = Theman::Object.new(agent.table_name, ActiveRecord::Base)
model.count model.count


=> 220

== Advanced usage with Active Record and an existing model == Advanced usage with Active Record and an existing model


Theman will call the <tt>create!</tt> method if you pass in a block. Theman will call the <tt>create!</tt> method if you pass in a block.


conn = ActiveRecord::Base.connection.raw_connection conn = ActiveRecord::Base.connection.raw_connection


agent = Theman::Agency.new conn, 'ugly.csv' |ag| agent = Theman::Agency.new conn, 'ugly.csv' do |smith|
ag.nulls /"N"/, /"UNKNOWN"/, /""/ smith.nulls /"N"/, /"UNKNOWN"/, /""/
ag.seds "-n -e :a -e '1,15!{P;N;D;};N;ba'" smith.seds "-n -e :a -e '1,15!{P;N;D;};N;ba'"
ag.delimiter "|" smith.delimiter "|"
ag.table do |t| smith.table do |t|
t.string :name, :limit => 50 t.string :name, :limit => 50
t.date :date t.date :date
t.integer :ext_id t.integer :ext_id
Expand All @@ -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.table_name = agent.table_name
MyModel.where(:exited => true).count MyModel.where(:exited => true).count


=> 220

In the above example we omitted the last 15 rows, made some things null and In the above example we omitted the last 15 rows, made some things null and
specified some column data types. specified some column data types.


If you do not provide a table block your columns will be VARCHAR(255); you 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. 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 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 == Drop on commit


Expand Down
20 changes: 10 additions & 10 deletions lib/theman/agency.rb
Expand Up @@ -11,7 +11,7 @@ class Agency
# * +options+ - Additional options are <tt>:temporary</tt>, # * +options+ - Additional options are <tt>:temporary</tt>,
# <tt>:on_commit</tt> and <tt>:headers</tt> # <tt>:on_commit</tt> and <tt>:headers</tt>
# #
# ==== Examples # ==== Example
# # Update all customers with the given attributes # # Update all customers with the given attributes
# conn = PGconn.open(:dbname => 'test') # conn = PGconn.open(:dbname => 'test')
# agent = Theman::Agency.new(conn, 'sample.csv') # agent = Theman::Agency.new(conn, 'sample.csv')
Expand Down Expand Up @@ -40,14 +40,14 @@ def transaction(&block)
connection.exec "COMMIT;" connection.exec "COMMIT;"
end end


def create_stream_columns #:nodoc def create_stream_columns #:nodoc:
@stream_columns_set = true @stream_columns_set = true
headers.split(delimiter_regexp).each do |column| headers.split(delimiter_regexp).each do |column|
@columns.string column @columns.string column
end end
end end


def headers #:nodoc def headers #:nodoc:
File.open(@stream, "r"){ |infile| infile.gets } File.open(@stream, "r"){ |infile| infile.gets }
end end


Expand Down Expand Up @@ -83,33 +83,33 @@ def delimiter(arg)
@delimiter = arg @delimiter = arg
end end


def psql_copy(psql = []) #:nodoc def psql_copy(psql = []) #:nodoc:
psql << "COPY #{table_name} FROM STDIN WITH" psql << "COPY #{table_name} FROM STDIN WITH"
psql << "DELIMITER '#{@delimiter}'" unless @delimiter.nil? psql << "DELIMITER '#{@delimiter}'" unless @delimiter.nil?
psql << "CSV" psql << "CSV"
psql << "HEADER" unless @options[:headers] == false psql << "HEADER" unless @options[:headers] == false
psql psql
end end


def psql_command(psql = []) #:nodoc def psql_command(psql = []) #:nodoc:
psql << "SET DATESTYLE TO #{@datestyle}" unless @datestyle.nil? psql << "SET DATESTYLE TO #{@datestyle}" unless @datestyle.nil?
psql << psql_copy.join(" ") psql << psql_copy.join(" ")
psql psql
end end


def sed_command(sed = []) #:nodoc def sed_command(sed = []) #:nodoc:
sed << nulls_to_sed unless @nulls.nil? sed << nulls_to_sed unless @nulls.nil?
sed << @seds unless @seds.nil? sed << @seds unless @seds.nil?
sed sed
end end


def nulls_to_sed #:nodoc def nulls_to_sed #:nodoc:
@nulls.map do |regex| @nulls.map do |regex|
"-e 's/#{regex.source}//g'" "-e 's/#{regex.source}//g'"
end end
end end


def delimiter_regexp #:nodoc def delimiter_regexp #:nodoc:
@delimiter_regexp ||= Regexp.new(@delimiter.nil? ? "," : "\\#{@delimiter}") @delimiter_regexp ||= Regexp.new(@delimiter.nil? ? "," : "\\#{@delimiter}")
end end


Expand Down Expand Up @@ -143,15 +143,15 @@ def drop!
@table_name = nil @table_name = nil
end end


def system_command #:nodoc def system_command #:nodoc:
unless sed_command.empty? unless sed_command.empty?
"cat #{@stream} | sed #{sed_command.join(" | sed ")}" "cat #{@stream} | sed #{sed_command.join(" | sed ")}"
else else
"cat #{@stream}" "cat #{@stream}"
end end
end end


def pipe_it(l = "") #:nodoc def pipe_it(l = "") #:nodoc:
connection.exec psql_command.join("; ") connection.exec psql_command.join("; ")
f = IO.popen(system_command) f = IO.popen(system_command)
begin begin
Expand Down
10 changes: 5 additions & 5 deletions lib/theman/agency/columns.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(conn)
@columns = [] @columns = []
end end


def to_sql #:nodoc def to_sql #:nodoc:
@columns.map{|column| column_to_sql(*column)}.join(', ') @columns.map{|column| column_to_sql(*column)}.join(', ')
end end


Expand All @@ -21,11 +21,11 @@ def #{type}(name, *args)
EOV EOV
end end


def symbolize(name) #:nodoc def symbolize(name) #:nodoc:
name.is_a?(Symbol) ? name : name.gsub(/ /,"_").gsub(/\W/, "").downcase.to_sym name.is_a?(Symbol) ? name : name.gsub(/ /,"_").gsub(/\W/, "").downcase.to_sym
end end


def column(name, type, *args) #:nodoc def column(name, type, *args) #:nodoc:
sym_col = symbolize(name) sym_col = symbolize(name)
@columns.each_with_index do |column, index| @columns.each_with_index do |column, index|
if column[0] == sym_col if column[0] == sym_col
Expand All @@ -40,7 +40,7 @@ def include?(sym_col)
@columns.map{|column| column[0] }.include?(sym_col) @columns.map{|column| column[0] }.include?(sym_col)
end end


def column_to_sql(name, type, options = {}) #:nodoc def column_to_sql(name, type, options = {}) #:nodoc:
sql = [quote_column_name(name)] sql = [quote_column_name(name)]
case type case type
when 'integer' when 'integer'
Expand Down Expand Up @@ -91,7 +91,7 @@ def column_to_sql(name, type, options = {}) #:nodoc
sql.join(' ') sql.join(' ')
end end


def quote_column_name(name) #:nodoc def quote_column_name(name) #:nodoc:
@connection.quote_ident(name.to_s) @connection.quote_ident(name.to_s)
end end
end end
Expand Down
8 changes: 8 additions & 0 deletions lib/theman/object.rb
@@ -1,5 +1,13 @@
module Theman module Theman
class Object 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) def self.new(table_name, parent = ::Object, conn = nil)
Class.new(parent) do Class.new(parent) do
unless conn.nil? unless conn.nil?
Expand Down
2 changes: 1 addition & 1 deletion lib/theman/version.rb
@@ -1,3 +1,3 @@
module Theman module Theman
VERSION = "0.1.1" VERSION = "0.1.2"
end end

0 comments on commit f62f6f2

Please sign in to comment.