Sha256: 8a0e82921b7d5c542d080a1cdba6c3bb61a93056b12db0a537db86f75ea16951

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

# encoding: utf-8
module AutoHtml
  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.send(:extend, ClassMethods)
    end

    module ClassMethods
      def auto_html_for(raw_attrs, &proc)
        include AutoHtml::Base::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

        [raw_attrs].flatten.each do |raw_attr|
          define_method("auto_html_prepare_#{raw_attr}") do
            suffix = AutoHtmlFor.auto_html_for_options[:htmlized_attribute_suffix]
            self.send("#{raw_attr}#{suffix}=", auto_html(self.send(raw_attr), &proc))
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
galetahub-auto_html-1.0.1 lib/auto_html/auto_html_for.rb