Module: Ccls::ActiveRecordExtension::Base::ClassMethods

Defined in:
lib/ccls_engine/active_record_extension/base.rb

Instance Method Summary collapse

Instance Method Details

#acts_like_a_hash(*args) ⇒ Object

[View source]

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ccls_engine/active_record_extension/base.rb', line 10

def acts_like_a_hash(*args)
  options = {
    :key   => :key,
    :value => :description
  }.update(args.extract_options!)

  class_eval do

    validates_presence_of   options[:key], options[:value]
    validates_uniqueness_of options[:key], options[:value]
    validates_length_of     options[:key], options[:value],
      :maximum => 250, :allow_blank => true

    # Treats the class a bit like a Hash and
    # searches for a record with a matching key.
    def self.[](key)
      # may need to deal with options[:key] here, but not needed yet
      find_by_key(key.to_s)
    end

  end  #  class_eval do
end