Class BigRecord::Model
In: lib/big_record/model.rb
Parent: Object

Methods

==   ===   []   []=   alias_attribute   all_attributes_loaded=   all_attributes_loaded?   assign_multiparameter_attributes   attr_accessible   attr_create_accessible   attr_protected   attr_readonly   attribute=   attribute?   attribute_before_type_cast   attribute_for_inspect   attribute_names   attribute_present?   attributes   attributes=   attributes_from_column_definition   attributes_protected_by_default   base_class   benchmark   clone_in_persistence_format   column   column_for_attribute   column_names   columns   columns_hash   compute_type   connection   content_columns   create   create_accessible_attributes   create_column   create_or_update   default_column_prefix   default_columns   default_views   deserialize   destroy   eql?   execute_callstack_for_multiparameter_attributes   extract_callstack_for_multiparameter_attributes   find_parameter_position   freeze   frozen?   generate_new_id   has_attribute?   hash   human_attribute_name   id   id=   inspect   instantiate   invalidate_columns   invalidate_views   log_protected_attribute_removal   method_missing   new   new_record?   preinitialize   primary_key   quote_value   read_attribute   read_attribute_before_type_cast   readonly?   readonly_attributes   reload   remove_attributes_protected_from_mass_assignment   remove_readonly_attributes   respond_to?   safe_attributes=   save   save!   silence   store_primary_key?   to_s   type_cast_attribute_value   undecorated_table_name   update   update_attribute   update_attributes   update_attributes!   update_bigrecord   validate_attribute_type   validate_attributes_schema  

Constants

ID_FIELD_SEPARATOR = '-'   Constants for special characters in generated IDs. An ID might then look like this: ‘United_States-Hawaii-Oahu-Honolulu-b9cef848-a4e0-11dc-a7ba-0018f3137ea8‘
ID_WHITE_SPACE_CHAR = '_'

External Aliases

respond_to? -> respond_to_without_attributes?
  For checking respond_to? without searching the attributes (which is faster).

Public Class methods

Overwrite the default class equality method to provide support for association proxies.

Specifies a white list of model attributes that can be set via mass-assignment, such as new(attributes), update_attributes(attributes), or attributes=(attributes)

This is the opposite of the attr_protected macro: Mass-assignment will only set attributes in this list, to assign to the rest of attributes you can use direct writer methods. This is meant to protect sensitive attributes from being overwritten by malicious users tampering with URLs or forms. If you‘d rather start from an all-open default and restrict attributes as needed, have a look at attr_protected.

  class Customer < ActiveRecord::Base
    attr_accessible :name, :nickname
  end

  customer = Customer.new(:name => "David", :nickname => "Dave", :credit_rating => "Excellent")
  customer.credit_rating # => nil
  customer.attributes = { :name => "Jolly fellow", :credit_rating => "Superb" }
  customer.credit_rating # => nil

  customer.credit_rating = "Average"
  customer.credit_rating # => "Average"

Attributes listed as create_accessible work with mass assignment ONLY on creation. After that, any updates to that attribute will be protected from mass assignment. This differs from attr_readonly since that macro prevents attributes from ever being changed (even with the explicit setters) after the record is created.

  class Customer < BigRecord::Base
    attr_create_accessible :name
  end

  customer = Customer.new(:name => "Greg")
  customer.name # => "Greg"
  customer.save # => true

  customer.attributes = { :name => "Nerd" }
  customer.name # => "Greg"
  customer.name = "Nerd"
  customer.name # => "Nerd"

Attributes named in this macro are protected from mass-assignment, such as new(attributes), update_attributes(attributes), or attributes=(attributes).

Mass-assignment to these attributes will simply be ignored, to assign to them you can use direct writer methods. This is meant to protect sensitive attributes from being overwritten by malicious users tampering with URLs or forms.

  class Customer < ActiveRecord::Base
    attr_protected :credit_rating
  end

  customer = Customer.new("name" => David, "credit_rating" => "Excellent")
  customer.credit_rating # => nil
  customer.attributes = { "description" => "Jolly fellow", "credit_rating" => "Superb" }
  customer.credit_rating # => nil

  customer.credit_rating = "Average"
  customer.credit_rating # => "Average"

To start from an all-closed default and enable attributes as needed, have a look at attr_accessible.

Attributes listed as readonly can be set for a new record, but will be ignored in database updates afterwards.

Log and benchmark multiple statements in a single block.

@example

  Project.benchmark("Creating project") do
    project = Project.create("name" => "stuff")
    project.create_manager("name" => "David")
    project.milestones << Milestone.find(:all)
  end

The benchmark is only recorded if the current level of the logger matches the log_level, which makes it easy to include benchmarking statements in production software that will remain inexpensive because the benchmark will only be conducted if the log level is low enough.

The logging of the multiple statements is turned off unless use_silence is set to false.

Macro for defining a new column for a model. Invokes {create_column} and adds the new column into the model‘s column hash.

@param type [Symbol, String] Column type as defined in the source of {ConnectionAdapters::Column#klass} @option options [true,false] :collection Whether this column is a collection. @option options [String] :alias Define an alias for the column that cannot be inferred. By default, ‘attribute:name’ will be aliased to ‘name’. @option options [String] :default Default value to set for this column.

@return [ConnectionAdapters::Column] The column object created.

Returns an array of column names as strings.

Override this method in the subclasses to add new columns. This is different from ActiveRecord because the number of columns in an Hbase table is variable.

Returns a hash of column objects for the table associated with this class.

Returns an array of column objects where the primary id, all columns ending in "_id" or "_count", and columns used for single table inheritance have been removed.

Returns an array of all the attributes that have been specified as create_accessible.

Default column prefix used when auto-generating column attribute names

Finder methods must instantiate through this method.

New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names). In both instances, valid attribute keys are determined by the column names of the associated table — hence you can‘t have attributes that aren‘t part of the table columns.

@param [Hash] Optional hash argument consisting of keys that match the names of the columns, and their values.

Evaluate the name of the column of the primary key only once

Returns an array of all the attributes that have been specified as readonly.

Silences the logger for the duration of the block.

Guesses the table name, but does not decorate it with prefix and suffix information.

Protected Class methods

Returns the class type of the record using the current module as a prefix. So descendents of MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.

Public Instance methods

Returns true if the comparison_object is the same object, or is of the same type and has the same id.

Returns the value of the attribute identified by attr_name after it has been typecast (for example, "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). (Alias for the protected read_attribute method).

Updates the attribute identified by attr_name with the specified value. (Alias for the protected write_attribute method).

Returns an array of names for the attributes available on this object sorted alphabetically.

Returns true if the specified attribute has been set by the user or by a database load and is neither nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings).

@return [String, Symbol] Name of an attribute. @return [true,false] Whether that attribute exists.

Get the attributes hash of the object.

@return [Hash] a duplicated attributes hash

Allows you to set all the attributes at once by passing in a hash with keys matching the attribute names (which again matches the column names). Sensitive attributes can be protected from this form of mass-assignment by using the attr_protected macro. Or you can alternatively specify which attributes can be accessed in with the attr_accessible macro. Then all the attributes not included in that won‘t be allowed to be mass-assigned.

Overridden by FamilySpanColumns Returns the column object for the named attribute.

Returns the connection adapter of the current session.

Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can‘t be persisted).

Just freeze the attributes hash, such that associations are still accessible even on destroyed records.

Checks whether the object has had its attributes hash frozen.

@return [true,false]

Returns true if the given attribute is in the attributes hash

@return [String, Symbol] Name of an attribute. @return [true,false] Whether that attribute exists in the attributes hash.

Delegates to id in order to allow two records of the same type and id to work with something like:

  [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]

A model instance‘s primary key is always available as model.id whether you name it the default ‘id’ or set it to something else.

Sets the primary ID.

Returns the contents of the record as a nicely formatted string.

Returns true if this object hasn‘t been saved yet — that is, a record for the object doesn‘t yet exist in the data store.

Callback method meant to be overriden by subclasses if they need to preload some attributes before initializing the record. (usefull when using a meta model where the list of columns depends on the value of an attribute)

Records loaded through joins with piggy-back attributes will be marked as read only as they cannot be saved and return true to this query.

Reloads the attributes of this object from the database. The optional options argument is passed to find when reloading so you may do e.g. record.reload(:lock => true) to reload the same record with an exclusive row lock.

@return Itself with reloaded attributes.

A Person object with a name attribute can ask person.respond_to?("name"), person.respond_to?("name="), and person.respond_to?("name?") which will all return true.

Safe version of attributes= so that objects can be instantiated even if columns are removed.

@param [Hash] Attribute hash consisting of the column name and their values. @param [true, false] Pass the attributes hash through {remove_attributes_protected_from_mass_assignment}

  • No record exists: Creates a new record with values matching those of the object attributes.
  • A record does exist: Updates the record with values matching those of the object attributes.

Attempts to save the record, but instead of just returning false if it couldn‘t happen, it raises a RecordNotSaved exception

Default to_s method that just returns invokes the {id} method

@return [String] The row identifier/id of the record

Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records. Note: This method is overwritten by the Validation module that‘ll make sure that updates made with this method doesn‘t get subjected to validation checks. Hence, attributes can be updated even if the full object isn‘t valid.

Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned.

Updates an object just like Base.update_attributes but calls save! instead of save so an exception is raised if the record is invalid.

Protected Instance methods

Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done by calling new on the column type or aggregation type (through composed_of) object with these parameters. So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the parentheses to have the parameters typecasted before they‘re used in the constructor. Use i for Fixnum, f for Float, s for String, and a for Array. If all the values for a given attribute are empty, the attribute will be set to nil.

Initializes the attributes array with keys matching the columns from the linked table and the values matching the corresponding default value of that column, so that a new instance, or one populated from a passed-in Hash, still has all the attributes that instances loaded from the database would.

The primary key and inheritance column can never be set by mass-assignment for security reasons.

Creates a record with values matching those of the instance attributes and returns its id. Generate a UUID as the row key.

Includes an ugly hack for Time.local instead of Time.new because the latter is reserved by Time itself.

Generate a new id with the UUIDTools library. Override this method to use another id generator.

Allows access to the object attributes, which are held in the @attributes hash, as were they first-class methods. So a Person class with a name attribute can use Person#name and Person#name= and never directly use the attributes hash — except for multiple assigns with ActiveRecord#attributes=. A Milestone class can also ask Milestone#completed? to test that the completed attribute is not nil or 0.

It‘s also possible to instantiate related objects, so a Client class belonging to the clients table with a master_id foreign key can instantiate master through Client#master.

Quote strings appropriately for SQL statements.

Returns the value of the attribute identified by attr_name after it has been typecast (for example, "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).

Removes any attributes from the argument hash that have been declared as protected from mass assignment. See the {attr_protected} and {attr_accessible} macros to define these attributes.

Removes attributes which have been marked as readonly.

Updates the associated record with values matching those of the instance attributes. Returns the number of affected rows.

Update this record in hbase. Cannot be directly in the method ‘update’ because it would trigger callbacks and therefore weird behaviors.

Validate the type of the values in the attributes hash

[Validate]