Module: Persisted::ClassMethods

Defined in:
lib/persisted.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/persisted.rb', line 59

def method_missing( method_name, *args )
  if respond_to_missing? method_name
    persisted_record_class.send method_name, *args
  else
    super
  end
end

Instance Attribute Details

#persisted_record_classObject

Returns the value of attribute persisted_record_class.



31
32
33
# File 'lib/persisted.rb', line 31

def persisted_record_class
  @persisted_record_class
end

Instance Method Details

#persist_with(model_name, options = {}) ⇒ Object

Instructs the persistence layer which ORM-backed class to use to persist the model.

Usage:

persist_with :user, attributes: %w{username password}

Configuration options: attributes: An array of shared attributes between models used for DB

interactions. Basically, use this to find the appropriate record.
Defaults to ['id']


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/persisted.rb', line 43

def persist_with( model_name, options = {} )
  self.persisted_record_class = model_name.to_s.camelize.constantize

  if options && options.is_a?( Hash )
    options.symbolize_keys!

    if options[:attributes].is_a?( Array )
      options[:attributes] ||= ['id']

      @shared_attributes = options[:attributes]
    end
  else
    raise ArgumentError, 'An array of shared attributes must be supplied as the :attributes option of the configuration hash'
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean



67
68
69
# File 'lib/persisted.rb', line 67

def respond_to_missing?( method_name, include_private = false )
  persisted_record_class.respond_to? method_name
end