Module: HasOptions

Defined in:
lib/adva/active_record/has_options.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#has_option(*names) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/adva/active_record/has_options.rb', line 2

def has_option(*names)
  unless respond_to?(:option_definitions)
    include InstanceMethods
    class_inheritable_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