Class Ruport::Data::Record
In: lib/ruport/data/record.rb
Parent: Object

Overview

Data::Records are the work-horse of Ruport‘s data model. These can behave as Array-like, Hash-like, or Struct-like objects. They are used as the base element for Data::Table

Methods

==   []   []=   attributes   each   eql?   get   hash   length   method_missing   new   rename_attribute   reorder   size   to_a   to_hash  

Included Modules

Enumerable Ruport::Renderer::Hooks

Attributes

attributes  [W]  Sets the attribute list for this Record. (Dangerous when used within Table objects!)
data  [R]  The data for the record

Public Class methods

Creates a new Record object. If the :attributes keyword is specified, Hash-like and Struct-like access will be enabled. Otherwise, Record elements may be accessed ordinally, like an Array.

A Record can accept either a Hash or an Array as its data.

Examples:

  a = Record.new [1,2,3]
  a[1] #=> 2

  b = Record.new [1,2,3], :attributes => %w[a b c]
  b[1]   #=> 2
  b['a'] #=> 1
  b.c    #=> 3

  c = Record.new {"a" => 1, "c" => 3, "b" => 2}, :attributes => %w[a b c]
  c[1]   #=> 2
  c['a'] #=> 1
  c.c    #=> 3

  d = Record.new { "a" => 1, "c" => 3, "b" => 2 }
  d[1]   #=> ? (without attributes, you cannot rely on order)
  d['a'] #=> 1
  d.c    #=> 3

Public Instance methods

If attributes and to_a are equivalent, then == evaluates to true. Otherwise, == returns false.

Allows either Array or Hash-like indexing.

Examples:

  my_record[1]
  my_record["foo"]

Allows setting a value at an index.

Examples:

   my_record[1] = "foo"
   my_record["bar"] = "baz"

Returns a copy of the attributes from this Record.

Example:

  a = Data::Record.new([1,2],:attributes => %w[a b])
  a.attributes #=> ["a","b"]

Yields each element of the Record. Does not provide attribute names.

eql?(other)

Alias for #==

Indifferent access to attributes.

Examples:

  record.get(:foo) # looks for an attribute "foo" or :foo,
                     or calls the method <tt>foo</tt>

  record.get("foo") # looks for an attribute "foo" or :foo

  record.get(0) # Gets the first element

Provides a unique hash value. If a Record contains the same data and attributes as another Record, they will hash to the same value, even if they are not the same object. This is similar to the way Array works, but different from Hash and other objects.

length()

Alias for size

Provides accessor style methods for attribute access.

Example:

  my_record.foo = 2
  my_record.foo #=> 2

Also provides a shortcut for the as() method by converting a call to to_format_name into a call to as(:format_name)

Takes an old name and a new name and renames an attribute.

The third option, update_index is for internal use.

Allows you to change the order of or reduce the number of columns in a Record.

Example:

  a = Data::Record.new([1,2,3,4],:attributes => %w[a b c d])
  a.reorder("a","d","b")
  a.attributes #=> ["a","d","b"]
  a.data #=> [1,4,2]

The size of the record (the number of items in the record‘s data).

Converts a Record into an Array.

Example:

  a = Data::Record.new([1,2],:attributes => %w[a b])
  a.to_a #=> [1,2]

Converts a Record into a Hash.

Example:

  a = Data::Record.new([1,2],:attributes => %w[a b])
  a.to_hash #=> {"a" => 1, "b" => 2}

[Validate]