Class: Jackfs::DbAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/jackfs/adapters/db_adapter.rb

Constant Summary collapse

TEMP_PATH =
File.join('tmp','fs_cache')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_root, app_env) ⇒ DbAdapter

Returns a new instance of DbAdapter.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jackfs/adapters/db_adapter.rb', line 18

def initialize(app_root, app_env)
  @app_root = app_root
  @app_env = app_env

  FileUtils.mkdir_p temp_file_path

  yml = YAML.load_file(config_file)[@app_env.to_s]
  @connection = uri_escape(yml["connection"])
  @table_name = yml["table_name"]

  # Clean up temp files
  FileUtils.remove_file(File.join(temp_file_path,'/*'), true)
end

Instance Attribute Details

#app_envObject

Returns the value of attribute app_env.



16
17
18
# File 'lib/jackfs/adapters/db_adapter.rb', line 16

def app_env
  @app_env
end

#app_rootObject

Returns the value of attribute app_root.



16
17
18
# File 'lib/jackfs/adapters/db_adapter.rb', line 16

def app_root
  @app_root
end

#connectionObject

Returns the value of attribute connection.



16
17
18
# File 'lib/jackfs/adapters/db_adapter.rb', line 16

def connection
  @connection
end

#dbaseObject

Returns the value of attribute dbase.



16
17
18
# File 'lib/jackfs/adapters/db_adapter.rb', line 16

def dbase
  @dbase
end

#table_nameObject

Returns the value of attribute table_name.



16
17
18
# File 'lib/jackfs/adapters/db_adapter.rb', line 16

def table_name
  @table_name
end

Instance Method Details

#config_fileObject



74
75
76
# File 'lib/jackfs/adapters/db_adapter.rb', line 74

def config_file
  File.join(@app_root, Jackfs::FileStore::CONFIG_FILE)
end

#dataObject



52
53
54
55
56
# File 'lib/jackfs/adapters/db_adapter.rb', line 52

def data
  db = Sequel.connect(@connection)
  make_table(db) unless db.tables.include?(@table_name.to_sym)
  db[@table_name.to_sym]
end

#get(name) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/jackfs/adapters/db_adapter.rb', line 43

def get(name)
  record = data.where(:name => name).order(:name).first
  unique_name = UUIDTools::UUID.random_create.to_s
  # Write Body to generated tmp file
  f = temp_file_path + unique_name
  open(File.join(f), 'wb') { |file| file.write Base64.decode64(record[:body]) }
  f
end

#make_table(db) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jackfs/adapters/db_adapter.rb', line 58

def make_table(db)
  db.create_table @table_name do
    primary_key :id
    string :name
    text :body
    timestamp :created_at
    timestamp :updated_at
  end
rescue
  # table exists
end

#store(f, name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/jackfs/adapters/db_adapter.rb', line 32

def store(f, name)
  body = Base64.encode64(f.read)
  data.insert(
    :name       => name,
    :body       => body,
    :created_at => Time.now,
    :updated_at => Time.now
  )
  name
end

#temp_file_pathObject



70
71
72
# File 'lib/jackfs/adapters/db_adapter.rb', line 70

def temp_file_path
  File.join(@app_root, TEMP_PATH)
end

#uri_escape(uri) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/jackfs/adapters/db_adapter.rb', line 78

def uri_escape(uri)
  begin
    URI.escape(uri)
  rescue Exception => e
    $stdout.puts "Error encountered when parsing #{uri} - #{e.message}"
  end
end