virtus icon indicating copy to clipboard operation
virtus copied to clipboard

Introduce an interface for registering custom attribute extensions

Open solnic opened this issue 12 years ago • 6 comments

As discussed in #207

Virtus::Attribute.register_extension(Virtus::Attribute::Annotated)

# it requires some method that would tell virtus when to use it
# maybe something like this:

module Virtus::Attribute::Annotated
  def self.extend?(options)
    options.key?(:annotations)
  end

  def annotations
    options[:annotations]
  end
end

# then in the Builder#initialize_attribute we could simply extend attribute
# with all registered extensions
Attribute.extensions.each do |mod|
  @attribute.extend(mod) if mod.extend?(@attribute.options)
end

IMPORTANT: this won't block 1.0.0 though, can be added in 1.x as it's just a new feature. Details have not been defined yet.

solnic avatar Oct 03 '13 11:10 solnic

It may be better to use attribute as argument for extend? method, because extension may want to access not only options, but i.e. name, to decide to be applied or not.

mirasrael avatar Oct 03 '13 13:10 mirasrael

That's a great suggestion

On Thu, Oct 3, 2013 at 3:55 PM, mirasrael [email protected] wrote:

It may be better to use attribute as argument for extend? method, because extension may want to access not only options, but i.e. name, to decide to be applied or not.

Reply to this email directly or view it on GitHub: https://github.com/solnic/virtus/issues/208#issuecomment-25622146

solnic avatar Oct 03 '13 13:10 solnic

Are you working on this feature or I can suggest it as pull request?

mirasrael avatar Oct 03 '13 14:10 mirasrael

No I'm not planning to work on it anytime soon. Would love to see a PR :)

On Thu, Oct 3, 2013 at 4:04 PM, mirasrael [email protected] wrote:

Are you working on this future or I can suggest it as pull request?

Reply to this email directly or view it on GitHub: https://github.com/solnic/virtus/issues/208#issuecomment-25622883

solnic avatar Oct 03 '13 14:10 solnic

I opened pull request #209

mirasrael avatar Oct 03 '13 15:10 mirasrael

@mirasrael thanks! that was fast :) I left some comments

solnic avatar Oct 03 '13 15:10 solnic