Module: DataMapper::Persistence::ClassMethods
- Defined in:
- lib/data_mapper/persistence.rb
Instance Method Summary collapse
-
#embed(name, options = {}, &block) ⇒ Object
An embedded value maps the values of an object to fields in the record of the object’s owner.
- #extended(klass) ⇒ Object
-
#foreign_key ⇒ Object
The foreign key for a model.
-
#index(indexes, unique = false) ⇒ Object
Creates a composite index for an arbitrary number of database columns.
- #logger ⇒ Object
- #new_with_attributes(details) ⇒ Object
-
#properties ⇒ Object
Returns the hash of properties for this model.
-
#property(*columns_and_options) ⇒ Object
Adds property accessors for a field that you’d like to be able to modify.
-
#property_getter(mapping, visibility = :public) ⇒ Object
TODO: Figure out how to make EmbeddedValue work with new property code.
- #property_setter(mapping, visibility = :public) ⇒ Object
-
#set_table_name(value) ⇒ Object
Allows you to override the table name for a model.
-
#subclasses ⇒ Object
Track classes that include this module.
- #table ⇒ Object
- #transaction ⇒ Object
Instance Method Details
#embed(name, options = {}, &block) ⇒ Object
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
: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 embeddedproperties. Affects both reader and writer. Allowable values are :public, :protected, :private. Defaults to :public -
reader: Like the accessor option but affects onlyproperty readers. -
writer: Like the accessor option but affects onlyproperty writers. -
protected: Alias for :reader => :public,:writer => :protected -
private: Alias for :reader => :public, :writer => :private
615 616 617 |
# File 'lib/data_mapper/persistence.rb', line 615 def (name, = {}, &block) EmbeddedValue::define(self, name, , &block) end |
#extended(klass) ⇒ Object
456 457 |
# File 'lib/data_mapper/persistence.rb', line 456 def extended(klass) end |
#foreign_key ⇒ Object
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"
452 453 454 |
# File 'lib/data_mapper/persistence.rb', line 452 def foreign_key Inflector.underscore(self.name) + "_id" end |
#index(indexes, unique = false) ⇒ Object
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:
-
property :name, :index => true
-
property :name, :index => :unique
647 648 649 650 651 652 653 |
# File 'lib/data_mapper/persistence.rb', line 647 def index(indexes, unique = false) if indexes.kind_of?(Array) # if given an index of multiple columns database.schema[self].add_composite_index(indexes, unique) else raise ArgumentError.new("You must supply an array for the composite index") end end |
#logger ⇒ Object
439 440 441 |
# File 'lib/data_mapper/persistence.rb', line 439 def logger database.logger end |
#new_with_attributes(details) ⇒ Object
428 429 430 431 432 |
# File 'lib/data_mapper/persistence.rb', line 428 def new_with_attributes(details) instance = allocate instance.initialize_with_attributes(details) instance end |
#properties ⇒ Object
Returns the hash of properties for this model.
620 621 622 |
# File 'lib/data_mapper/persistence.rb', line 620 def properties @properties end |
#property(*columns_and_options) ⇒ Object
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. = 3
puts att.name, att.
=> 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
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/data_mapper/persistence.rb', line 499 def property(*) columns, = .partition {|item| not item.is_a?(Hash)} = (.empty? ? {} : [0]) type = columns.pop @properties ||= [] new_properties = [] columns.flatten.each do |name| property = DataMapper::Property.new(self, name, type, ) new_properties << property @properties << property end return (new_properties.length == 1 ? new_properties[0] : new_properties) end |
#property_getter(mapping, visibility = :public) ⇒ Object
TODO: Figure out how to make EmbeddedValue work with new property code. EV relies on these next two methods.
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/data_mapper/persistence.rb', line 518 def property_getter(mapping, visibility = :public) if mapping.lazy? class_eval " \#{visibility.to_s}\n def \#{mapping.name}\n lazy_load!(\#{mapping.name.inspect})\n class << self;\n attr_accessor \#{mapping.name.inspect}\n end\n @\#{mapping.name}\n end\n EOS\n else\n class_eval(\"\#{visibility.to_s}; def \#{mapping.name}; \#{mapping.instance_variable_name} end\") unless [ :public, :private, :protected ].include?(mapping.name)\n end\n\n if mapping.type == :boolean\n class_eval(\"\#{visibility.to_s}; def \#{mapping.name.to_s.ensure_ends_with('?')}; \#{mapping.instance_variable_name} end\")\n end\n\nrescue SyntaxError\n raise SyntaxError.new(mapping)\nend\n" |
#property_setter(mapping, visibility = :public) ⇒ Object
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
# File 'lib/data_mapper/persistence.rb', line 542 def property_setter(mapping, visibility = :public) if mapping.lazy? class_eval " \#{visibility.to_s}\n def \#{mapping.name}=(value)\n class << self;\n attr_accessor \#{mapping.name.inspect}\n end\n @\#{mapping.name} = value\n end\n EOS\n else\n class_eval(\"\#{visibility.to_s}; def \#{mapping.name}=(value); \#{mapping.instance_variable_name} = value end\")\n end\nrescue SyntaxError\n raise SyntaxError.new(mapping)\nend\n" |
#set_table_name(value) ⇒ Object
Allows you to override the table name for a model. EXAMPLE:
class WorkItem
set_table_name 't_work_item_list'
end
565 566 567 |
# File 'lib/data_mapper/persistence.rb', line 565 def set_table_name(value) database.table(self).name = value end |
#subclasses ⇒ Object
Track classes that include this module.
435 436 437 |
# File 'lib/data_mapper/persistence.rb', line 435 def subclasses @subclasses || (@subclasses = []) end |
#table ⇒ Object
459 460 461 |
# File 'lib/data_mapper/persistence.rb', line 459 def table database.table(self) end |
#transaction ⇒ Object
443 444 445 |
# File 'lib/data_mapper/persistence.rb', line 443 def transaction yield end |