Module DataMapper::Persistence::ClassMethods
In: lib/data_mapper/persistence.rb
lib/data_mapper/persistence.rb

Methods

Public Instance methods

An embedded value maps the values of an object to fields in the record of the object‘s owner. embed takes a symbol to define the embedded class, options, and an optional block. See examples for use cases.

EXAMPLE:

  class CellPhone < DataMapper::Base
    property :number, :string

    embed :owner, :prefix => true do
      property :name, :string
      property :address, :string
    end
  end

  my_phone = CellPhone.new
  my_phone.owner.name = "Nick"
  puts my_phone.owner.name

  => Nick

OPTIONS:

  • prefix: define a column prefix, so instead of mapping
                       :address to an 'address' column, it would map to
                       'owner_address' in the example above. If
                       :prefix => true is specified, the prefix will
                       be the name of the symbol given as the first
                       parameter. If the prefix is a string the
                       specified
                       string will be used for the prefix.
    
  • lazy: lazy-load all embedded values at the same time.
                       :lazy => true to enable. Disabled (false) by
                       default.
    
  • accessor: Set method visibility for all embedded
                       properties. Affects both reader and writer.
                       Allowable values are :public, :protected,
                       :private. Defaults to :public
    
  • reader: Like the accessor option but affects only
                       embedded property readers.
    
  • writer: Like the accessor option but affects only
                       embedded property writers.
    
  • protected: Alias for :reader => :public,
                        :writer => :protected
    
  • private: Alias for :reader => :public, :writer => :private

[Source]

     # File lib/data_mapper/persistence.rb, line 617
617:       def embed(name, options = {}, &block)
618:         EmbeddedValue::define(self, name, options, &block)
619:       end

An embedded value maps the values of an object to fields in the record of the object‘s owner. embed takes a symbol to define the embedded class, options, and an optional block. See examples for use cases.

EXAMPLE:

  class CellPhone < DataMapper::Base
    property :number, :string

    embed :owner, :prefix => true do
      property :name, :string
      property :address, :string
    end
  end

  my_phone = CellPhone.new
  my_phone.owner.name = "Nick"
  puts my_phone.owner.name

  => Nick

OPTIONS:

  • prefix: define a column prefix, so instead of mapping
                       :address to an 'address' column, it would map to
                       'owner_address' in the example above. If
                       :prefix => true is specified, the prefix will
                       be the name of the symbol given as the first
                       parameter. If the prefix is a string the
                       specified
                       string will be used for the prefix.
    
  • lazy: lazy-load all embedded values at the same time.
                       :lazy => true to enable. Disabled (false) by
                       default.
    
  • accessor: Set method visibility for all embedded
                       properties. Affects both reader and writer.
                       Allowable values are :public, :protected,
                       :private. Defaults to :public
    
  • reader: Like the accessor option but affects only
                       embedded property readers.
    
  • writer: Like the accessor option but affects only
                       embedded property writers.
    
  • protected: Alias for :reader => :public,
                        :writer => :protected
    
  • private: Alias for :reader => :public, :writer => :private

[Source]

     # File lib/data_mapper/persistence.rb, line 617
617:       def embed(name, options = {}, &block)
618:         EmbeddedValue::define(self, name, options, &block)
619:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 456
456:       def extended(klass)
457:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 456
456:       def extended(klass)
457:       end

The foreign key for a model. It is based on the lowercased and underscored name of the class, suffixed with _id.

  Widget.foreign_key    # => "widget_id"
  NewsItem.foreign_key  # => "news_item_id"

[Source]

     # File lib/data_mapper/persistence.rb, line 452
452:       def foreign_key
453:         Inflector.underscore(self.name) + "_id"
454:       end

The foreign key for a model. It is based on the lowercased and underscored name of the class, suffixed with _id.

  Widget.foreign_key    # => "widget_id"
  NewsItem.foreign_key  # => "news_item_id"

[Source]

     # File lib/data_mapper/persistence.rb, line 452
452:       def foreign_key
453:         Inflector.underscore(self.name) + "_id"
454:       end

Creates a composite index for an arbitrary number of database columns. Note that it also is possible to specify single indexes directly for each property.

EXAMPLE WITH COMPOSITE INDEX:

  class Person < DataMapper::Base
    property :server_id, :integer
    property :name, :string

    index [:server_id, :name]
  end

EXAMPLE WITH COMPOSITE UNIQUE INDEX:

  class Person < DataMapper::Base
    property :server_id, :integer
    property :name, :string

    index [:server_id, :name], :unique => true
  end

SINGLE INDEX EXAMPLES:

[Source]

     # File lib/data_mapper/persistence.rb, line 649
649:       def index(indexes, unique = false)
650:         if indexes.kind_of?(Array) # if given an index of multiple columns
651:           database.schema[self].add_composite_index(indexes, unique)
652:         else
653:           raise ArgumentError.new("You must supply an array for the composite index")
654:         end
655:       end

Creates a composite index for an arbitrary number of database columns. Note that it also is possible to specify single indexes directly for each property.

EXAMPLE WITH COMPOSITE INDEX:

  class Person < DataMapper::Base
    property :server_id, :integer
    property :name, :string

    index [:server_id, :name]
  end

EXAMPLE WITH COMPOSITE UNIQUE INDEX:

  class Person < DataMapper::Base
    property :server_id, :integer
    property :name, :string

    index [:server_id, :name], :unique => true
  end

SINGLE INDEX EXAMPLES:

[Source]

     # File lib/data_mapper/persistence.rb, line 649
649:       def index(indexes, unique = false)
650:         if indexes.kind_of?(Array) # if given an index of multiple columns
651:           database.schema[self].add_composite_index(indexes, unique)
652:         else
653:           raise ArgumentError.new("You must supply an array for the composite index")
654:         end
655:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 439
439:       def logger
440:         database.logger
441:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 439
439:       def logger
440:         database.logger
441:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 428
428:       def new_with_attributes(details)
429:         instance = allocate
430:         instance.initialize_with_attributes(details)
431:         instance
432:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 428
428:       def new_with_attributes(details)
429:         instance = allocate
430:         instance.initialize_with_attributes(details)
431:         instance
432:       end

Returns the hash of properties for this model.

[Source]

     # File lib/data_mapper/persistence.rb, line 622
622:       def properties
623:         @properties
624:       end

Returns the hash of properties for this model.

[Source]

     # File lib/data_mapper/persistence.rb, line 622
622:       def properties
623:         @properties
624:       end

Adds property accessors for a field that you‘d like to be able to modify. The DataMapper doesn‘t use the table schema to infer accessors, you must explicity call property to add field accessors to your model.

Can accept an unlimited amount of property names. Optionally, you may pass the property names as an array.

For more documentation, see Property.

EXAMPLE:

  class CellProvider
    property :name, :string
    property :rating_number, :rating_percent, :integer # will create two properties with same type and text
    property [:bill_to, :ship_to, :mail_to], :text, :lazy => false # will create three properties all with same type and text
  end

  att = CellProvider.new(:name => 'AT&T')
  att.rating = 3
  puts att.name, att.rating

  => AT&T
  => 3

OPTIONS:

  * <tt>lazy</tt>: Lazy load the specified property (:lazy => true). False by default.
  * <tt>accessor</tt>: Set method visibility for the property accessors. Affects both
  reader and writer. Allowable values are :public, :protected, :private. Defaults to
  :public
  * <tt>reader</tt>: Like the accessor option but affects only the property reader.
  * <tt>writer</tt>: Like the accessor option but affects only the property writer.
  * <tt>protected</tt>: Alias for :reader => :public, :writer => :protected
  * <tt>private</tt>: Alias for :reader => :public, :writer => :private

[Source]

     # File lib/data_mapper/persistence.rb, line 499
499:       def property(*columns_and_options)        
500:         columns, options = columns_and_options.partition {|item| not item.is_a?(Hash)}
501:         options = (options.empty? ? {} : options[0])
502:         type = columns.pop
503:       
504:         @properties ||= []
505:         new_properties = []
506:       
507:         columns.flatten.each do |name|
508:           property = DataMapper::Property.new(self, name, type, options)
509:           new_properties << property
510:           @properties << property
511:         end
512:               
513:         return (new_properties.length == 1 ? new_properties[0] : new_properties)
514:       end

Adds property accessors for a field that you‘d like to be able to modify. The DataMapper doesn‘t use the table schema to infer accessors, you must explicity call property to add field accessors to your model.

Can accept an unlimited amount of property names. Optionally, you may pass the property names as an array.

For more documentation, see Property.

EXAMPLE:

  class CellProvider
    property :name, :string
    property :rating_number, :rating_percent, :integer # will create two properties with same type and text
    property [:bill_to, :ship_to, :mail_to], :text, :lazy => false # will create three properties all with same type and text
  end

  att = CellProvider.new(:name => 'AT&T')
  att.rating = 3
  puts att.name, att.rating

  => AT&T
  => 3

OPTIONS:

  * <tt>lazy</tt>: Lazy load the specified property (:lazy => true). False by default.
  * <tt>accessor</tt>: Set method visibility for the property accessors. Affects both
  reader and writer. Allowable values are :public, :protected, :private. Defaults to
  :public
  * <tt>reader</tt>: Like the accessor option but affects only the property reader.
  * <tt>writer</tt>: Like the accessor option but affects only the property writer.
  * <tt>protected</tt>: Alias for :reader => :public, :writer => :protected
  * <tt>private</tt>: Alias for :reader => :public, :writer => :private

[Source]

     # File lib/data_mapper/persistence.rb, line 499
499:       def property(*columns_and_options)        
500:         columns, options = columns_and_options.partition {|item| not item.is_a?(Hash)}
501:         options = (options.empty? ? {} : options[0])
502:         type = columns.pop
503:       
504:         @properties ||= []
505:         new_properties = []
506:       
507:         columns.flatten.each do |name|
508:           property = DataMapper::Property.new(self, name, type, options)
509:           new_properties << property
510:           @properties << property
511:         end
512:               
513:         return (new_properties.length == 1 ? new_properties[0] : new_properties)
514:       end

TODO: Figure out how to make EmbeddedValue work with new property code. EV relies on these next two methods.

[Source]

     # File lib/data_mapper/persistence.rb, line 518
518:       def property_getter(mapping, visibility = :public)
519:         if mapping.lazy?
520:           class_eval "\#{visibility.to_s}\ndef \#{mapping.name}\nlazy_load!(\#{mapping.name.inspect})\nclass << self;\nattr_accessor \#{mapping.name.inspect}\nend\n@\#{mapping.name}\nend\n"
521:         else
522:           class_eval("#{visibility.to_s}; def #{mapping.name}; #{mapping.instance_variable_name} end") unless [ :public, :private, :protected ].include?(mapping.name)
523:         end
524: 
525:         if mapping.type == :boolean
526:           class_eval("#{visibility.to_s}; def #{mapping.name.to_s.ensure_ends_with('?')}; #{mapping.instance_variable_name} end")
527:         end
528: 
529:       rescue SyntaxError
530:         raise SyntaxError.new(mapping)
531:       end

TODO: Figure out how to make EmbeddedValue work with new property code. EV relies on these next two methods.

[Source]

     # File lib/data_mapper/persistence.rb, line 518
518:       def property_getter(mapping, visibility = :public)
519:         if mapping.lazy?
520:           class_eval "\#{visibility.to_s}\ndef \#{mapping.name}\nlazy_load!(\#{mapping.name.inspect})\nclass << self;\nattr_accessor \#{mapping.name.inspect}\nend\n@\#{mapping.name}\nend\n"
521:         else
522:           class_eval("#{visibility.to_s}; def #{mapping.name}; #{mapping.instance_variable_name} end") unless [ :public, :private, :protected ].include?(mapping.name)
523:         end
524: 
525:         if mapping.type == :boolean
526:           class_eval("#{visibility.to_s}; def #{mapping.name.to_s.ensure_ends_with('?')}; #{mapping.instance_variable_name} end")
527:         end
528: 
529:       rescue SyntaxError
530:         raise SyntaxError.new(mapping)
531:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 543
543:       def property_setter(mapping, visibility = :public)
544:         if mapping.lazy?
545:           class_eval "\#{visibility.to_s}\ndef \#{mapping.name}=(value)\nclass << self;\nattr_accessor \#{mapping.name.inspect}\nend\n@\#{mapping.name} = value\nend\n"
546:         else
547:           class_eval("#{visibility.to_s}; def #{mapping.name}=(value); #{mapping.instance_variable_name} = value end")
548:         end
549:       rescue SyntaxError
550:         raise SyntaxError.new(mapping)
551:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 543
543:       def property_setter(mapping, visibility = :public)
544:         if mapping.lazy?
545:           class_eval "\#{visibility.to_s}\ndef \#{mapping.name}=(value)\nclass << self;\nattr_accessor \#{mapping.name.inspect}\nend\n@\#{mapping.name} = value\nend\n"
546:         else
547:           class_eval("#{visibility.to_s}; def #{mapping.name}=(value); #{mapping.instance_variable_name} = value end")
548:         end
549:       rescue SyntaxError
550:         raise SyntaxError.new(mapping)
551:       end

Allows you to override the table name for a model. EXAMPLE:

  class WorkItem
    set_table_name 't_work_item_list'
  end

[Source]

     # File lib/data_mapper/persistence.rb, line 567
567:       def set_table_name(value)
568:         database.table(self).name = value
569:       end

Allows you to override the table name for a model. EXAMPLE:

  class WorkItem
    set_table_name 't_work_item_list'
  end

[Source]

     # File lib/data_mapper/persistence.rb, line 567
567:       def set_table_name(value)
568:         database.table(self).name = value
569:       end

Track classes that include this module.

[Source]

     # File lib/data_mapper/persistence.rb, line 435
435:       def subclasses
436:         @subclasses || (@subclasses = [])
437:       end

Track classes that include this module.

[Source]

     # File lib/data_mapper/persistence.rb, line 435
435:       def subclasses
436:         @subclasses || (@subclasses = [])
437:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 459
459:       def table
460:         database.table(self)
461:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 459
459:       def table
460:         database.table(self)
461:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 443
443:       def transaction
444:         yield
445:       end

[Source]

     # File lib/data_mapper/persistence.rb, line 443
443:       def transaction
444:         yield
445:       end

[Validate]