Sha256: a02ce3966987360f180bde0185ed5357428661d05923c5d902a75fcbc6f79ff8

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

module AutoHtmlFor

  # default options that can be overridden on the global level
  @@auto_html_for_options = {
    :htmlized_attribute_suffix => '_html'
  }
  mattr_reader :auto_html_for_options

  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def auto_html_for(raw_attrs, &proc)
      include AutoHtmlFor::InstanceMethods
      before_save :auto_html_prepare

      define_method("auto_html_prepare") do
        auto_html_methods = self.methods.select { |m| m=~/^auto_html_prepare_/ }
        auto_html_methods.each do |method|
          self.send(method)
        end
      end

      suffix =  AutoHtmlFor.auto_html_for_options[:htmlized_attribute_suffix]

      [raw_attrs].flatten.each do |raw_attr|
        define_method("#{raw_attr}#{suffix}=") do |val|
          write_attribute("#{raw_attr}#{suffix}", val)
        end
        define_method("#{raw_attr}#{suffix}") do
          read_attribute("#{raw_attr}#{suffix}") || send("auto_html_prepare_#{raw_attr}")
        end
        define_method("auto_html_prepare_#{raw_attr}") do
          self.send(raw_attr.to_s + suffix + "=", 
            auto_html(self.send(raw_attr), &proc))
        end
      end
    end
  end

  module InstanceMethods
    include AutoHtml
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
auto_html-1.4.0 lib/auto_html/auto_html_for.rb
auto_html-1.3.7 lib/auto_html/auto_html_for.rb
auto_html-1.3.6 lib/auto_html/auto_html_for.rb
auto_html-1.3.5 lib/auto_html/auto_html_for.rb
auto_html-1.3.4 lib/auto_html/auto_html_for.rb
auto_html-1.3.3 lib/auto_html/auto_html_for.rb
auto_html-1.3.2 lib/auto_html/auto_html_for.rb
auto_html-1.3.1 lib/auto_html/auto_html_for.rb
auto_html-1.3.0 lib/auto_html/auto_html_for.rb