hash_out
Include HashOut to your class and convert your object to a hash with the #hash_out method.
Install
Add to your Gemfile:
gem 'hash_out', '~> 0.1'
Or install it from the command line:
$ gem install hash_out
Usage
require 'hash_out'.- Include
HashOutin your class. - If desired, exclude methods from
#hash_outby addingexclude_from_hash_outto them. - Call
#hash_outon the instance to return a hash of method names and their values.
#hash_out automatically excludes the following from the hash it returns:
- private methods
- methods that require arguments
- instance methods in classes that include
HashOutthat wrap or otherwise call#hash_out
require 'hash_out'
class Movie
include HashOut
def title
'Fire Walk With Me'
end
def director
exclude_from_hash_out
'David Lynch'
end
def available_instantly? catalog=TerribleStreamingService.new
catalog.available_instantly? self
end
def has_actor? actor
actors.include? actor
end
def chance_of_sequel? existing_sequels, strategy=FutureSequelStrategy
strategy.new(self, existing_sequels).chance > 0.5
end
private
def actors
['Sheryl Lee', 'David Bowie', 'Ray Wise']
end
end
movie = Movie.new
movie.hash_out
# => {:title=>"Fire Walk With Me", :available_instantly?=>false}
Contribute
- Fork the repo.
- Create a branch.
- Add specs and code.
- Ensure the specs are green (
$ rake) - Open a pull request.
License
The MIT License (MIT)
Copyright (c) 2013 Dave Jachimiak
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
