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

- old
+ new

@@ -41,11 +41,11 @@ object else # Find the attribute with the block if possible, then fallback # to the default coercion method. block = lambda do |attribute| - block_given? and yield(attribute) or coerce_attribute(attribute) + coerce_attribute(block_given? && yield(attribute) || attribute) end new(Array(object).map(&block), options) end end @@ -112,24 +112,23 @@ private_class_method :coerce_attribute, :assert_unique_names, :duplicate_names # Initialize a Header # # @example - # header = Header.new(attributes, :keys => [ [ :id ] ]) + # header = Header.new(attributes, keys: [[:id]]) # # @param [Array] attributes # # @param [Hash] options # # @return [undefined] # # @api public def initialize(attributes, options) @attributes = freeze_object(attributes) - @options = freeze_object(options) @attribute_for = Hash[@attributes.map(&:name).zip(@attributes)] - @keys = coerce_keys + @keys = coerce_keys(options.fetch(:keys, EMPTY_ARRAY)) end # Iterate over each attribute in the header # # @example @@ -161,12 +160,14 @@ # the attribute when the name is known # # @api public def call(name) @attribute_for.fetch(Attribute.name_from(name)) do |attribute_name| - raise UnknownAttributeError, + raise( + UnknownAttributeError, "the attribute #{attribute_name} is unknown" + ) end end # Return a header with only the attributes specified # @@ -181,11 +182,11 @@ # # @return [Header] # # @api public def project(attributes) - coerce(attributes, :keys => keys.project(attributes)) + coerce(attributes, keys: keys.project(attributes)) end # Return a header with the new attributes added # # The original keys from the header are reused because @@ -199,11 +200,11 @@ # # @return [Header] # # @api public def extend(attributes) - new(to_ary + coerce(attributes), :keys => keys) + new(to_ary + coerce(attributes), keys: keys) end # Return a header with the attributes renamed # # The attributes in the original keys are renamed, but @@ -219,11 +220,11 @@ # # @api public def rename(aliases) new( map { |attribute| aliases[attribute] }, - :keys => keys.rename(aliases) + keys: keys.rename(aliases) ) end # Return the intersection of the header with another header # @@ -239,11 +240,11 @@ # # @api public def intersect(other) other = coerce(other) attributes = to_ary & other - new(attributes, :keys => (keys | other.keys).project(attributes)) + new(attributes, keys: (keys | other.keys).project(attributes)) end # Return the union of the header with another header # # The common keys from the headers become the new keys because @@ -257,11 +258,11 @@ # @return [Header] # # @api public def union(other) other = coerce(other) - new(to_ary | other, :keys => keys & other.keys) + new(to_ary | other, keys: keys & other.keys) end # Return the difference of the header with another header # # The original keys from the header are reused because @@ -275,13 +276,28 @@ # @return [Header] # # @api public def difference(other) other = coerce(other) - new(to_ary - other, :keys => keys - other.keys) + new(to_ary - other, keys: keys - other.keys) end + # Evaluate a block within the context of the header + # + # @yield [context] + # + # @yieldparam [Evaluator::Context] context + # + # @yieldreturn [Evaluator::Context] + # + # @return [Header] + # + # @api private + def context(&block) + Evaluator::Context.new(self, &block) + end + # Convert the Header into an Array # # @example # array = header.to_ary # @@ -317,16 +333,16 @@ self.class.new(*args) end # Coerce the keys into an Array of Headers # + # @return [Array] + # # @return [Array<Header>] # # @api private - def coerce_keys - Keys.coerce(@options.fetch(:keys, EMPTY_ARRAY)) do |attributes| - coerce(attributes) - end + def coerce_keys(keys) + Keys.coerce(keys) { |attributes| coerce(attributes) } end # Coerce the object into a Header # # @param [Array] args