module Property::Declaration::InstanceMethods

Public Instance Methods

has_role?(role) click to toggle source

Return true if the current object has all the roles of the given object, class or role.

# File lib/property/declaration.rb, line 111
def has_role?(role)
  own_schema.has_role? role
end
include_role(role) click to toggle source

Include a new set of property definitions (Role) into the current instance’s schema. You can also provide a class to simulate multiple inheritance.

# File lib/property/declaration.rb, line 100
def include_role(role)
  own_schema.include_role role
end
schema() click to toggle source

Instance’s schema (can be different from the instance’s class schema if roles have been added to the instance.

# File lib/property/declaration.rb, line 94
def schema
  @own_schema || self.class.schema
end
used_roles() 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/declaration.rb, line 106
def used_roles
  own_schema.used_roles_in(self)
end

Protected Instance Methods

method_missing(meth, *args, &block) click to toggle source

When roles are dynamically added to a model, we use method_missing to mimic property accessors. Since this has a cost, it is better to use ‘prop’ based accessors in production code (this is mostly helpful for testing/debugging).

Calls superclass method
# File lib/property/declaration.rb, line 127
def method_missing(meth, *args, &block)
  method = meth.to_s
  if args.empty?
    if method[-1..-1] == '?'
      # predicate
      key = method[0..-2]
    else
      # reader
      key = method
    end

    if schema.has_column?(key)
      return prop[key]
    end
  elsif args.size == 1 && method[-1..-1] == '='
    # writer
    key = method[0..-2]
    if schema.has_column?(key)
      return prop[key] = args.first
    end
  end
  # Not a property method
  super
end
own_schema() click to toggle source
# File lib/property/declaration.rb, line 120
def own_schema
  @own_schema ||= make_own_schema
end
properties_validation() click to toggle source
# File lib/property/declaration.rb, line 116
def properties_validation
  properties.validate
end

Private Instance Methods

make_own_schema() click to toggle source

Create a schema for the instance and inherit from the class

# File lib/property/declaration.rb, line 154
def make_own_schema
  Property::Schema.new(nil, :superschema => self.class.schema)
end