lib/ajaxlibs/library.rb in ajaxlibs-0.1.3 vs lib/ajaxlibs/library.rb in ajaxlibs-0.1.4
- old
+ new
@@ -7,11 +7,11 @@
class Ajaxlibs::Library
# Available versions for this library.
Versions = []
Requirements = {}
- attr_reader :version, :source
+ attr_reader :version, :source, :secure
@@subclasses = {}
def self.inherited(child) #:nodoc:
@@subclasses[child.library_name.to_sym] = child
@@ -21,10 +21,11 @@
def self.all
@@subclasses.values
end
# Search a specific library by its name (could be either a string or a symbol) and initialized it with given version and source.
+ # See initialize method for available <tt>options</tt>.
def self.by_name(name, options = {})
@@subclasses[name.to_sym].new options
rescue NoMethodError
raise Ajaxlibs::Exception::LibraryNotFound
end
@@ -32,13 +33,19 @@
# Library name based on class name
def self.library_name
name.match(/::(\w+)$/)[1].downcase
end
+ # Initialize a new instance of a specific library.
+ # <tt>options</tt> can take the following arguments :
+ # * <tt>:version</tt> : specify a version (ex: "1.8.1").
+ # * <tt>:source</tt> : force the source to use, default to <tt>:local</tt>.
+ # * <tt>:secure</tt> : specify if the generated link should be secured (https) or not. Default is <tt>false</tt>.
def initialize(options = {})
@version = check_version_or_latest_version(options[:version])
@source = options[:source] || :local
+ @secure = options[:secure] || false
end
# Returns requirements for a library (for example, prototype for scriptaculous)
def requires
self.class::Requirements[@version] || self.class::Requirements[:all] || {}
@@ -64,10 +71,11 @@
File.join('ajaxlibs', library_name, version, file_name)
end
# Include path using google CDN
def google_cdn_include_path
- "http://ajax.googleapis.com/ajax/libs/#{library_name}/#{version}/#{file_name}.js"
+ scheme = secure ? "https" : "http"
+ "#{scheme}://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 or local_only?) ? local_path : google_cdn_include_path
\ No newline at end of file