Class: ConfigKit::Data::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/config_kit/data/loader.rb

Direct Known Subclasses

FileLoader, GitLoader

Defined Under Namespace

Classes: LoaderFailure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(batch_size = 10) ⇒ Loader

Returns a new instance of Loader.



55
56
57
58
59
60
61
# File 'lib/config_kit/data/loader.rb', line 55

def initialize(batch_size=10)
  @files = []
  @batch_size = batch_size
  @current_files = []
  @cursor = 0
  @files = retrieve_files
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



54
55
56
# File 'lib/config_kit/data/loader.rb', line 54

def batch_size
  @batch_size
end

#cursorObject (readonly)

Returns the value of attribute cursor.



54
55
56
# File 'lib/config_kit/data/loader.rb', line 54

def cursor
  @cursor
end

#filesObject (readonly)

Returns the value of attribute files.



54
55
56
# File 'lib/config_kit/data/loader.rb', line 54

def files
  @files
end

Class Method Details

.err(message) ⇒ Object



18
19
20
# File 'lib/config_kit/data/loader.rb', line 18

def self.err(message)
  STDERR.puts message
end

.info(message) ⇒ Object



14
15
16
# File 'lib/config_kit/data/loader.rb', line 14

def self.info(message)
  STDOUT.puts message
end

.inherited(subclass) ⇒ Object



25
26
27
# File 'lib/config_kit/data/loader.rb', line 25

def self.inherited(subclass)
  @loaders << subclass
end

.load(app, from, uri_kls, env, codename, version, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/config_kit/data/loader.rb', line 32

def self.load(app, from, uri_kls, env, codename, version, &block)
  @loader_names = @loaders.map{ |l| l.loader}

  @uri_kls, @env, @app, @version, @codename = uri_kls, env, app, version, codename

  loader = from


  if loader.nil?
    ConfigKit.logger.error "from empty source with #{@uri_kls.to_s}"
  elsif !@loader_names.include?(loader)
    ConfigKit.logger.error "Unrecognize loader: #{loader} for #{@uri}(#{@codename})"
  else
    loader_class = @loaders.find{ |c| c.loader == loader}
    ConfigKit.logger.debug "#{loader_class.loader} is loading env(#{@env}) app(#{app}) data(#{@version}) codename(#{codename}) from #{uri_kls.to_s}"
    loader_class.new(@uri_kls, @env, @codename, @app, @version).run(&block)
    ConfigKit.logger.debug "#{loader_class.loader} is loaded env(#{@env}) app(#{app}) data(#{@version}) codename(#{codename}) from #{uri_kls.to_s}"
  end
rescue => e
  raise ConfigKit::Data::Loader::LoaderFailure.new e.message
end

Instance Method Details

#file_countObject



63
64
65
# File 'lib/config_kit/data/loader.rb', line 63

def file_count
  @files.count
end

#finish?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/config_kit/data/loader.rb', line 82

def finish?
  @cursor >= file_count
end

#next_batchObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/config_kit/data/loader.rb', line 67

def next_batch
  if finish?
    @current_files = []
    @cursor = file_count
    return @cursor
  end
  _next_cursor = next_cursor
  @current_files = @files[@cursor.._next_cursor - 1]
  @cursor = _next_cursor
end

#next_cursorObject



78
79
80
# File 'lib/config_kit/data/loader.rb', line 78

def next_cursor
  @cursor + @batch_size > file_count ? file_count : (@cursor + @batch_size)
end

#retrieve_filesObject

Raises:

  • (NotImplementedError)


86
87
88
# File 'lib/config_kit/data/loader.rb', line 86

def retrieve_files
  raise NotImplementedError
end