Class: EvilSeed::Configuration::Root

Inherits:
Object
  • Object
show all
Defined in:
lib/evil_seed/configuration/root.rb

Overview

Configuration for dumping some root model and its associations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, dont_nullify, *constraints) ⇒ Root

Returns a new instance of Root.

Parameters:

  • model (String)

    Name of the model class to dump

  • constraints (String, Hash)

    Everything you can feed into where to limit number of records



13
14
15
16
17
18
19
20
21
# File 'lib/evil_seed/configuration/root.rb', line 13

def initialize(model, dont_nullify, *constraints)
  @model = model
  @constraints = constraints
  @exclusions = []
  @inclusions = {}
  @association_limits = {}
  @deep_limit = nil
  @dont_nullify = dont_nullify
end

Instance Attribute Details

#association_limitsObject (readonly)

Returns the value of attribute association_limits.



8
9
10
# File 'lib/evil_seed/configuration/root.rb', line 8

def association_limits
  @association_limits
end

#constraintsObject (readonly)

Returns the value of attribute constraints.



7
8
9
# File 'lib/evil_seed/configuration/root.rb', line 7

def constraints
  @constraints
end

#deep_limitObject (readonly)

Returns the value of attribute deep_limit.



8
9
10
# File 'lib/evil_seed/configuration/root.rb', line 8

def deep_limit
  @deep_limit
end

#dont_nullifyObject (readonly)

Returns the value of attribute dont_nullify.



8
9
10
# File 'lib/evil_seed/configuration/root.rb', line 8

def dont_nullify
  @dont_nullify
end

#exclusionsObject (readonly)

Returns the value of attribute exclusions.



9
10
11
# File 'lib/evil_seed/configuration/root.rb', line 9

def exclusions
  @exclusions
end

#inclusionsObject (readonly)

Returns the value of attribute inclusions.



9
10
11
# File 'lib/evil_seed/configuration/root.rb', line 9

def inclusions
  @inclusions
end

#limit(limit = nil) ⇒ Object (readonly)

Returns the value of attribute limit.



7
8
9
# File 'lib/evil_seed/configuration/root.rb', line 7

def limit
  @limit
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/evil_seed/configuration/root.rb', line 7

def model
  @model
end

#order(order = nil) ⇒ Object (readonly)

Returns the value of attribute order.



7
8
9
# File 'lib/evil_seed/configuration/root.rb', line 7

def order
  @order
end

#total_limitObject (readonly)

Returns the value of attribute total_limit.



8
9
10
# File 'lib/evil_seed/configuration/root.rb', line 8

def total_limit
  @total_limit
end

Instance Method Details

#do_not_nullify(nullify_flag) ⇒ Object



98
99
100
# File 'lib/evil_seed/configuration/root.rb', line 98

def do_not_nullify(nullify_flag)
  @dont_nullify = nullify_flag
end

#exclude(*association_patterns) ⇒ Object

Exclude some of associations from the dump

Parameters:

  • association_patterns

    Array<String, Regex> Patterns to exclude associated models from dump



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/evil_seed/configuration/root.rb', line 25

def exclude(*association_patterns)
  association_patterns.each do |pattern|
    case pattern
    when String, Regexp
      @exclusions << pattern
    else
      path_prefix = model.constantize.model_name.singular
      @exclusions += compile_patterns(pattern, prefix: path_prefix).map { |p| Regexp.new(/\A#{p}\z/) }
    end
  end
end

#exclude_has_relationsObject



53
54
55
# File 'lib/evil_seed/configuration/root.rb', line 53

def exclude_has_relations
  @excluded_has_relations = :exclude_has_relations
end

#exclude_optional_belongs_toObject



57
58
59
# File 'lib/evil_seed/configuration/root.rb', line 57

def exclude_optional_belongs_to
  @excluded_optional_belongs_to = :exclude_optional_belongs_to
end

#excluded?(association_path) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/evil_seed/configuration/root.rb', line 102

def excluded?(association_path)
  exclusions.find { |exclusion| association_path.match(exclusion) } #.match(association_path) }
end

#excluded_has_relations?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/evil_seed/configuration/root.rb', line 110

def excluded_has_relations?
  @excluded_has_relations
end

#excluded_optional_belongs_to?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/evil_seed/configuration/root.rb', line 114

def excluded_optional_belongs_to?
  @excluded_optional_belongs_to
end

#include(*association_patterns, &block) ⇒ Object

Include some excluded associations back to the dump

Parameters:

  • association_patterns

    Array<String, Regex> Patterns to exclude associated models from dump



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/evil_seed/configuration/root.rb', line 39

def include(*association_patterns, &block)
  association_patterns.each do |pattern|
    case pattern
    when String, Regexp
      @inclusions[pattern] = block
    else
      path_prefix = model.constantize.model_name.singular
      compile_patterns(pattern, prefix: path_prefix).map do |p|
        @inclusions[Regexp.new(/\A#{p}\z/)] = block
      end
    end
  end
end

#included?(association_path) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/evil_seed/configuration/root.rb', line 106

def included?(association_path)
  inclusions.find { |inclusion, _block| association_path.match(inclusion) }
end

#limit_associations_size(limit, *association_patterns) ⇒ Object

Limit number of records in all (if pattern is not provided) or given associations to include into dump

Parameters:

  • limit (Integer)

    Maximum number of records in associations to include into dump

  • association_pattern

    Array<String, Regex, Hash> Pattern to limit number of records for certain associated models



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/evil_seed/configuration/root.rb', line 76

def limit_associations_size(limit, *association_patterns)
  return @total_limit = limit if association_patterns.empty?

  association_patterns.each do |pattern|
    case pattern
    when String, Regexp
      @association_limits[pattern] = limit
    else
      path_prefix = model.constantize.model_name.singular
      compile_patterns(pattern, prefix: path_prefix, partial: false).map do |p|
        @association_limits[Regexp.new(/\A#{p}\z/)] = limit
      end
    end
  end
end

#limit_deep(limit) ⇒ Object

Limit deepenes of associations to include into dump

Parameters:

  • limit (Integer)

    Maximum level to recursively dive into associations



94
95
96
# File 'lib/evil_seed/configuration/root.rb', line 94

def limit_deep(limit)
  @deep_limit = limit
end