Module: ActiveWarehouse::HierarchicalDimension::ClassMethods

Defined in:
lib/active_warehouse/dimension/hierarchical_dimension.rb

Overview

:nodoc

Instance Method Summary collapse

Instance Method Details

#acts_as_hierarchy_dimensionObject Also known as: acts_as_hierarchical_dimension

Indicates that a dimension is a variable-depth hierarchy.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_warehouse/dimension/hierarchical_dimension.rb', line 11

def acts_as_hierarchy_dimension
  unless hierarchical_dimension?
     class << self
       # Get the bridge class for this dimension
       def bridge_class
         unless @bridge_class
           unless Object.const_defined?(bridge_class_name.to_sym)
             Object.const_set(bridge_class_name.to_sym, Class.new(ActiveWarehouse::Bridge))
           end
           @bridge_class = Object.const_get(bridge_class_name.to_sym)
         end
         @bridge_class
       end

       # Get the bridge class name for this hierarchical dimension
       def bridge_class_name
         @child_hierarchy_relationship.class_name || @parent_hierarchy_relationship.class_name
       end

      # Define the child relationship on the bridge table to the dimension
      # table. We can specify a different class name for the bridge table
      # and different foreign-key.
      def child_bridge(association_id, options = {})
        options[:class_name] ||= name.gsub(/Dimension$/, 'HierarchyBridge')
        options[:foreign_key] ||= "parent_id"
        has_many association_id, options
        @child_hierarchy_relationship = reflections[association_id]
      end

      # Define the parent relationship on the bridge table to the dimension
      # table. 
      def parent_bridge(association_id, options = {})
        options[:class_name] ||= name.gsub(/Dimension$/, 'HierarchyBridge')
        options[:foreign_key] ||= "child_id"
        has_many association_id, options
        @parent_hierarchy_relationship = reflections[association_id]
      end

      # the foreign key column name on the bridge table for finding the
      # children.
      def child_foreign_key
        @child_hierarchy_relationship.primary_key_name
      end
      
      # the foreign key column name on the bridge table for finding the
      # parent.
      def parent_foreign_key
        @parent_hierarchy_relationship.primary_key_name
      end
      
      # the column name on the bridge table that defines the number of levels
      # from the parent
      def levels_from_parent
        bridge_class.levels_from_parent
      end
     end
  end
  include InstanceMethods
end

#hierarchical_dimension?Boolean

Return true if this is a hierarchical dimension

Returns:

  • (Boolean)


73
74
75
# File 'lib/active_warehouse/dimension/hierarchical_dimension.rb', line 73

def hierarchical_dimension?
  self.included_modules.include?(InstanceMethods)
end