Module: Poro::Modelify

Defined in:
lib/poro/modelify.rb

Overview

When this module is mixed into a a persistent object, it adds the object oriented methods directly to the class, making it behave like a model in other ORMs.

If this module is not mixed in, the object can still persist, but the context must be directly accessed to fetch, save, and remove the data.

Additionally, this module also ensures that the appropriate primary key accessor methods exist on the object, creating them if necessary.

WARNING: You should configure the primary keys on the class’ Context before including this module if you want to use an accessor other than the Context’s default.

See Modelify::ClassMethods and Modelify::InstanceMethods for the methods added by this mixin.

TODO: Piecewise Modelfication

Modelfication should be done in a piece-meal way, so that we can layer in features such as pk accesor generation, basic model methods, find methods, and hook methods. We may still keep this top-level include to add the full suite, but we should break up the pieces. (In doing this, we could to manage dependencies so that we get the basic pieces included in when we need things they depend on, but by not managing this, it gives more flexability for advanced users to replace segments.

Defined Under Namespace

Modules: ClassMethods, FindMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(mod) ⇒ Object

:nodoc:

Raises:

  • (NameError)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/poro/modelify.rb', line 30

def self.included(mod) # :nodoc:
  mod.send(:extend, ClassMethods)
  mod.send(:include, InstanceMethods)
  mod.send(:include, FindMethods)
  
  context = Context.fetch(mod)
  pk = context.primary_key.to_s.gsub(/[^A-Za-z0-9]/, '_').strip
  raise NameError, "Cannot create a primary key method from #{context.primary_key.inspect}" if pk.nil? || pk.empty?
  mod.class_eval("def #{pk}; return @#{pk}; end")
  mod.class_eval("def #{pk}=(value); @#{pk} = value; end")
end