module Property::SchemaModule

The SchemaModule enables a class to act as a Schema.

Public Instance Methods

add_column(column) click to toggle source

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

Calls superclass method
# 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
columns() click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

Add a set of property definitions to the schema.

# 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_groups() click to toggle source

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

# 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) click to toggle source
# 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
klass() click to toggle source
# File lib/property/schema_module.rb, line 21
def klass
  @klass
end
roles() click to toggle source
# File lib/property/schema_module.rb, line 25
def roles
  @roles
end
used_roles_in(object) click to toggle source

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).

# 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