Class | Ruport::Data::Record |
In: |
lib/ruport/data/record.rb
|
Parent: | Object |
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
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
If attributes and to_a are equivalent, then == evaluates to true. Otherwise, == returns false.
Returns a copy of the attributes from this Record.
Example:
a = Data::Record.new([1,2],:attributes => %w[a b]) a.attributes #=> ["a","b"]
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 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]