Module: StateFu::Persistence

Defined in:
lib/persistence.rb,
lib/persistence/base.rb,
lib/persistence/relaxdb.rb,
lib/persistence/session.rb,
lib/persistence/attribute.rb,
lib/persistence/active_record.rb

Overview

class StateFu::Persistence::MagneticCarpet < StateFu::Persistence::Base

  def prepare_field

  def read_attribute
    object.send "magnetised_#{field_name}"
  end

  def write_attribute( string_value )
    Logging.debug "magnetising ( #{field_name} => #{string_value} on #{object.inspect}"
    object.send "magnetised_#{field_name}=", string_value
  end
end

Defined Under Namespace

Classes: ActiveRecord, Attribute, Base, RelaxDB, Session

Constant Summary collapse

DEFAULT_SUFFIX =
'_field'
@@class_for =
{}
@@fields_prepared =
{}

Class Method Summary collapse

Class Method Details

.active_record_column?(klass, field_name) ⇒ Boolean

checks to see if the field_name for persistence is an ActiveRecord column. Safe to use (skipped) if ActiveRecord is not included.

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/persistence.rb', line 107

def self.active_record_column?(klass, field_name)
  Object.const_defined?("ActiveRecord") &&
    ::ActiveRecord.const_defined?("Base") &&
    klass.ancestors.include?(::ActiveRecord::Base) &&
    klass.table_exists? &&
    klass.columns.map(&:name).include?(field_name.to_s)
end

.class_for(klass, field_name) ⇒ Object

returns the appropriate persister class for the given class & field name.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/persistence.rb', line 42

def self.class_for(klass, field_name)
  raise ArgumentError if [klass, field_name].any?(&:nil?)
  @@class_for[klass] ||= {}
  @@class_for[klass][field_name] ||=
    if active_record_column?( klass, field_name )
      self::ActiveRecord
    elsif relaxdb_document_property?( klass, field_name )
      self::RelaxDB
    else
      self::Attribute
    end
end

.default_field_name(machine_name) ⇒ Object

Class Methods



37
38
39
# File 'lib/persistence.rb', line 37

def self.default_field_name( machine_name )
  machine_name == DEFAULT ? DEFAULT_FIELD : "#{machine_name.to_s.underscore.tr(' ','_')}#{DEFAULT_SUFFIX}"
end

.for(binding) ⇒ Object

returns a new persister appropriate to the given binding and field_name also ensures the persister class method :prepare_field has been called once for the given class & field name so the field can be set up; eg an attr_accessor or a before_save hook defined



72
73
74
75
76
77
78
79
# File 'lib/persistence.rb', line 72

def self.for(binding)
  field_name = binding.field_name.to_sym
  if binding.singleton?
    for_instance( binding, field_name )
  else
    for_class( binding.target, binding, field_name )
  end
end

.for_class(klass, binding, field_name) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/persistence.rb', line 55

def self.for_class(klass, binding, field_name)
  persister_class = class_for klass, field_name
  prepare_field( klass, field_name, persister_class)
  returning persister_class.new( binding, field_name ) do |persister|
    Logging.debug( "#{persister_class}: method #{binding.method_name} as field #{persister.field_name}" )
  end
end

.for_instance(binding, field_name) ⇒ Object



63
64
65
66
# File 'lib/persistence.rb', line 63

def self.for_instance(binding, field_name)
  metaclass = class << binding.object; self; end
  for_class( metaclass, binding, field_name )
end

.prepare_field(klass, field_name, persister_class = nil) ⇒ Object

ensures that <persister_class>.prepare_field is called only once



82
83
84
85
86
87
88
89
# File 'lib/persistence.rb', line 82

def self.prepare_field(klass, field_name, persister_class=nil)
  @@fields_prepared[klass] ||= []
  unless @@fields_prepared[klass].include?(field_name)
    persister_class ||= class_for(klass, field_name)
    persister_class.prepare_field( klass, field_name )
    @@fields_prepared[klass] << field_name
  end
end

.relaxdb_document_property?(klass, field_name) ⇒ Boolean

checks to see if the field_name for persistence is a RelaxDB attribute. Safe to use (skipped) if RelaxDB is not included.

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/persistence.rb', line 98

def self.relaxdb_document_property?(klass, field_name)
  Object.const_defined?('RelaxDB') &&
    klass.ancestors.include?( ::RelaxDB::Document ) &&
    klass.properties.map(&:to_s).include?(field_name.to_s)
end