Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: sporkmonger/addressable
base: master
...
head fork: mislav/addressable
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 6 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
Commits on Sep 27, 2009
@mislav mislav support symbol keys in mapping 50f40b3
@mislav mislav numeric values in mapping get casted to string bcae891
@mislav mislav gemspec for v2.1.1 27ee36e
Commits on Oct 20, 2009
@mislav mislav created #variable_defaults method to obtain default values from pattern
Returns a hash of variable names with their defaults, such as:
  { "foo" => "bar" }

Variables without defaults are not present here.
f079f72
@mislav mislav Merge commit 'origin/master' into mislav
Conflicts:
	spec/addressable/template_spec.rb
066274d
@mislav mislav no need to manage the LOAD_PATH, really df4b3cd
View
38 Rakefile
@@ -1,7 +1,3 @@
-lib_dir = File.expand_path(File.join(File.dirname(__FILE__), "lib"))
-$:.unshift(lib_dir)
-$:.uniq!
-
require 'rubygems'
require 'rake'
require 'rake/testtask'
@@ -10,7 +6,7 @@ require 'rake/packagetask'
require 'rake/gempackagetask'
require 'spec/rake/spectask'
-require File.join(File.dirname(__FILE__), 'lib/addressable', 'version')
+require File.join(File.dirname(__FILE__), 'lib', 'addressable', 'version')
PKG_DISPLAY_NAME = 'Addressable'
PKG_NAME = PKG_DISPLAY_NAME.downcase
@@ -31,6 +27,38 @@ Ruby's standard library. It more closely conforms to the relevant RFCs and
adds support for IRIs and URI templates.
TEXT
+desc "generates .gemspec file"
+task :gemspec do
+ spec = Gem::Specification.new do |p|
+ p.name = 'addressable'
+ p.version = PKG_VERSION
+
+ p.summary = PKG_SUMMARY
+ p.description = PKG_DESCRIPTION
+
+ p.author = 'Bob Aman'
+ p.email = 'bob@sporkmonger.com'
+ p.homepage = 'http://github.com/mislav/addressable'
+ p.rubyforge_project = nil
+
+ p.files = FileList['Rakefile', '{bin,lib,tasks,spec}/**/*', 'README*', 'LICENSE*', 'CHANGELOG*'] & `git ls-files`.split
+
+ p.executables = Dir['bin/*'].map { |f| File.basename(f) }
+
+ p.has_rdoc = true
+ end
+
+ spec_string = spec.to_ruby
+
+ begin
+ Thread.new { eval("$SAFE = 3\n#{spec_string}", binding) }.join
+ rescue
+ abort "unsafe gemspec: #{$!}"
+ else
+ File.open("#{spec.name}.gemspec", 'w') { |file| file.write spec_string }
+ end
+end
+
PKG_FILES = FileList[
"lib/**/*", "spec/**/*", "vendor/**/*",
"tasks/**/*", "website/**/*",
View
30 addressable.gemspec
@@ -0,0 +1,30 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{addressable}
+ s.version = "2.1.1"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Bob Aman"]
+ s.date = %q{2009-09-27}
+ s.description = %q{Addressable is a replacement for the URI implementation that is part of
+Ruby's standard library. It more closely conforms to the relevant RFCs and
+adds support for IRIs and URI templates.
+}
+ s.email = %q{bob@sporkmonger.com}
+ s.files = ["Rakefile", "lib/addressable/idna.rb", "lib/addressable/template.rb", "lib/addressable/uri.rb", "lib/addressable/version.rb", "tasks/clobber.rake", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/rdoc.rake", "tasks/rubyforge.rake", "tasks/spec.rake", "spec/addressable/idna_spec.rb", "spec/addressable/template_spec.rb", "spec/addressable/uri_spec.rb", "spec/data/rfc3986.txt", "README", "LICENSE", "CHANGELOG"]
+ s.homepage = %q{http://github.com/mislav/addressable}
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{URI Implementation}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
View
44 lib/addressable/template.rb
@@ -22,10 +22,6 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
-$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '/..')))
-$:.uniq!
-
-require "addressable/version"
require "addressable/uri"
module Addressable
@@ -39,7 +35,7 @@ class Template
Addressable::URI::CharacterClasses::UNRESERVED
OPERATOR_EXPANSION =
/\{-([a-zA-Z]+)\|([#{anything}]+)\|([#{anything}]+)\}/
- VARIABLE_EXPANSION = /\{([#{anything}]+?)(=([#{anything}]+))?\}/
+ VARIABLE_EXPANSION = /\{([#{anything}]+?)(?:=([#{anything}]+))?\}/
##
# Raised if an invalid template value is supplied.
@@ -484,24 +480,38 @@ def expand(mapping, processor=nil)
#
# @return [Array] The variables present in the template's pattern.
def variables
- @variables ||= (begin
- result = []
+ @variables ||= ordered_variable_defaults.map { |var, val| var }.uniq
+ end
+ alias_method :keys, :variables
+
+ ##
+ # Returns a mapping of variables to their default values specified
+ # in the template. Variables without defaults are not returned.
+ #
+ # @return [Hash] Mapping of template variables to their defaults
+ def variable_defaults
+ @variable_defaults ||= Hash[*ordered_variable_defaults.reject { |k,v| v.nil? }.flatten]
+ end
+ private
+
+ def ordered_variable_defaults
+ @ordered_variable_defaults ||= begin
expansions, expansion_regexp = parse_template_pattern(pattern)
- expansions.each do |expansion|
- if expansion =~ OPERATOR_EXPANSION
- _, _, variables, _ = parse_template_expansion(expansion)
- result.concat(variables)
- else
- result << expansion[VARIABLE_EXPANSION, 1]
+
+ expansions.inject([]) do |result, expansion|
+ case expansion
+ when OPERATOR_EXPANSION
+ _, _, variables, mapping = parse_template_expansion(expansion)
+ result.concat variables.map { |var| [var, mapping[var]] }
+ when VARIABLE_EXPANSION
+ result << [$1, $2]
end
+ result
end
- result.uniq
- end)
+ end
end
- alias_method :keys, :variables
- private
##
# Transforms a mapping so that values can be substituted into the
# template.
View
4 lib/addressable/uri.rb
@@ -22,10 +22,6 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
-$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '/..')))
-$:.uniq!
-
-require "addressable/version"
require "addressable/idna"
module Addressable
View
3  spec/addressable/idna_spec.rb
@@ -22,9 +22,6 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
-$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../../lib'))
-$:.uniq!
-
require "addressable/idna"
describe Addressable::IDNA, "when converting from unicode to ASCII" do
View
14 spec/addressable/template_spec.rb
@@ -22,10 +22,6 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
-$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../../lib'))
-$:.uniq!
-
-require "addressable/uri"
require "addressable/template"
if !"".respond_to?("force_encoding")
@@ -1341,6 +1337,16 @@ def self.transform(name, value)
end
end
+describe Addressable::URI, "extracting defaults from a pattern" do
+ before do
+ @template = Addressable::Template.new("{foo}{bar=baz}{-opt|found|cond}")
+ end
+
+ it "should extract default value" do
+ @template.variable_defaults.should == {"bar" => "baz"}
+ end
+end
+
describe Addressable::URI, "when given a mapping with symbol keys" do
before do
@mapping = { :name => "fred" }
View
3  spec/addressable/uri_spec.rb
@@ -22,9 +22,6 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
-$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../../lib'))
-$:.uniq!
-
require "addressable/uri"
if !"".respond_to?("force_encoding")
View
13 tasks/spec.rake
@@ -1,12 +1,10 @@
namespace :spec do
Spec::Rake::SpecTask.new(:rcov) do |t|
+ t.libs = %w[lib spec]
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_opts = ['--color', '--format', 'specdoc']
- if RCOV_ENABLED
- t.rcov = true
- else
- t.rcov = false
- end
+
+ t.rcov = RCOV_ENABLED
t.rcov_opts = [
'--exclude', 'spec',
'--exclude', '1\\.8\\/gems',
@@ -16,6 +14,7 @@ namespace :spec do
end
Spec::Rake::SpecTask.new(:normal) do |t|
+ t.libs = %w[lib spec]
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_opts = ['--color', '--format', 'specdoc']
t.rcov = false
@@ -23,11 +22,11 @@ namespace :spec do
desc "Generate HTML Specdocs for all specs"
Spec::Rake::SpecTask.new(:specdoc) do |t|
- specdoc_path = File.expand_path(
- File.join(File.dirname(__FILE__), '../specdoc/'))
+ specdoc_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'specdoc'))
Dir.mkdir(specdoc_path) if !File.exist?(specdoc_path)
output_file = File.join(specdoc_path, 'index.html')
+ t.libs = %w[lib spec]
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_opts = ["--format", "\"html:#{output_file}\"", "--diff"]
t.fail_on_error = false

No commit comments for this range

Something went wrong with that request. Please try again.