Class: AutoSeeding::Seeder

Inherits:
Object
  • Object
show all
Defined in:
lib/auto-seeding/seeder.rb

Constant Summary collapse

TYPES =
[:boolean, :date, :datetime, :float, :decimal, :integer, :string, :text, :time, :timestamp]
@@globals =
{ conf: {}, sources: {} }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Seeder

Returns a new instance of Seeder.



10
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
# File 'lib/auto-seeding/seeder.rb', line 10

def initialize( opts = {} )
  options = { conf: @@globals[:conf], sources: @@globals[:sources] }
  AutoSeeding::_deep_merge!( options, opts )

  @columns = {}
  @models = {}
  @extra_validations = { confirmations: [] }
  options[:ignore_attrs] ||= [:id, :created_at, :updated_at]
  options[:ignore_attrs] += @@globals[:ignore_attrs] if @@globals[:ignore_attrs]
  options[:skip_associations] ||= []
  options[:skip_associations] += @@globals[:skip_associations] if @@globals[:skip_associations]

  path = options[:conf][:file]
  if path
    options[:conf].delete :seeder
  else
    yml_file = if options[:conf][:seeder] == :ffaker
        puts 'warning: seeder set to ffaker but FFaker is not available' unless defined? FFaker
        'ffaker.yml'
      elsif options[:conf][:seeder] == :faker
        puts 'warning: seeder set to faker but Faker is not available' unless defined? Faker
        'faker.yml'
      else
        'basic.yml'
      end
    path = Pathname.new( File.dirname __FILE__ ).join( 'data', yml_file ).to_s
  end

  # Random.srand( options[:conf][:seed_number] ? options[:conf][:seed_number].to_i : Random.new_seed )  # NOTE: problems here

  yml = Seeder::symbolize_keys YAML.load_file( path )
  # @sources = yml[:sources].merge( options[:sources] ? options[:sources] : {} )
  @sources = yml[:sources].dup
  AutoSeeding::_deep_merge!( @sources, options[:sources] ) if options[:sources]
  @sources[:fields] ||= {}
  @sources[:fields].map! { |s| Seeder::symbolize_keys s }
  @sources[:fields].sort! { |a, b| ( a['model'] || a[:model] ) ? -1 : ( ( b['model'] || b[:model] ) ? 1 : 0 ) }
  @options = options

  self
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/auto-seeding/seeder.rb', line 6

def options
  @options
end

#sourcesObject (readonly)

Returns the value of attribute sources.



6
7
8
# File 'lib/auto-seeding/seeder.rb', line 6

def sources
  @sources
end

Class Method Details

.config(options = nil) ⇒ Object



112
113
114
# File 'lib/auto-seeding/seeder.rb', line 112

def self.config( options = nil )
  @@globals = options ? options : { conf: {}, sources: {} }
end

Instance Method Details

#update(object) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/auto-seeding/seeder.rb', line 52

def update( object )
  model = object.class

  model.content_columns.each do |column|
    col = column.name.to_sym
    next if @options[:ignore_attrs].include? col
    @columns[col] ||= {
      validators: prepare_validators( model._validators[col] )
    }

    found = false
    @sources[:fields].each do |f|
      if f[:model]
        next unless Regexp.new( f[:model], Regexp::IGNORECASE ).match( model.to_s )
      end
      if( f[:in] && f[:in].include?( col.to_s ) ) ||
        ( f[:regexp] && Regexp.new( f[:regexp], Regexp::IGNORECASE ).match( col.to_s ) )
        col_ = ( f[:prepend] || f[:append] ) ? ( f[:prepend].to_s + col.to_s + f[:append].to_s ) : col.to_s
        @columns[col][:src] ||= Source.new( col, f[:type] ? f[:type].to_sym : :string, @columns[col][:validators], f )
        object.send( col_ + '=', @columns[col][:src].gen )
        found = true
        break
      end
    end
    next if found

    if TYPES.include? column.type
      object.send( col.to_s + '=', ( @columns[col][:src] ||= Source.new( col, column.type, @columns[col][:validators], @sources[:types][column.type] ) ).gen )
    end
  end

  # Setup associations
  model._reflections.each do |association, data|
    next if @options[:skip_associations].include? association.to_sym
    model2 = data.klass
    if @options[:auto_create] && @options[:auto_create].include?( association.to_sym )
      auto_seeding = AutoSeeding::Seeder.new( { conf: { seeder: @options[:seeder] || @@globals[:conf][:seeder] }, skip_associations: [model.to_s.underscore.to_sym] } )
      object.send( association + '=', auto_seeding.update( model2.new ) )
    else
      @models[model2.table_name] ||= model2.all
      if data.is_a?( ActiveRecord::Reflection::ThroughReflection )  # many-to-many
        sam = @models[model2.table_name].sample( rand( 5 ) )
        object.send( association ).push( sam ) if sam.any?
      elsif data.parent_reflection && data.parent_reflection.is_a?( ActiveRecord::Reflection::HasAndBelongsToManyReflection )
        next
      else
        sam = @models[model2.table_name].sample
        object.send( association + '=', sam ) if sam
      end
    end
  end

  # Extra validations
  @extra_validations[:confirmations].each do |field|
    object.send field.to_s+'_confirmation=', object[field]
  end

  object
end