Class: SeedData::DataManager

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

Constant Summary collapse

ANY_ENVIRONMENT =
'any'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DataManager

Returns a new instance of DataManager.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/seed_data/data_manager.rb', line 12

def initialize(options)
  @data_sets = []

  @environment_variable = options[:environment_variable] || 'RACK_ENV'

  if options[:builder] == nil
    raise SeedData::DataBuilderNotSpecifiedException.new
  end

  @builder = options[:builder]
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



7
8
9
# File 'lib/seed_data/data_manager.rb', line 7

def builder
  @builder
end

#data_setsObject (readonly)

Returns the value of attribute data_sets.



6
7
8
# File 'lib/seed_data/data_manager.rb', line 6

def data_sets
  @data_sets
end

#environment_variableObject (readonly)

Returns the value of attribute environment_variable.



8
9
10
# File 'lib/seed_data/data_manager.rb', line 8

def environment_variable
  @environment_variable
end

Instance Method Details

#loadObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/seed_data/data_manager.rb', line 39

def load

  puts 'SeedData:: Load.'

  valid_data_sets.each do |itm|

    puts "SeedData:: Loading data set: #{itm[:data_set]}"

    @builder.build(itm[:data_set])

  end

  puts 'SeedData:: Loading complete.'

  return true

end

#register(options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/seed_data/data_manager.rb', line 24

def register(options)

  if !options.has_key?(:environment)
    options[:environment] = ANY_ENVIRONMENT
  end

  if !options.has_key?(:data_set)
    raise SeedData::InvalidRegistrationException.new('Invalid registration option, :data_set must be specified.')
  end

  @data_sets << options

  return true
end

#valid_data_setsObject



57
58
59
# File 'lib/seed_data/data_manager.rb', line 57

def valid_data_sets
  return @data_sets.select { |i| i[:environment] == ENV[@environment_variable] || i[:environment] == ANY_ENVIRONMENT }
end