parameters 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,3 +1,11 @@
1
+ === 0.1.5 / 2009-05-06
2
+
3
+ * Added Parameters::Parser for parsing parameter values passed in from
4
+ the command-line or the web.
5
+ * Changed Parameters::MissingParam and Parameters::ParamNotFound to
6
+ inherit from StandardError.
7
+ * All specs pass on Ruby 1.9.1-p0.
8
+
1
9
  === 0.1.4 / 2009-01-17
2
10
 
3
11
  * All specs now pass with RSpec 1.1.12.
data/Manifest.txt CHANGED
@@ -9,6 +9,7 @@ lib/parameters/exceptions/param_not_found.rb
9
9
  lib/parameters/extensions.rb
10
10
  lib/parameters/extensions/meta.rb
11
11
  lib/parameters/extensions/meta/object.rb
12
+ lib/parameters/parser.rb
12
13
  lib/parameters/class_param.rb
13
14
  lib/parameters/instance_param.rb
14
15
  lib/parameters/param.rb
@@ -18,4 +19,5 @@ tasks/spec.rb
18
19
  spec/spec_helper.rb
19
20
  spec/helpers/test_parameters.rb
20
21
  spec/helpers/inherited_parameters.rb
22
+ spec/parser_spec.rb
21
23
  spec/parameters_spec.rb
data/README.txt CHANGED
@@ -1,7 +1,7 @@
1
1
  = Parameters
2
2
 
3
3
  * http://parameters.rubyforge.org/
4
- * https://github.com/postmodern/parameters
4
+ * http://github.com/postmodern/parameters/
5
5
  * Postmodern (postmodern.mod3 at gmail.com)
6
6
 
7
7
  == DESCRIPTION:
@@ -1,4 +1,4 @@
1
1
  module Parameters
2
- class MissingParam < RuntimeError
2
+ class MissingParam < StandardError
3
3
  end
4
4
  end
@@ -1,4 +1,4 @@
1
1
  module Parameters
2
- class ParamNotFound < RuntimeError
2
+ class ParamNotFound < StandardError
3
3
  end
4
4
  end
@@ -18,14 +18,14 @@ module Parameters
18
18
  # Returns the value of the instance param.
19
19
  #
20
20
  def value
21
- @object.instance_variable_get("@#{@name}")
21
+ @object.instance_variable_get("@#{@name}".to_sym)
22
22
  end
23
23
 
24
24
  #
25
25
  # Sets the value of the instance param.
26
26
  #
27
27
  def value=(value)
28
- @object.instance_variable_set("@#{@name}",value)
28
+ @object.instance_variable_set("@#{@name}".to_sym,value)
29
29
  end
30
30
 
31
31
  #
@@ -147,7 +147,7 @@ module Parameters
147
147
  def initialize_parameters
148
148
  self.class.each_param do |param|
149
149
  # do not override existing instance value if present
150
- unless instance_variable_get("@#{param.name}")
150
+ unless instance_variable_get("@#{param.name}".to_sym)
151
151
  begin
152
152
  if param.value.kind_of?(Proc)
153
153
  value = param.value.call()
@@ -158,7 +158,7 @@ module Parameters
158
158
  value = param.value
159
159
  end
160
160
 
161
- instance_variable_set("@#{param.name}",value)
161
+ instance_variable_set("@#{param.name}".to_sym,value)
162
162
  end
163
163
 
164
164
  self.params[param.name] = InstanceParam.new(self,param.name,param.description)
@@ -196,7 +196,7 @@ module Parameters
196
196
  name = name.to_sym
197
197
 
198
198
  # set the instance variable
199
- instance_variable_set("@#{name}",options[:default])
199
+ instance_variable_set("@#{name}".to_sym,options[:default])
200
200
 
201
201
  # add the new parameter
202
202
  self.params[name] = InstanceParam.new(self,name,options[:description])
@@ -204,12 +204,12 @@ module Parameters
204
204
  instance_eval %{
205
205
  # define the reader method for the parameter
206
206
  def #{name}
207
- instance_variable_get("@#{name}")
207
+ instance_variable_get("@#{name}".to_sym)
208
208
  end
209
209
 
210
210
  # define the writer method for the parameter
211
211
  def #{name}=(value)
212
- instance_variable_set("@#{name}",value)
212
+ instance_variable_set("@#{name}".to_sym,value)
213
213
  end
214
214
  }
215
215
 
@@ -318,7 +318,7 @@ module Parameters
318
318
  names.each do |name|
319
319
  name = name.to_s
320
320
 
321
- unless instance_variable_get("@#{name}")
321
+ unless instance_variable_get("@#{name}".to_sym)
322
322
  raise(Parameters::MissingParam,"parameter #{name.dump} has no value",caller)
323
323
  end
324
324
  end
@@ -0,0 +1,73 @@
1
+ require 'uri'
2
+
3
+ module Parameters
4
+ module Parser
5
+ #
6
+ # The Array of parameter patterns and their parsers.
7
+ #
8
+ def Parser.formats
9
+ @@parameters_parser_formats ||= []
10
+ end
11
+
12
+ #
13
+ # Itereates over each parameter pattern and parser, passing them to the
14
+ # specified _block_.
15
+ #
16
+ def Parser.each_format(&block)
17
+ Parser.formats.each do |format|
18
+ block.call(format[:pattern],format[:parser])
19
+ end
20
+ end
21
+
22
+ #
23
+ # Adds a new parameter _pattern_ using the specified _block_ as the
24
+ # parser.
25
+ #
26
+ def Parser.recognize(pattern,&block)
27
+ Parser.formats.unshift({
28
+ :pattern => pattern,
29
+ :parser => block
30
+ })
31
+ end
32
+
33
+ #
34
+ # Parses the specified _value_ string and returns the native Ruby value.
35
+ # If the _value_ matches one of the patterns within +FORMATS+,
36
+ # then the associated parser will be used to parse the _value_.
37
+ #
38
+ def Parser.parse_value(value)
39
+ Parser.each_format do |pattern,parser|
40
+ if value.match(pattern)
41
+ return parser.call(value)
42
+ end
43
+ end
44
+
45
+ return value
46
+ end
47
+
48
+ #
49
+ # Parses the specified _name_and_value_ string of the form
50
+ # "name=value" and extracts both the _name_ and the _value_, saving
51
+ # both the _name_ and _value_ within the +params+ Hash. If the
52
+ # extracted _value_ matches one of the patterns within +FORMATS+,
53
+ # then the associated parser will first parse the _value_.
54
+ #
55
+ def Parser.parse_param(name_and_value)
56
+ name, value = name_and_value.split('=',2)
57
+
58
+ value = Parser.parse_value(value) if value
59
+ return {name.to_sym => value}
60
+ end
61
+
62
+ Parser.recognize(/^'(\\'|[^'])+'/) { |value|
63
+ value[1...-1].gsub("\\'","'")
64
+ }
65
+ Parser.recognize(/^[a-zA-Z][a-zA-Z0-9]*:\/\//) { |value| URI(value) }
66
+ Parser.recognize('false') { |value| false }
67
+ Parser.recognize('true') { |value| true }
68
+ Parser.recognize(/^[0-9]+$/) { |value| value.to_i }
69
+ Parser.recognize(/^0[0-7]+$/) { |value| value.oct }
70
+ Parser.recognize(/^0x[0-9a-fA-F]+$/) { |value| value.hex }
71
+
72
+ end
73
+ end
@@ -1,3 +1,3 @@
1
1
  module Parameters
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -77,10 +77,10 @@ describe Parameters do
77
77
  end
78
78
 
79
79
  it "should set instance variables for paramters" do
80
- @test.instance_variable_get('@var_with_default').should == 'thing'
80
+ @test.instance_variable_get(:@var_with_default).should == 'thing'
81
81
 
82
82
  @test.var = 3
83
- @test.instance_variable_get('@var').should == 3
83
+ @test.instance_variable_get(:@var).should == 3
84
84
  end
85
85
 
86
86
  it "should allow using lambdas for the default values of parameters" do
@@ -0,0 +1,40 @@
1
+ require 'parameters/parser'
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Parameters::Parser do
6
+ it "should parse string values" do
7
+ Parameters::Parser.parse_value("'bla \\' \\\" bla'").should == "bla ' \\\" bla"
8
+ end
9
+
10
+ it "should parse decimal values" do
11
+ Parameters::Parser.parse_value("100").should == 100
12
+ end
13
+
14
+ it "should parse octal values" do
15
+ Parameters::Parser.parse_value("012").should == 10
16
+ end
17
+
18
+ it "should parse hexadecimal values" do
19
+ Parameters::Parser.parse_value("0xff").should == 0xff
20
+ end
21
+
22
+ it "should parse boolean values" do
23
+ Parameters::Parser.parse_value('true').should == true
24
+ Parameters::Parser.parse_value('false').should == false
25
+ end
26
+
27
+ it "should parse URI values" do
28
+ url = 'http://example.com/'
29
+
30
+ Parameters::Parser.parse_value(url).should == URI(url)
31
+ end
32
+
33
+ it "should parse params of the form 'name'" do
34
+ Parameters::Parser.parse_param('var').should == {:var => nil}
35
+ end
36
+
37
+ it "should parse params of the form 'name=value'" do
38
+ Parameters::Parser.parse_param('var1=test').should == {:var1 => 'test'}
39
+ end
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Postmodern
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-17 00:00:00 -08:00
12
+ date: 2009-05-06 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.8.2
23
+ version: 1.12.2
24
24
  version:
25
25
  description: Parameters allows you to add annotated variables to your classes which may have configurable default values.
26
26
  email:
@@ -45,6 +45,7 @@ files:
45
45
  - lib/parameters/extensions.rb
46
46
  - lib/parameters/extensions/meta.rb
47
47
  - lib/parameters/extensions/meta/object.rb
48
+ - lib/parameters/parser.rb
48
49
  - lib/parameters/class_param.rb
49
50
  - lib/parameters/instance_param.rb
50
51
  - lib/parameters/param.rb
@@ -54,6 +55,7 @@ files:
54
55
  - spec/spec_helper.rb
55
56
  - spec/helpers/test_parameters.rb
56
57
  - spec/helpers/inherited_parameters.rb
58
+ - spec/parser_spec.rb
57
59
  - spec/parameters_spec.rb
58
60
  has_rdoc: true
59
61
  homepage: http://parameters.rubyforge.org/