Class: Es::Load

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entities) ⇒ Load

Returns a new instance of Load.



273
274
275
276
# File 'lib/es.rb', line 273

def initialize(entities)
  @entities = entities
  validate
end

Instance Attribute Details

#entitiesObject

Returns the value of attribute entities.



259
260
261
# File 'lib/es.rb', line 259

def entities
  @entities
end

Class Method Details

.parse(spec) ⇒ Object



261
262
263
264
265
# File 'lib/es.rb', line 261

def self.parse(spec)
  Load.new(spec.map do |entity_spec|
    Entity.parse(entity_spec)
  end)
end

.parseOldFormat(spec) ⇒ Object



267
268
269
270
271
# File 'lib/es.rb', line 267

def self.parseOldFormat(spec)
  Load.new(spec.map do |entity_spec|
           Entity.parseOldFormat(entity_spec[1])
  end)
end

Instance Method Details

#get_entity(name) ⇒ Object



289
290
291
# File 'lib/es.rb', line 289

def get_entity(name)
  entities.detect {|e| e.name == name}
end

#get_merged_entity_for(name) ⇒ Object



278
279
280
281
282
283
284
285
286
287
# File 'lib/es.rb', line 278

def get_merged_entity_for(name)
  entities_to_merge = entities.find_all {|e| e.name == name}
  fail UnableToMerge.new("There is no entity #{name.bright} in current load object.") if entities_to_merge.empty?
  merged_fields = entities_to_merge.inject([]) {|all, e| all.concat e.fields}
  merged_fields = merged_fields.uniq_by {|obj| [obj.name, obj.type]}
  Entity.new(name, {
    :file => "MERGED",
    :fields => merged_fields
  })
end

#to_configObject



300
301
302
# File 'lib/es.rb', line 300

def to_config
  entities.map {|e| e.to_load_config}
end

#to_config_file(filename) ⇒ Object



314
315
316
317
318
# File 'lib/es.rb', line 314

def to_config_file(filename)
  File.open(filename, 'w') do |f|
    f.write(JSON.pretty_generate(to_config))
  end
end

#to_config_generatorObject



304
305
306
307
308
309
310
311
312
# File 'lib/es.rb', line 304

def to_config_generator
  entities.map do |e|
    d = ActiveSupport::OrderedHash.new
    d['entity'] = e.to_config_generator[:entity]
    d['file'] = "data/estore-in/#{e.to_config_generator[:file].match(/[^\/]*.csv/)[0]}"
    d['fields'] = e.to_config_generator[:fields]
    d
  end 
end

#validateObject



293
294
295
296
297
298
# File 'lib/es.rb', line 293

def validate
  names = entities.map {|e| e.name}.uniq
  names.each do |name|
    merged_entity = get_merged_entity_for(name)
  end
end