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
# File lib/ruport/data/record.rb, line 53 def initialize(data,options={}) data = data.dup case(data) when Array @attributes = options[:attributes] || (0...data.length).to_a @data = @attributes.inject({}) { |h,a| h.merge(a => data.shift) } when Hash @data = data.dup @attributes = options[:attributes] || data.keys end end
If attributes and to_a are equivalent, then == evaluates to true. Otherwise, == returns false.
# File lib/ruport/data/record.rb, line 183 def ==(other) @attributes.eql?(other.attributes) && to_a == other.to_a end
Allows either Array or Hash-like indexing.
Examples:
my_record[1] my_record["foo"]
# File lib/ruport/data/record.rb, line 102 def [](index) case(index) when Integer @data[@attributes[index]] else @data[index] end end
Allows setting a value at an index.
Examples:
my_record[1] = "foo" my_record["bar"] = "baz"
# File lib/ruport/data/record.rb, line 118 def []=(index,value) case(index) when Integer @data[@attributes[index]] = value else @data[index] = value @attributes << index unless @attributes.include? index end end
Returns a copy of the attributes from this Record.
Example:
a = Data::Record.new([1,2],:attributes => %w[a b]) a.attributes #=> ["a","b"]
# File lib/ruport/data/record.rb, line 76 def attributes @attributes.dup end
Yields each element of the Record. Does not provide attribute names.
# File lib/ruport/data/record.rb, line 195 def each to_a.each { |e| yield(e) } end
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
# File lib/ruport/data/record.rb, line 139 def get(name) case name when String,Symbol self[name] || send(name) when Fixnum self[name] else raise ArgumentError, "Whatchu Talkin' Bout, Willis?" end end
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.
# File lib/ruport/data/record.rb, line 248 def hash @attributes.hash + to_a.hash end
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)
# File lib/ruport/data/record.rb, line 274 def method_missing(id,*args,&block) k = id.to_s.gsub(/=$/,"") key_index = @attributes.index(k) || @attributes.index(k.to_sym) if key_index args[0] ? self[key_index] = args[0] : self[key_index] else return as($1.to_sym,*args,&block) if id.to_s =~ /^to_(.*)/ super end end
Takes an old name and a new name and renames an attribute.
The third option, update_index is for internal use.
# File lib/ruport/data/record.rb, line 206 def rename_attribute(old_name,new_name,update_index=true) @attributes[@attributes.index(old_name)] = new_name if update_index @data[new_name] = @data.delete(old_name) end
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]
# File lib/ruport/data/record.rb, line 220 def reorder(*indices) indices[0].kind_of?(Array) && indices.flatten! if indices.all? { |i| i.kind_of? Integer } raise ArgumentError unless indices.all? { |i| @attributes[i] } self.attributes = indices.map { |i| @attributes[i] } else raise ArgumentError unless (indices - @attributes).empty? self.attributes = indices end self end
The size of the record (the number of items in the record's data).
# File lib/ruport/data/record.rb, line 88 def size; @data.size; end
Converts a Record into an Array.
Example:
a = Data::Record.new([1,2],:attributes => %w[a b]) a.to_a #=> [1,2]
# File lib/ruport/data/record.rb, line 161 def to_a @attributes.map { |a| @data[a] } end
Converts a Record into a Hash.
Example:
a = Data::Record.new([1,2],:attributes => %w[a b]) a.to_hash #=> {"a" => 1, "b" => 2}
# File lib/ruport/data/record.rb, line 172 def to_hash @data.dup end
Generated with the Darkfish Rdoc Generator 2.