solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 229 forks source link

Replace global configuration polluting Virtus module with instance-based configuration #165

Closed solnic closed 11 years ago

solnic commented 11 years ago

Right now you can configure virtus global attribute options and coercers using Virtus module. The idea is to replace that with a module builder so that you can create as many virtus modules to be included in your classes as you need where each of those modules would have its own configuration.

For instance it could look like that:

# create customized virtus module
MyCustomVirtusModule = Virtus.configure { |config|
  config.coercion = true
  # some extra conf should be possible too
  config.string.boolean_map = { 'yup' => true, 'nope' => false }
}

class Book
  include MyCustomVirtusModule

  attribute :published, Boolean
end

This way we won't be polluting global Virtus constant and provide ability to use Virtus in different ways depending on the context. For instance in form-like objects you want to have coercions turned on but in pure domain objects you may want to have coercions turned off. Or you could use strict-mode for coercion where unknown coercion raises an exception in one place and have it turned off in other etc.

elskwid commented 11 years ago

@solnic, thank you for writing this up. I will take a stab at it over the next couple days and see what I can come up with.

solnic commented 11 years ago

closing since it's now a WIP in #167