Project

parameters

0.02
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Parameters allows you to add annotated variables to your classes which may have configurable default values.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.4
~> 0.7
 Project Readme

Parameters

Description

Parameters allows you to add annotated variables to your classes which may have configurable default values.

Features

  • Give parameters default values.
    • Default values maybe either objects or lambdas used to generate the default value.
  • Change default values of parameters.
  • Enforce types on the values of parameters.
  • Give descriptions to parameters.
  • Set parameters en-mass.

Examples

require 'parameters'

class Octagon

  include Parameters

  #
  # A parameter with a default value
  #
  parameter :x, :default => 0

  #
  # Another parameter with a default value
  #
  parameter :y, :default => 0.5

  #
  # A parameter with an enforced type and description.
  #
  # Availble types are: Array[Class], Array, Set[Class], Set,
  # URI, Regexp, DateTime, Date, Symbol, String, Integer, Float
  # and true (for boolean types).
  #
  parameter :radius, :type        => Float,
                     :description => 'The radius of the Octagon'

  #
  # A parameter with a lambda for a default value
  #
  parameter :opacity, :default     => lambda { rand },
                      :description => 'The opacity of the Octagon'

end

# Create an object with default values for all parameters
oct = Octagon.new
oct.x       # => 0
oct.y       # => 0.5
oct.opacity # => 0.25

# Create an object with the given parameter values.
oct = Octagon.new(:radius => 10)
oct.radius  # => 10
oct.opacity # => 0.7

# Set parameter values of a class
Octagon.radius = 33
Octagon.opacity = 0.3

# Create an object with parameter defaulte values inherited from the
# class parameters
oct = Octagon.new
oct.radius  # => 33
oct.opacity # => 0.3

# Coerce data from the command-line into the given parameter type
oct.radius = ARGV[2]
oct.radius # => 89.455

Install

$ gem install parameters

License

See {file:LICENSE.txt} for license information.