Class: Cacofonix::ProductBase

Inherits:
Element
  • Object
show all
Defined in:
lib/cacofonix/elements/product_base.rb

Overview

This is the abstract base class of Product, RelatedProduct and ContainedItem.

Direct Known Subclasses

ContainedItem, Product, RelatedProduct

Instance Method Summary collapse

Methods inherited from Element

alias_accessor, #fetch, #fetch_all, #initialize, onix_boolean_flag, onix_code_from_list, onix_codes_from_list, onix_composite, onix_date_accessor, onix_space_separated_list, onix_spaced_codes_from_list, xml_accessor

Constructor Details

This class inherits a constructor from Cacofonix::Element

Instance Method Details

#interpret(mods) ⇒ Object

Extend this product instance with a module. Typically these modules make it easier to read or write common values in the Product.

The product tracks the modules that have extended it, so that it can easily pass these extensions on to other products (see #interpret_like_me).

For convenience, this method returns the product itself.



34
35
36
37
38
39
40
41
42
# File 'lib/cacofonix/elements/product_base.rb', line 34

def interpret(mods)
  @_extensions ||= []
  [mods].flatten.compact.uniq.each { |mod|
    next  if @_extensions.include?(mod)
    @_extensions << mod
    extend(mod)
  }
  self
end

#interpret_like_me(product) ⇒ Object

Apply all the modules that have extended this product to another product.

This is useful when, say, accessing RelatedProduct or ContainedItem composites. Your module might do something like:

def print_product
  prod = related_products.detect { |p| p.relation_code == 13 }
  prod ? interpret_like_me(prod) : nil
end

As a result, this related product will have all the extensions applied to this product.



57
58
59
# File 'lib/cacofonix/elements/product_base.rb', line 57

def interpret_like_me(product)
  product.interpret(@_extensions)
end