Class: ActiveExplorer::Exploration

Inherits:
Object
  • Object
show all
Defined in:
lib/exploration.rb

Constant Summary collapse

ASSOCIATION_FILTER_VALUES =
[:has_many, :has_one, :belongs_to, :all]

Instance Method Summary collapse

Constructor Details

#initialize(object, depth: 5, class_filter: nil, attribute_filter: nil, attribute_limit: nil, association_filter: nil, parent_object: nil) ⇒ Exploration

Creates new exploration and generates exploration hash.

Parameters:

  • association_filter (Array) (defaults to: nil)

    Values of array: :has_many, :has_one, :belongs_to, :all. When empty then it “follows previous association” (i.e. uses :belongs_to when previous assoc. was :belongs_to and uses :has_xxx when previous assoc. was :has_xxx). To always follow all associations you must specify all associations (e.g. uses ActiveExplorer::Exploration::ASSOCIATION_FILTER_VALUES as a value).

  • class_filter (Array or Hash) (defaults to: nil)

    If Array is used then it means to show only those classes in Array. When Hash is used then it can have these keys:

    - `:show` - Shows these classes, ignores at all other classes.
    - `:ignore` - Stops processing at these, does not show it and does not go to children. Processing goes back to parent.
    

    Use plural form (e.g. books).

  • depth (Integer) (defaults to: 5)

    How deep into the subobjects should the explorere go. Depth 1 is only direct children. Depth 0 returns no children.

Raises:

  • (TypeError)


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
# File 'lib/exploration.rb', line 27

def initialize(object, depth: 5, class_filter: nil, attribute_filter: nil, attribute_limit: nil, association_filter: nil, parent_object: nil)
  raise TypeError, "Parameter 'class_filter' must be Array or Hash but is #{class_filter.class}." unless class_filter.nil? || class_filter.is_a?(Array) || class_filter.is_a?(Hash)
  raise TypeError, "Parameter 'association_filter' must be Array but is #{association_filter.class}." unless association_filter.nil? || association_filter.is_a?(Array)
  raise TypeError, "Parameter 'association_filter' must only contain values #{ASSOCIATION_FILTER_VALUES.to_s[1..-2]}." unless association_filter.nil? || association_filter.empty? || (association_filter & ASSOCIATION_FILTER_VALUES).any?

  @object = object
  @depth = depth
  @parent_object = parent_object

  @attribute_limit = attribute_limit || ActiveExplorer::Config.attribute_limit
  @attribute_filter = attribute_filter || ActiveExplorer::Config.attribute_filter

  @hash = { class_name: make_safe(@object.class.name),
            attributes: attributes }

  unless @depth.zero?
    @class_filter = class_filter || ActiveExplorer::Config.class_filter
    @class_filter = { show: @class_filter } if @class_filter.is_a?(Array)

    if @class_filter
      [:show, :ignore].each do |group|
        @class_filter[group] = @class_filter[group].present? ? each_val_to_s(@class_filter[group]) : []
      end
    end

    @association_filter = association_filter || ActiveExplorer::Config.association_filter
    @association_filter = ASSOCIATION_FILTER_VALUES if @association_filter.present? && @association_filter.include?(:all)

    @associations = associtations(@object, @class_filter, @association_filter)

    subobject_hash = subobjects_hash(@object, @associations)
    @hash[:subobjects] = subobject_hash unless subobject_hash.empty?
  end
end

Instance Method Details

#attributesObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/exploration.rb', line 62

def attributes
  attrs = @object.attributes.symbolize_keys
  attrs = attrs.first(@attribute_limit).to_h if @attribute_limit

  return attrs if @attribute_filter.nil?

  filter = @attribute_filter[@object.class.name.downcase.pluralize.to_sym]

  if filter
    attrs.select { |key| filter.include?(key) }
  else
    attrs
  end
end

#get_hashObject



77
78
79
# File 'lib/exploration.rb', line 77

def get_hash
  @hash.dup
end

#to_consoleObject



81
82
83
# File 'lib/exploration.rb', line 81

def to_console
  Writer.new(self).write
end

#to_image(file, origin_as_root: false) ⇒ Object



85
86
87
# File 'lib/exploration.rb', line 85

def to_image(file, origin_as_root: false)
  Painter.new(self, file).paint(origin_as_root: origin_as_root)
end