Class: Amalgalite::Requires
- Inherits:
-
Object
- Object
- Amalgalite::Requires
- Defined in:
- lib/amalgalite/requires.rb,
ext/amalgalite/c/amalgalite_requires_bootstrap.c
Overview
Requires encapsulates requiring items from the database
Defined Under Namespace
Classes: Bootstrap
Instance Attribute Summary collapse
-
#compressed_column ⇒ Object
readonly
Returns the value of attribute compressed_column.
-
#contents_column ⇒ Object
readonly
Returns the value of attribute contents_column.
-
#db_connection ⇒ Object
readonly
Returns the value of attribute db_connection.
-
#dbfile_name ⇒ Object
readonly
Returns the value of attribute dbfile_name.
-
#filename_column ⇒ Object
readonly
Returns the value of attribute filename_column.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Class Method Summary collapse
-
.db_connection_to(dbfile_name) ⇒ Object
Allocate a database connection to the given filename.
- .load_path ⇒ Object
- .load_path_db_connections ⇒ Object
- .require(filename) ⇒ Object
-
.requiring ⇒ Object
Setting a class level variable as a flag to know what we are currently in the middle of requiring.
Instance Method Summary collapse
-
#file_contents(filename) ⇒ Object
Return the contents of the named file.
-
#import(sql) ⇒ Object
Import an SQL dump into the fake file system.
-
#initialize(opts = {}) ⇒ Requires
constructor
A new instance of Requires.
-
#require(filename) ⇒ Object
load a file in this database table.
-
#sql ⇒ Object
return the sql to find the file contents for a file in this requires.
Constructor Details
#initialize(opts = {}) ⇒ Requires
Returns a new instance of Requires.
74 75 76 77 78 79 80 81 82 |
# File 'lib/amalgalite/requires.rb', line 74 def initialize( opts = {} ) @dbfile_name = opts[:dbfile_name] || Bootstrap::DEFAULT_DB @table_name = opts[:table_name] || Bootstrap::DEFAULT_TABLE @filename_column = opts[:filename_column] || Bootstrap::DEFAULT_FILENAME_COLUMN @contents_column = opts[:contents_column] || Bootstrap::DEFAULT_CONTENTS_COLUMN @compressed_column = opts[:compressed_column] || Bootstrap::DEFAULT_COMPRESSED_COLUMN @db_connection = Requires.db_connection_to( dbfile_name ) Requires.load_path << self end |
Instance Attribute Details
#compressed_column ⇒ Object (readonly)
Returns the value of attribute compressed_column.
71 72 73 |
# File 'lib/amalgalite/requires.rb', line 71 def compressed_column @compressed_column end |
#contents_column ⇒ Object (readonly)
Returns the value of attribute contents_column.
70 71 72 |
# File 'lib/amalgalite/requires.rb', line 70 def contents_column @contents_column end |
#db_connection ⇒ Object (readonly)
Returns the value of attribute db_connection.
72 73 74 |
# File 'lib/amalgalite/requires.rb', line 72 def db_connection @db_connection end |
#dbfile_name ⇒ Object (readonly)
Returns the value of attribute dbfile_name.
67 68 69 |
# File 'lib/amalgalite/requires.rb', line 67 def dbfile_name @dbfile_name end |
#filename_column ⇒ Object (readonly)
Returns the value of attribute filename_column.
69 70 71 |
# File 'lib/amalgalite/requires.rb', line 69 def filename_column @filename_column end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
68 69 70 |
# File 'lib/amalgalite/requires.rb', line 68 def table_name @table_name end |
Class Method Details
.db_connection_to(dbfile_name) ⇒ Object
Allocate a database connection to the given filename. For file databases, this means giving the same connection back if you ask for a connection to the same file. For in-memory databases, you get a new one each time.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/amalgalite/requires.rb', line 26 def db_connection_to( dbfile_name ) if dbfile_name == ":memory:" return ::Amalgalite::Database.new( dbfile_name ) else unless connection = load_path_db_connections[ dbfile_name ] connection = ::Amalgalite::Database.new( dbfile_name ) load_path_db_connections[dbfile_name] = connection end return connection end end |
.load_path ⇒ Object
16 17 18 |
# File 'lib/amalgalite/requires.rb', line 16 def load_path @load_path ||= [] end |
.load_path_db_connections ⇒ Object
12 13 14 |
# File 'lib/amalgalite/requires.rb', line 12 def load_path_db_connections @load_path_db_connections ||= {} end |
.require(filename) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/amalgalite/requires.rb', line 46 def require( filename ) if load_path.empty? then raise ::LoadError, "Amalgalite load path is empty -- #{filename}" elsif $LOADED_FEATURES.include?( filename ) then return false elsif Requires.requiring.include?( filename ) then return false else Requires.requiring << filename load_path.each do |lp| if lp.require( filename ) then Requires.requiring.delete( filename ) return true end end Requires.requiring.delete( filename ) raise ::LoadError, "amalgalite has no such file to load -- #{filename}" end end |
.requiring ⇒ Object
Setting a class level variable as a flag to know what we are currently in the middle of requiring
42 43 44 |
# File 'lib/amalgalite/requires.rb', line 42 def requiring @requiring ||= [] end |
Instance Method Details
#file_contents(filename) ⇒ Object
Return the contents of the named file.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/amalgalite/requires.rb', line 125 def file_contents(filename) rows = db_connection.execute(sql, filename) if rows.size > 0 then row = rows.first contents = row[contents_column].to_s if row[compressed_column] then contents = ::Amalgalite::Packer.gunzip( contents ) end return contents else return nil end end |
#import(sql) ⇒ Object
Import an SQL dump into the fake file system.
145 146 147 |
# File 'lib/amalgalite/requires.rb', line 145 def import(sql) db_connection.import(sql) end |
#require(filename) ⇒ Object
load a file in this database table. This will check and see if the file is already required. If it isn’t it will select the contents associated with the row identified by the filename and eval those contents within the context of TOPLEVEL_BINDING. The filename is then appended to $LOADED_FEATURES.
if the file was required then true is returned, otherwise false
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/amalgalite/requires.rb', line 100 def require( filename ) if $LOADED_FEATURES.include?( filename ) then return false else begin filename = filename.gsub(/\.rb\Z/,'') contents = file_contents(filename) if contents eval( contents, TOPLEVEL_BINDING, filename ) $LOADED_FEATURES << filename return true else return false end rescue => e raise ::LoadError, "Failure loading #{filename} from #{dbfile_name} : #{e}" end end end |
#sql ⇒ Object
return the sql to find the file contents for a file in this requires
87 88 89 |
# File 'lib/amalgalite/requires.rb', line 87 def sql @sql ||= "SELECT #{filename_column}, #{compressed_column}, #{contents_column} FROM #{table_name} WHERE #{filename_column} = ?" end |