Traitorous
This is a simple trait based system that emphasizes reading in and out data structures, using an plugin Converter system. Each trait defined has a name and a Converter obj that responds to :export, and :import.
This process came out of a need to have a flexible config system that could be read from files easily (yaml, json, f*$k xml), populate a nested set of objects, and able to export the while thing ready to be encoded and saved back to disk.
The converters can be used to help (de)serialize, set default values, do validations and translations. The converter's job is to be able to import a value, instantiating into classes and assigning values.
I took a lot of inspiration from virtus.
Installation
Add this line to your application's Gemfile:
gem 'traitorous'
And then execute:
$ bundle
Or install it yourself as:
$ gem install traitorous
Usage
# see spec/
require 'traitorous'
class Ruin
include Traitorous
include Traitorous::Equality
trait :name
trait :danger
end
r = Ruin.new(name: 'Skull Mountain', danger: 'The Devil of')
#
puts r.name
# Skull Mountain
puts r.danger
# The Devil of
puts r.export
# {'name' => 'Skull Mountain, 'danger' => 'The Devil of'}
puts Ruin.new(r.export) == r
# true
class Area
include Traitorous
include Traitorous::Equality
trait :name
trait :size, Converter::DefaultValueStatic.new('sub-continent')
trait :ruins, Converter::UniformArray.new(Ruin)
end
area = Area.new(
name: 'Western Marches',
size: 'Region',
ruins: [{name: 'Skull Mountain', danger: 'The Devil of'},
{name: 'Dire Swamp', danger: 'The Devil of'}
]
)
puts area.ruins.length
# 2
puts area.export
# {:name=>"Western Slope", :size=>"region", :ruins=>[{:name=>"Dire Swamp", :danger=>"The Creature of"}, {:name=>"Skull Mountain", :danger=>"The Devil of"}]}
puts Area.new(area.export) == area
# true
Development
I use Guard to automate testing. It won't affect anything execpt the disk space
needed to store the gems. If you do want to use it, from a shell in the home
directory of the gem and type guard.
After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
- Fork it ( https://github.com/[my-github-username]/traitorous/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request