Sha256: 17f0dd2d594590cbd749cc8f607629eb7974822a4ab386b68e3ab5d89d31c8fe

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

module HasOptions
  def has_option(*names)
    unless respond_to?(:option_definitions)
      include InstanceMethods
      cattr_accessor :option_definitions
      self.option_definitions ||= {}
      serialize :options
    end

    definition = names.extract_options!
    names.map(&:to_sym).each do |name|
      self.option_definitions[name] = definition

      class_eval <<-rb, __FILE__, __LINE__
        def #{name}
          self.options ||= {}
          options.key?(:#{name}) ? options[:#{name}] : option_default(:#{name})
        end
        # alias #{name}_before_type_cast #{name}

        def #{name}=(value)
          options_will_change!
          self.options ||= {}
          options[:#{name}] = option_type_cast(:#{name}, value)
        end
      rb
    end
  end

  module InstanceMethods
    OPTION_DEFAULTS = { :default => nil, :type => :string }

    [:type, :default].each do |reader|
      define_method(:"option_#{reader}") do |name|
        self.class.option_definitions[name].key?(reader) ? self.class.option_definitions[name][reader] : OPTION_DEFAULTS[reader]
      end
    end

    define_method :option_type_cast do |name, value|
      return nil if value.nil?
      case option_type(name)
        when :string    then value.to_s
        when :integer   then value.to_i rescue value ? 1 : 0
        when :float     then value.to_f
        when :datetime  then ActiveRecord::ConnectionAdapters::Column.string_to_time(value)
        when :time      then ActiveRecord::ConnectionAdapters::Column.string_to_dummy_time(value)
        when :date      then ActiveRecord::ConnectionAdapters::Column.string_to_date(value)
        when :boolean   then ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
        else value
      end
    end
  end
end

ActiveRecord::Base.extend(HasOptions)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adva-core-0.0.2 lib/adva/active_record/has_options.rb
adva-core-0.0.1 lib/adva/active_record/has_options.rb