lib/ajaxlibs/library.rb in ajaxlibs-0.1.0 vs lib/ajaxlibs/library.rb in ajaxlibs-0.1.1
- old
+ new
@@ -5,11 +5,14 @@
# and a few functions to generate either a filepath to local library or javascript loading code
# for Google CDN.
class Ajaxlibs::Library
# Available versions for this library.
Versions = []
+ Requirements = {}
+ attr_reader :version, :source
+
@@subclasses = {}
def self.inherited(child) #:nodoc:
@@subclasses[child.library_name.to_sym] = child
end
@@ -17,25 +20,30 @@
# Returns all available libraries (instance of Ajaxlibs::Library).
def self.all
@@subclasses.values
end
- # Search a specific library by its name, could by either a string or a symbol.
- def self.by_name(name)
- @@subclasses[name.to_sym].new
+ # Search a specific library by its name (could be either a string or a symbol) and initialized it with given version and source.
+ def self.by_name(name, options = {})
+ @@subclasses[name.to_sym].new options
rescue NoMethodError
raise Ajaxlibs::Exception::LibraryNotFound
end
# Library name based on class name
def self.library_name
name.match(/::(\w+)$/)[1].downcase
end
+ def initialize(options = {})
+ @version = check_version_or_latest_version(options[:version])
+ @source = options[:source] || :local
+ end
+
# Returns requirements for a library (for example, prototype for scriptaculous)
def requires
- nil
+ self.class::Requirements[@version] || self.class::Requirements[:all]
end
# Library name based on class name
def library_name
self.class.library_name
@@ -50,16 +58,29 @@
def latest_version
self.class::Versions.max {|a, b| Ajaxlibs::VersionsTools.compare a, b}
end
# Local path for a particular version, or the latest if given version is nil.
- def local_path(version = nil)
- File.join('ajaxlibs', library_name, check_version_or_latest_version(version), file_name)
+ def local_path
+ File.join('ajaxlibs', library_name, version, file_name)
end
- # Javascript load code through google jsapi for a particular version, or the latest if given version is nil.
- def google_cdn_load_code(version = nil)
- "google.load('#{library_name}', '#{check_version_or_latest_version(version)}');"
+ # Include path using google CDN
+ def google_cdn_include_path
+ "http://ajax.googleapis.com/ajax/libs/#{library_name}/#{version}/#{file_name}.js"
+ end
+
+ # Javascript include path regarding source (call either local_path or google_cdn_include_path)
+ def include_path
+ source == :local ? local_path : google_cdn_include_path
+ end
+
+ def local_only?
+ false
+ end
+
+ def ==(other)
+ self.class == other.class and self.version == other.version and self.source == other.source
end
private
# Checks if given version is available for this library,
# raises Ajaxlibs::Exception::VersionNotFound if not and returns it.
\ No newline at end of file