0.0
No commit activity in last 3 years
No release in over 3 years
Implements a simple pattern for creating "options" in Ruby method. Mix it in to a class and use the DSL to setup defaults for methods that can easily be overwritten as needed.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.6
>= 3.0.0, ~> 3.0
 Project Readme

DefaultOptions

Super simple library provides an easy DSL for setting default options in methods.

Installation

Add this line to your application's Gemfile:

gem 'default_options'

And then execute:

$ bundle

Or install it yourself as:

$ gem install default_options

Usage

Let's say you have a method that returns the number of views a particular blog post receives. This method also accepts a hash of options to allow use to limit the number of views returned to a particular date range:

This is a Rails example, but the library works in any Ruby project

def blog_views(opts = {})
  default_options = {
    :start_date => self.created_at,
    :end_date => Time.zone.now
  }
  options = opts.reverse_merge(default_options)

  self.views.where(:start_date => options[:start_date], :end_date => options[:end_date]).count
end

With DefaultOptions, this same code would look like:

def blog_views(opts = {})
  options = defaults_for opts do
    start_date self.created_at
    end_date Time.zone.now
  end

  self.views.where(:start_date => options.start_date, :end_date => options.end_date).count
end

To add this functionality to any Ruby class, just include the library.

require 'default_options' # If you are using Rails, you don't need this line

class AwesomeSauce
  include DefautOptions

  def rad_thing(opts = {})
	…
  end
end

Contributing

  1. Fork it ( https://github.com/mertonium/default_options/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request