|
Friday, 19 March 2010 13:48 |
Return a value from 0 to 5:
rand(6)
Return a value between 0 and 1:
rand
Resources:
- Module: Kernel [ruby-doc.org] Read more: |
|
Sunday, 28 February 2010 20:22 |
// description of your code here
class OptParseSimple
def initialize(filename, args)
doc = Document.new(File.open('options.xml','r').read)
@options = XPath.match(doc.root, 'records/option[switch!=""]')
switches = @options.map do |option|
switch = option.text('switch')
switch[0] == '-' ? switch : nil
end
switches.compact!
# split the argument switches if grouped e.g. -ltr
args.map! do |arg|
if arg[/^\-[a-zA-Z]+$/] and switches.grep(/#{arg}/).empty? then
arg[1..-1].scan(/./).map {|x| '-' + x}
else
arg
end
end
args.flatten!
a1 = options_match(@options[0], args).flatten.each_slice(2).map {|x| x if x[0]}.compact
options_remaining = XPath.match(doc.root, 'records/option[switch=""]/name/text()')
a2 = args.zip(options_remaining).map(&:reverse)
if a2.map(&:first).all? then
@h = Hash[*(a1+a2).map{|x,y| [x.to_s.to_sym, y]}.flatten]
else
invalid_option = a2.detect {|x,y| x.nil? }.last
raise "invalid option: %s not recognised" % invalid_option
end
end
def to_h()
@h
end
private
def options_match(option, args)
switch, switch_alias = option.text('switch'), option.text('alias')
switch_pattern = switch_alias ? "(%s|%s)" % [switch, switch_alias] : switch
switch_matched, arg_index = args.each_with_index.detect {|x,j| x[/^#{switch_pattern}/]}
key, value = nil
if switch_matched then
value_pattern = option.text('value')
if value_pattern then
# check for equal sign
value = switch_matched[/\=(#{value_pattern})/,1]
# check the next arg
if value.nil? and args.length > 0 then
next_arg = args[arg_index + 1]
# check to make sure it's not the next switch
next_option = @options[1] if @options.length > 1
if next_arg != next_option then
# validate using the regex
value = next_arg[/#{value_pattern}/]
if value then
args.delete_at(arg_index + 1)
else
raise option.text('errors/records/error[last()]')
end
end
else
args.delete_at(arg_index)
end
else
args.delete_at(arg_index)
end
key = option.text('name')
elsif option.text('mandatory').downcase == 'true' then
raise option.text('errors/records/error')
end
pair = [key, value]
@options.shift
next_pair = options_match(@options[0], args) if @options.length > 0
[pair, next_pair]
end
end
 Read more: |
|
|
Sunday, 28 February 2010 20:22 |
// Using an XML file to define options OptParseSimple
class OptParseSimple
def initialize(filename, args)
doc = Document.new(File.open('options.xml','r').read)
@options = XPath.match(doc.root, 'records/option[switch!=""]')
switches = @options.map do |option|
switch = option.text('switch')
switch[0] == '-' ? switch : nil
end
switches.compact!
# split the argument switches if grouped e.g. -ltr
args.map! do |arg|
if arg[/^\-[a-zA-Z]+$/] and switches.grep(/#{arg}/).empty? then
arg[1..-1].scan(/./).map {|x| '-' + x}
else
arg
end
end
args.flatten!
a1 = options_match(@options[0], args).flatten.each_slice(2).map {|x| x if x[0]}.compact
options_remaining = XPath.match(doc.root, 'records/option[switch=""]/name/text()')
a2 = args.zip(options_remaining).map(&:reverse)
if a2.map(&:first).all? then
@h = Hash[*(a1+a2).map{|x,y| [x.to_s.to_sym, y]}.flatten]
else
invalid_option = a2.detect {|x,y| x.nil? }.last
raise "invalid option: %s not recognised" % invalid_option
end
end
def to_h()
@h
end
private
def options_match(option, args)
switch, switch_alias = option.text('switch'), option.text('alias')
switch_pattern = switch_alias ? "(%s|%s)" % [switch, switch_alias] : switch
switch_matched, arg_index = args.each_with_index.detect {|x,j| x[/^#{switch_pattern}/]}
key, value = nil
if switch_matched then
value_pattern = option.text('value')
if value_pattern then
# check for equal sign
value = switch_matched[/\=(#{value_pattern})/,1]
# check the next arg
if value.nil? and args.length > 0 then
next_arg = args[arg_index + 1]
# check to make sure it's not the next switch
next_option = @options[1] if @options.length > 1
if next_arg != next_option then
# validate using the regex
value = next_arg[/#{value_pattern}/]
if value then
args.delete_at(arg_index + 1)
else
raise option.text('errors/records/error[last()]')
end
end
else
args.delete_at(arg_index)
end
else
args.delete_at(arg_index)
end
key = option.text('name')
elsif option.text('mandatory').downcase == 'true' then
raise option.text('errors/records/error')
end
pair = [key, value]
@options.shift
next_pair = options_match(@options[0], args) if @options.length > 0
[pair, next_pair]
end
end
 Read more: |
|
Written by
|
|
Tuesday, 16 February 2010 12:41 |
// We were having a problem where it was not possible to detect the difference between a value being passed with no value and the value not being passed at all. This was a problem because we have a singe update page to handle most of the database saves; it would loop through all the possible values of a data object setting them based on what was passed in the form. If a column was not present in the original form it would be set to NULL by the update page. The work around was to place these values in the original form as hidden elements; this meant that whenever we added a column it had to be added to the form(s). This was obviously not a tenable solution so I came up with the following method of requesting a value from a POST or GET.
#region Private Properties
string[] _formKeys;
string[] _queryStringKeys;
#endregion
#region Web Page Properties
/// A list of keys found in Request.Form
protected string[] FormKeys
{
get
{
if (this._formKeys == null)
this._formKeys = Request.Form.AllKeys;
return this._formKeys;
}
}
/// A list of keys found in Request.QueryString
protected string[] QueryStringKeys
{
get
{
if (this._queryStringKeys == null)
this._queryStringKeys = Request.QueryString.AllKeys;
return this._queryStringKeys;
}
}
#endregion
#region public methods
///Grabs parameter from Form or Query String. Never returns NULL.
/// The name of the parameter to be pulled
/// Value of parameter. Returns String.Empty if no value or parameter found.
protected string requestParam(string paramName)
{
string result = requestParam(paramName, false);
return (result == null) ? String.Empty : result.Trim();
}
///Grabs parameter from Form or Query String.
/// The name of the parameter to be pulled
/// Will return NULL if parameter was not passed in the form or query string. If false an empty string will be returned.
/// Value of parameter. Returns String.Empty parameter found but has no value.
///
protected string requestParam(string paramName, bool returnNullIfParamNotFound)
{
string result = String.Empty;
if (Context.Request.Form.Count != 0)
{
result = Convert.ToString(Context.Request.Form[paramName]);
}
if (Context.Request.QueryString.Count != 0 && (result == String.Empty || result == null))
{
result = Convert.ToString(Context.Request.QueryString[paramName]);
}
if (result == null && returnNullIfParamNotFound)
{
// check the form keys
for (int x = 0; x < this.FormKeys.Length; x++)
if (string.Equals(paramName, this.FormKeys[x], StringComparison.OrdinalIgnoreCase))
return string.Empty; //doing a return to skip the rest
// check the query string keys
for (int x = 0; x < this.QueryStringKeys.Length; x++)
if (string.Equals(paramName, this.QueryStringKeys[x], StringComparison.OrdinalIgnoreCase))
return string.Empty; //doing a return to skip the rest
}
return result;
}
#endregion
 Read more: |
|