Module: Property::Declaration::InstanceMethods

Defined in:
lib/property/declaration.rb

Overview

ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (protected)

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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

Instance Method Details

#has_role?(role) ⇒ Boolean

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

Returns:

  • (Boolean)


111
112
113
# File 'lib/property/declaration.rb', line 111

def has_role?(role)
  own_schema.has_role? role
end

#include_role(role) ⇒ Object

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



100
101
102
# File 'lib/property/declaration.rb', line 100

def include_role(role)
  own_schema.include_role role
end

#schemaObject

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



94
95
96
# File 'lib/property/declaration.rb', line 94

def schema
  @own_schema || self.class.schema
end

#used_rolesObject

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



106
107
108
# File 'lib/property/declaration.rb', line 106

def used_roles
  own_schema.used_roles_in(self)
end