Module: Property::RoleModule
- Included in:
- Role, StoredRole
- Defined in:
- lib/property/role_module.rb
Overview
The RoleModule enables a class to hold information on a group of property columns. This enables classes to act in the same way as the ruby Module: as a mixin. The Schema class “includes” roles.
Instance Method Summary collapse
-
#column_names ⇒ Object
Return the list of column names.
-
#columns ⇒ Object
List all property columns defined for this role.
-
#defined_columns ⇒ Object
List all property columns defined for this role.
-
#defined_indices ⇒ Object
Return a list of index definitions from the defined columns in the form [type, key, proc_or_nil].
-
#has_column?(name) ⇒ Boolean
Return true if the role contains the given column (property).
-
#index(type, &block) ⇒ Object
This is used to create complex indices with the following syntax:.
- #inspect ⇒ Object
- #name ⇒ Object
-
#property ⇒ Object
Use this method to declare properties into a Role or Schema.
-
#serialize(name, klass, options = {}) ⇒ Object
This is used to serialize a non-native DB type.
-
#used_in(object) ⇒ Object
Returns true if the role is used by the given object.
-
#used_keys_in(object) ⇒ Object
Returns the list of column names in the current role that are used by the given object (value not blank).
Instance Method Details
#column_names ⇒ Object
Return the list of column names.
16 17 18 |
# File 'lib/property/role_module.rb', line 16 def column_names columns.keys end |
#columns ⇒ Object
List all property columns defined for this role
21 22 23 |
# File 'lib/property/role_module.rb', line 21 def columns defined_columns end |
#defined_columns ⇒ Object
List all property columns defined for this role
108 109 110 |
# File 'lib/property/role_module.rb', line 108 def defined_columns @defined_columns ||= {} end |
#defined_indices ⇒ Object
Return a list of index definitions from the defined columns in the form [type, key, proc_or_nil]
94 95 96 97 98 99 100 |
# File 'lib/property/role_module.rb', line 94 def defined_indices defined_columns.values.select do |c| c.indexed? end.map do |c| [c.index, c.name, c.index_proc] end + group_indices end |
#has_column?(name) ⇒ Boolean
Return true if the role contains the given column (property).
11 12 13 |
# File 'lib/property/role_module.rb', line 11 def has_column?(name) column_names.include?(name) end |
#index(type, &block) ⇒ Object
This is used to create complex indices with the following syntax:
p.index(:text) do |r| # r = record
{
"high" => "gender:#{r.gender} age:#{r.age} name:#{r.name}",
"name_#{r.lang}" => r.name, # multi-lingual index
}
end
The first argument is the type (used to locate the table where the data will be stored) and the block will be yielded with the record and should return a hash of key => value pairs.
75 76 77 78 |
# File 'lib/property/role_module.rb', line 75 def index(type, &block) # type, key, proc group_indices << [type, nil, block] end |
#inspect ⇒ Object
102 103 104 105 |
# File 'lib/property/role_module.rb', line 102 def inspect # "#<#{self.class}:#{sprintf("0x%x", object_id)} #{@name.inspect} @klass = #{@klass.inspect} @defined_columns = #{@defined_columns.inspect}>" "#<#{self.class}:'#{name}' #{defined_columns.keys.join(', ')}>" end |
#name ⇒ Object
6 7 8 |
# File 'lib/property/role_module.rb', line 6 def name @name end |
#property ⇒ Object
Use this method to declare properties into a Role or Schema.
Example:
@role.property.string 'phone', :default => ''
You can also use the “property” method in the class to access the schema:
Example:
Page.property.string 'phone', :default => ''
You can also use a block:
Page.property do |p|
p.string 'phone', 'name', :default => ''
end
39 40 41 42 43 44 |
# File 'lib/property/role_module.rb', line 39 def property if block_given? yield self end self end |
#serialize(name, klass, options = {}) ⇒ Object
This is used to serialize a non-native DB type. Use:
p.serialize 'pet', Dog
59 60 61 62 |
# File 'lib/property/role_module.rb', line 59 def serialize(name, klass, = {}) Property.validate_property_class(klass) add_column(Property::Column.new(name, nil, klass, .merge(:role => self))) end |
#used_in(object) ⇒ Object
Returns true if the role is used by the given object. A role is considered to be used if any of it’s defined columns is not blank in the object’s properties.
83 84 85 |
# File 'lib/property/role_module.rb', line 83 def used_in(object) object.properties.keys & defined_columns.keys != [] end |
#used_keys_in(object) ⇒ Object
Returns the list of column names in the current role that are used by the given object (value not blank).
89 90 91 |
# File 'lib/property/role_module.rb', line 89 def used_keys_in(object) object.properties.keys & column_names end |