lib/arison/core.rb in arison-0.1.3 vs lib/arison/core.rb in arison-0.2.0
- old
+ new
@@ -32,10 +32,11 @@
def tables
@connection.tables
end
def import(table_name, data)
+ create_table(table_name, data)
klass = get_class(table_name)
limits = get_limit_hash(klass)
instances = []
data.each do |record|
instance = klass.new
@@ -86,37 +87,25 @@
}
end
def get_add_column_dsl(table_name, column_name, record)
dsl = %Q{
- add_column "#{table_name}", "#{column_name}", :#{get_type(column_name, record['column_name'])}
+ add_column "#{table_name}", "#{column_name}", :#{Util.get_type(column_name, record['column_name'])}
}
end
def create_table(table_name, data)
first = data.class == Array ? data.first : data
- if @connection.data_source_exists?(table_name)
+ if ActiveRecord::Base.connection.data_source_exists?(table_name.to_sym)
add_column_live(table_name, data)
return
end
create_table_dsl = get_create_table_dsl(table_name, first)
Arison::Migration.run_dsl(create_table_dsl)
add_column_live(table_name, data)
end
- def parse_json(buffer)
- begin
- data = JSON.parse(buffer)
- rescue => e
- data = []
- buffer.lines.each do |line|
- data << JSON.parse(line)
- end
- end
- data
- end
-
def get_class(table_name)
define_class(table_name)
table_name.camelcase.constantize
end
@@ -130,41 +119,11 @@
end)
end
def get_column_schema(hash)
hash.map do |k, v|
- type = get_type(k, v)
+ type = Util.get_type(k, v)
%Q{t.#{type} "#{k}"} unless type.nil?
- end
- end
-
- def get_type(k, v)
- if v.nil?
- %Q{string}
- elsif k =~ /^id$/i
- nil
- elsif v.class == String
- to_time_or_nil(v).nil? ? %Q{string} : %Q{datetime}
- elsif v.class == TrueClass || v.class == FalseClass
- %Q{boolean}
- elsif v.class == Fixnum
- %Q{integer}
- elsif v.class == Float
- %Q{float}
- elsif v.class == Array || v.class == Hash
- %Q{text}
- elsif v.respond_to?(:strftime)
- %Q{datetime}
- end
- end
-
- def to_time_or_nil(value)
- return nil if value.slice(0, 4) !~ /^[0-9][0-9][0-9][0-9]/
- begin
- time = value.to_time
- time.to_i >= 0 ? time : nil
- rescue => e
- nil
end
end
def get_limit_hash(klass)
columns(klass).inject({}){ |result, column|