solnic / virtus

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

What is the best way to define custom setters? #278

Closed hendricius closed 9 years ago

hendricius commented 9 years ago

Having an issue with custom setters I want to define. I keep getting a stack level too deep. I wonder what is the best practice?

  class CampaignSetupPage
    include Virtus.model
    extend ActiveModel::Naming
    include ActiveModel::Conversion
    include ActiveModel::Validations

    attribute :url, String
    attribute :custom_title, String
    attribute :crawled_title, String
    attribute :keywords, Array
    attribute :scraper, ShoppageScraper

    def url=(new_url)
      if new_url.is_a? String
        prefixed = LinkHelper.prefix_with_protocol_if_no_protocol(url_string: new_url)
        self[:url] = prefixed
      else
        super
      end
    end
  end
hendricius commented 9 years ago

Figured it out, updated the readme: https://github.com/solnic/virtus/pull/279

Thanks for the awesome gem!