Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .document
Octocat-spinner-32 .gemtest
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .yardopts
Octocat-spinner-32 ChangeLog.md
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 gemspec.yml
Octocat-spinner-32 parameters.gemspec
README.md

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.

Something went wrong with that request. Please try again.