Class: Historical::ClassBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/historical/class_builder.rb

Overview

Builds the customized classes for a record.

Instance Attribute Summary collapse

Builder Methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ ClassBuilder

Returns a new instance of ClassBuilder.

Parameters:

  • base

    The record on which the customized classes should be based on



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/historical/class_builder.rb', line 21

def initialize(base)
  self.callbacks = []
  
  self.version_class  = Historical::Models::ModelVersion.for_class(base)
  self.diff_class     = Historical::Models::ModelVersion::Diff.for_class(base)
  self.meta_class     = Historical::Models::ModelVersion::Meta.for_class(base)
  
  base.historical_customizations.each do |customization|
    instance_eval(&customization)
  end
end

Instance Attribute Details

#callbacksArray<Proc>

Returns A list of callbacks to be evaluated on save.

Returns:

  • (Array<Proc>)

    A list of callbacks to be evaluated on save.



6
7
8
# File 'lib/historical/class_builder.rb', line 6

def callbacks
  @callbacks
end

#diff_classClass

Returns A customized subclass of Models::ModelVersion::Diff.

Returns:



14
15
16
# File 'lib/historical/class_builder.rb', line 14

def diff_class
  @diff_class
end

#meta_classClass

Returns A customized subclass of Models::ModelVersion::Meta.

Returns:



10
11
12
# File 'lib/historical/class_builder.rb', line 10

def meta_class
  @meta_class
end

#version_classClass

Returns A customized subclass of Models::ModelVersion.

Returns:



18
19
20
# File 'lib/historical/class_builder.rb', line 18

def version_class
  @version_class
end

Instance Method Details

#callback {|version| ... } ⇒ Object

A callback to be called when a new version is spawned (i.e. model was saved with changes)

Examples:

Usage within is_historical

is_historical do
  callback do |version|
    version.meta.some_key = "foo"
  end
end

Yields:

  • (version)

    Your callback

Yield Parameters:



56
57
58
# File 'lib/historical/class_builder.rb', line 56

def callback(&block)
  self.callbacks << block
end

#meta(&block) ⇒ Object

Evaluated within class scope of the custom Models::ModelVersion::Meta for this record. The Meta-class includes ‘MongoMapper::EmbeddedDocument` and MongoMapperEnhancements.

Examples:

Usage within is_historical

is_historical do
  meta do
    key :some_key, String
  end
end


43
44
45
# File 'lib/historical/class_builder.rb', line 43

def meta(&block)
  meta_class.instance_eval(&block)
end