solnic / virtus

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

Introduce an interface for registering custom attribute extensions #208

Open solnic opened 10 years ago

solnic commented 10 years ago

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.

mirasrael commented 10 years ago

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.

solnic commented 10 years ago

That's a great suggestion

On Thu, Oct 3, 2013 at 3:55 PM, mirasrael notifications@github.com 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

mirasrael commented 10 years ago

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

solnic commented 10 years ago

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 notifications@github.com 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

mirasrael commented 10 years ago

I opened pull request #209

solnic commented 10 years ago

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