Class: ActiveRecordData

Inherits:
BaseUri show all
Defined in:
lib/libisi/uri/activerecord.rb

Constant Summary collapse

ADAPTERS =
["mysql","postgresql","sqlite3","sqlite"]
@@num =
0

Instance Attribute Summary collapse

Attributes inherited from BaseUri

#options, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseUri

#base_uri, #credential_hash, #execute_command, #find, #password, #user

Constructor Details

#initialize(uri, options) ⇒ ActiveRecordData

Returns a new instance of ActiveRecordData.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/libisi/uri/activerecord.rb', line 57

def initialize(uri, options)
  require "rubygems"
  require "active_record"
  ActiveRecord::Base.logger = $log

  super
      
  @adapter = uri.scheme
  
  vals = ActiveRecordData.split_path(uri)
  @database = vals[:database]
  @table = vals[:table]
  raise "No database given" unless @database
  raise "No table given" unless @table    

  if options[:model]
    require "active_support/core_ext/string/inflections.rb"
    
    $log.info("Loading classes from #{options[:model]}")
    @model_path = Pathname.new(options[:model])
    @class_names = []

    raise "Model path #{@model_path} not found" unless @model_path.exist?
    
    use_activesupport = true
    if use_activesupport
	ActiveSupport::Dependencies.load_paths << @model_path.to_s
    end

    @model_path.find {|p|
	next unless p.to_s =~ /\/([^\/]+)\.rb$/
	
	class_name = $1.camelize
	@class_names << class_name
	
	unless use_activesupport
 $log.debug("Define autoload: #{class_name.to_sym} => #{p}")
 autoload(class_name.to_sym, p.to_s)	
	end
    }
  end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



56
57
58
# File 'lib/libisi/uri/activerecord.rb', line 56

def adapter
  @adapter
end

#databaseObject (readonly)

Returns the value of attribute database.



56
57
58
# File 'lib/libisi/uri/activerecord.rb', line 56

def database
  @database
end

#tableObject (readonly)

Returns the value of attribute table.



56
57
58
# File 'lib/libisi/uri/activerecord.rb', line 56

def table
  @table
end

Class Method Details

.split_path(uri) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/libisi/uri/activerecord.rb', line 24

def ActiveRecordData.split_path(uri)
  return {} unless ADAPTERS.include?(uri.scheme)

  return {} if uri.path.length <= 1
  splited = uri.path[1..-1].split("/")
  case uri.scheme
  when "mysql","postgresql"
    ret = nil
    case splited.length
    when 1
	ret = {:database => splited[0]}
    when 2
	ret = {:database => splited[0], :table => splited[1]}
    else
    ret = {}
    end
  when "sqlite","sqlite3"
    while splited.length > 1 and (exist = Pathname.new(splited[0]).exist?) and Pathname.new(splited[0]).directory?
	$log.debug("#{splited[0]} exist and is a directory")
	splited = [splited[0..1].join("/")] + splited[2..-1]
    end
   
    ret = {:database => splited[0], :table => splited[1]}
  end
  $log.debug("ActiveRecordData split_path: #{uri.inspect} => #{ret.inspect}")
  ret
end

.supports?(uri) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/libisi/uri/activerecord.rb', line 52

def ActiveRecordData.supports?(uri)
  !split_path(uri)[:table].nil?
end

Instance Method Details

#column_namesObject



102
# File 'lib/libisi/uri/activerecord.rb', line 102

def column_names; active_record_class.column_names; end

#create(attributes, &block) ⇒ Object



106
# File 'lib/libisi/uri/activerecord.rb', line 106

def create(attributes,&block); active_record.create(attributes,&block) ;end

#entry_not_found_exceptionObject



105
# File 'lib/libisi/uri/activerecord.rb', line 105

def entry_not_found_exception; ActiveRecord::RecordNotFound; end

#itemsObject



104
# File 'lib/libisi/uri/activerecord.rb', line 104

def items; active_record_class.find(:all).to_a; end

#primary_keyObject



100
# File 'lib/libisi/uri/activerecord.rb', line 100

def primary_key; active_record_class.primary_key.to_s ;end