Module: Property::StoredRole

Includes:
RoleModule
Defined in:
lib/property/stored_role.rb

Overview

This module lets you use a custom class to store a set of property definitions inside the database. For the rest, this class behaves just like Role.

Once this module is included, you need to set the has_many association to the class that contains the columns definitions with something like:

stored_columns_class NameOfColumnsClass

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RoleModule

#column_names, #columns, #defined_indices, #has_column?, #index, #inspect, #serialize, #used_in, #used_keys_in

Class Method Details

.included(base) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/property/stored_role.rb', line 21

def self.included(base)
  base.class_eval do
    after_save :update_columns
    validates_presence_of :name

    extend ClassMethods

    def self.new(arg, &block)
      unless arg.kind_of?(Hash)
        arg = {:name => arg}
      end

      if block_given?
        obj = super(arg) do
          # Dummy block to hide our special property declaration block
        end

        obj.property(&block)
      else
        obj = super(arg)
      end

      obj
    end
  end
end

Instance Method Details

#defined_columnsObject

Get all property definitions defined for this role



49
50
51
52
# File 'lib/property/stored_role.rb', line 49

def defined_columns
  load_columns_from_db unless @columns_from_db_loaded
  super
end

#nameObject

Overwrite name reader in RoleModule



59
60
61
# File 'lib/property/stored_role.rb', line 59

def name
  self[:name]
end

#propertyObject



54
55
56
# File 'lib/property/stored_role.rb', line 54

def property
  super
end