Sha256: 59c29a8319588fcdf5c1447f3095023217ad49260b49d133cb7baab0ae5d61fa

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

module AplContextBuilder

  #Used as a place holder for contexts where validations are applicable for all types at parent level
  #For instance if a validation is applicable for some item irrespective of which invoice it belongs to, we can use all in the context
  #before specifying item type e.g. create_all_tax_item for tax_item validations irrespective of the invoice belongs to
  ALL_ENTITY_TYPES_CURRENT_LEVEL = 'all'
  UNDERSCORE = '_'

  #This method will generate all contexts containing the leaf level entity
  #by substituting parent at each level with ALL_ENTITY_TYPES_CURRENT_LEVEL starting from the root
  #Ex Input - 'create', ['cost_invoice', 'product_item', 'sub_item_type']
  #Output - ["create_cost_invoice_product_item_sub_item_type", "create_all_product_item_sub_item_type", "create_all_all_sub_item_type"]

  #if leaf_node is not nil, return only leaf node i.e. the context containg all the elements in context_hierarchy
  def get_contexts(action, context_hierarchy, return_leaf_node_only = false)
    contexts = Array.new
    context = action + UNDERSCORE
    context_changer = 0
    loop_run = 0
    begin
      context_hierarchy.each do |context_node|
        if(context_changer > 0)
          index = context_hierarchy.index(context_node)
          context_node = ALL_ENTITY_TYPES_CURRENT_LEVEL
          context_hierarchy[index] = context_node
          context_changer -= 1
        end
        if context_hierarchy.index(context_node) != (context_hierarchy.size - 1)

          context += context_node + UNDERSCORE
        else
          context += context_node
        end
      end
      loop_run += 1
      context_changer = loop_run
      contexts << context
      context = action + UNDERSCORE
    end while loop_run < context_hierarchy.size

    if contexts.empty?
      return (!return_leaf_node_only) ? [] : nil
    else
      return (!return_leaf_node_only) ?  contexts : contexts[0]
    end

  end
end

Version data entries

2 entries across 1 versions & 1 rubygems

Version Path
apl-library-0.0.90 lib/apl-library/apl_context_builder.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/lib/apl-library/apl_context_builder.rb