lib/axiom/relation/header.rb in axiom-0.1.1 vs lib/axiom/relation/header.rb in axiom-0.2.0

- old
+ new

@@ -8,11 +8,11 @@ extend Aliasable include Enumerable include Equalizer.new(:to_set, :keys) inheritable_alias( - :[] => :call, + :[] => :fetch, :& => :intersect, :| => :union, :- => :difference ) @@ -21,10 +21,20 @@ # @return [Keys] # # @api private attr_reader :keys + # Convert the Header into an Array + # + # @example + # array = header.to_ary + # + # @return [Array] + # + # @api public + attr_reader :to_ary + # Coerce an Array-like object into a Header # # @param [Header, #to_ary] object # the header or attributes # @param [Hash] options @@ -59,11 +69,11 @@ # @param [Hash] _options # # @return [Header] # # @api public - def self.new(attributes = EMPTY_ARRAY, _options = EMPTY_HASH) + def self.new(attributes = EMPTY_ARRAY, *) assert_unique_names(attributes.map(&:name)) super end # Coerce the attribute into an Attribute @@ -88,11 +98,11 @@ # # @api private def self.assert_unique_names(names) duplicates = duplicate_names(names) if duplicates - raise DuplicateNameError, "duplicate names: #{duplicates.inspect}" + fail DuplicateNameError, "duplicate names: #{duplicates}" end end # Returns the duplicate names, if any # @@ -121,13 +131,13 @@ # @param [Hash] options # # @return [undefined] # # @api public - def initialize(attributes, options) - @attributes = freeze_object(attributes) - @attribute_for = Hash[@attributes.map(&:name).zip(@attributes)] + def initialize(attributes, options = EMPTY_HASH) + @to_ary = self.class.freezer.call(attributes) + @attribute_for = Hash[@to_ary.map(&:name).zip(@to_ary)] @keys = coerce_keys(options.fetch(:keys, EMPTY_ARRAY)) end # Iterate over each attribute in the header # @@ -158,13 +168,13 @@ # # @return [Attribute] # the attribute when the name is known # # @api public - def call(name) + def fetch(name) @attribute_for.fetch(Attribute.name_from(name)) do |attribute_name| - raise( + fail( UnknownAttributeError, "the attribute #{attribute_name} is unknown" ) end end @@ -218,14 +228,12 @@ # # @return [Header] # # @api public def rename(aliases) - new( - map { |attribute| aliases[attribute] }, - keys: keys.rename(aliases) - ) + aliases = Algebra::Rename::Aliases.coerce(self, aliases) + new(map(&aliases.method(:[])), keys: keys.rename(aliases)) end # Return the intersection of the header with another header # # The unique keys from the headers become the new keys because @@ -294,20 +302,17 @@ # @api private def context(&block) Evaluator::Context.new(self, &block) end - # Convert the Header into an Array + # The number of attributes # - # @example - # array = header.to_ary + # @return [Integer] # - # @return [Array] - # # @api public - def to_ary - @attributes + def size + to_ary.size end # Test if there are no attributes # # @example @@ -350,12 +355,10 @@ # # @return [Header] # # @api private def coerce(*args) - self.class.coerce(*args) do |attribute| - @attribute_for[attribute] - end + self.class.coerce(*args, &@attribute_for.method(:[])) end # Represent an empty set of attributes EMPTY = new