Class: Arison::Core
- Inherits:
- 
      Object
      
        - Object
- Arison::Core
 
- Defined in:
- lib/arison/core.rb
Instance Method Summary collapse
- #add_column_live(table_name, data) ⇒ Object
- #columns(klass) ⇒ Object
- #columns_with_table_name(table_name) ⇒ Object
- #create_table(table_name, data) ⇒ Object
- #define_class(table_name) ⇒ Object
- #get_add_column_dsl(table_name, column_name, record) ⇒ Object
- #get_add_index_dsl(table_name, uniq_columns) ⇒ Object
- #get_class(table_name) ⇒ Object
- #get_column_schema(hash) ⇒ Object
- #get_create_table_dsl(table_name, hash) ⇒ Object
- #get_limit_hash(klass) ⇒ Object
- #import(table_name, data) ⇒ Object
- 
  
    
      #initialize(profile)  ⇒ Core 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of Core. 
- #query(sql) ⇒ Object
- #tables ⇒ Object
Constructor Details
#initialize(profile) ⇒ Core
Returns a new instance of Core.
| 10 11 12 13 14 15 16 | # File 'lib/arison/core.rb', line 10 def initialize(profile) @profile = profile ActiveRecord::Base.establish_connection(@profile) ActiveRecord::Base.default_timezone = :local @connection = ActiveRecord::Base.connection @logger = Logger.new(STDOUT) end | 
Instance Method Details
#add_column_live(table_name, data) ⇒ Object
| 136 137 138 139 140 141 142 143 144 145 146 147 | # File 'lib/arison/core.rb', line 136 def add_column_live(table_name, data) data.each do |record| limits = get_limit_hash(get_class(table_name)) diff_keys = (record.keys - limits.keys) diff_keys.each do |column| add_column_dsl = get_add_column_dsl(table_name, column, record) Arison::Migration.run_dsl(add_column_dsl) end ActiveRecord::Base.establish_connection(@profile) end end | 
#columns(klass) ⇒ Object
| 26 27 28 29 30 | # File 'lib/arison/core.rb', line 26 def columns(klass) klass.columns.map do |column| JSON.parse(column.to_json) end end | 
#columns_with_table_name(table_name) ⇒ Object
| 22 23 24 | # File 'lib/arison/core.rb', line 22 def columns_with_table_name(table_name) columns(get_class(table_name)) end | 
#create_table(table_name, data) ⇒ Object
| 96 97 98 99 100 101 102 103 104 105 | # File 'lib/arison/core.rb', line 96 def create_table(table_name, data) first = data.class == Array ? data.first : data 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 | 
#define_class(table_name) ⇒ Object
| 112 113 114 115 116 117 118 119 120 | # File 'lib/arison/core.rb', line 112 def define_class(table_name) klass_sym = table_name.camelcase.to_sym if Object.constants.include?(klass_sym) Object.send(:remove_const, klass_sym) end Object.const_set(table_name.camelcase, Class.new(ActiveRecord::Base) do self.table_name = table_name end) end | 
#get_add_column_dsl(table_name, column_name, record) ⇒ Object
| 90 91 92 93 94 | # File 'lib/arison/core.rb', line 90 def get_add_column_dsl(table_name, column_name, record) dsl = %Q{ add_column "#{table_name}", "#{column_name}", :#{Util.get_type(column_name, record['column_name'])} } end | 
#get_add_index_dsl(table_name, uniq_columns) ⇒ Object
| 84 85 86 87 88 | # File 'lib/arison/core.rb', line 84 def get_add_index_dsl(table_name, uniq_columns) dsl = %Q{ add_index(:#{table_name}, #{uniq_columns.map(&:to_sym)}, unique: true) } end | 
#get_class(table_name) ⇒ Object
| 107 108 109 110 | # File 'lib/arison/core.rb', line 107 def get_class(table_name) define_class(table_name) table_name.camelcase.constantize end | 
#get_column_schema(hash) ⇒ Object
| 122 123 124 125 126 127 | # File 'lib/arison/core.rb', line 122 def get_column_schema(hash) hash.map do |k, v| type = Util.get_type(k, v) %Q{t.#{type} "#{k}"} unless type.nil? end end | 
#get_create_table_dsl(table_name, hash) ⇒ Object
| 74 75 76 77 78 79 80 81 82 | # File 'lib/arison/core.rb', line 74 def get_create_table_dsl(table_name, hash) dsl = %Q{ create_table "#{table_name}" do |t| #{get_column_schema(hash).join("\n")} t.datetime "created_at" t.datetime "updated_at" end } end | 
#get_limit_hash(klass) ⇒ Object
| 129 130 131 132 133 134 | # File 'lib/arison/core.rb', line 129 def get_limit_hash(klass) columns(klass).inject({}){ |result, column| result[column['name']] = column['limit'] result } end | 
#import(table_name, data) ⇒ Object
| 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | # File 'lib/arison/core.rb', line 36 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 begin record = record.inject({}){ |result, (k, v)| length = limits[k] result[k] = if (v.class == Array || v.class == Hash) v.to_s elsif (length.nil? || v.nil? || v.class != String) v else v.slice(0, length) end result } instance.attributes = record rescue ActiveRecord::UnknownAttributeError => e rescue => e puts "\n#{e.}\n#{e.backtrace.join("\n")}" ensure instances << instance end end instances.in_groups_of(10000, false) do |block| begin klass.import(block) rescue => e pp block @logger.error "\n#{e.}\n#{e.backtrace.join("\n")}" raise end end end | 
#query(sql) ⇒ Object
| 18 19 20 | # File 'lib/arison/core.rb', line 18 def query(sql) @connection.exec_query(sql).to_a end | 
#tables ⇒ Object
| 32 33 34 | # File 'lib/arison/core.rb', line 32 def tables @connection.tables end |