Module: Property::SchemaModule

Included in:
Schema, StoredSchema
Defined in:
lib/property/schema_module.rb

Overview

The SchemaModule enables a class to act as a Schema.

Instance Method Summary collapse

Instance Method Details

#add_column(column) ⇒ Object

When a column is added in a Schema: define accessors in related class



92
93
94
95
96
97
# File 'lib/property/schema_module.rb', line 92

def add_column(column)
  super
  if @klass
    @klass.define_property_methods(column) if column.should_create_accessors?
  end
end

#columnsObject

Return a hash with the column definitions defined in the schema and in the included roles.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/property/schema_module.rb', line 57

def columns
  # FIXME: can we memoize this list on the first call ? Do we need to update properties after such a call ?
  # @columns ||=
  begin
    res = {}
    @roles.flatten.uniq.each do |role|
      # TODO: we could check for property redefinitions.
      res.merge!(role.defined_columns)
    end
    res
  end
end

#has_role?(role) ⇒ Boolean

Return true if the role has been included or is included in any superclass.

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/property/schema_module.rb', line 79

def has_role?(role)
  if role.kind_of?(Schema)
    role.roles.flatten - @roles.flatten == []
  elsif role.kind_of?(RoleModule)
    @roles.flatten.include?(role)
  elsif role.respond_to?(:schema) && role.schema.kind_of?(Role)
    has_role?(role.schema)
  else
    false
  end
end

#include_role(role) ⇒ Object

Add a set of property definitions to the schema.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/property/schema_module.rb', line 30

def include_role(role)
  # @columns = nil # clear cache
  if role.kind_of?(SchemaModule)
    # Superclass inheritance
    @roles << role.roles
  elsif role.kind_of?(RoleModule)
    @roles << role
  elsif role.respond_to?(:schema) && role.schema.kind_of?(Role)
    @roles << role.schema.roles
  else
    raise TypeError.new("Cannot include role #{role} (invalid type).")
  end
end

#index_groupsObject

Return a hash with indexed types as keys and index definitions as values.



45
46
47
48
49
50
51
52
53
# File 'lib/property/schema_module.rb', line 45

def index_groups
  index_groups = {}
  @roles.flatten.uniq.each do |b|
    b.defined_indices.each do |list|
      (index_groups[list.first] ||= []) << list[1..-1]
    end
  end
  index_groups
end

#initialize_schema_module(opts) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/property/schema_module.rb', line 4

def initialize_schema_module(opts)
  @klass = opts[:class]

  @roles = [self]

  # Schema inheritance
  unless superschema = opts[:superschema]
    if @klass && @klass.superclass.respond_to?(:schema)
      superschema = @klass.superclass.schema
    end
  end

  if superschema
    include_role superschema
  end
end

#klassObject



21
22
23
# File 'lib/property/schema_module.rb', line 21

def klass
  @klass
end

#rolesObject



25
26
27
# File 'lib/property/schema_module.rb', line 25

def roles
  @roles
end

#used_roles_in(object) ⇒ Object

Return the list of active roles. The active roles are all the Roles included in the current object for which properties have been defined (not blank).



72
73
74
75
76
# File 'lib/property/schema_module.rb', line 72

def used_roles_in(object)
  roles.flatten.uniq.select do |role|
    role.used_in(object)
  end
end