Class: ArtirixDataModels::AggregationsFactory

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

Defined Under Namespace

Modules: SortedBucketsAggregationClassFactory

Constant Summary collapse

DEFAULT_COLLECTION_CLASS_NAME =
''.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAggregationsFactory

FACTORY INSTANCE



13
14
15
16
# File 'lib/artirix_data_models/aggregations_factory.rb', line 13

def initialize
  @_loaders = Hash.new { |h, k| h[k] = {} }
  setup_config
end

Class Method Details

.sorted_aggregation_class_based_on_index_on(index_array) ⇒ Object

AGGREGATION CLASS BUILDING



7
8
9
# File 'lib/artirix_data_models/aggregations_factory.rb', line 7

def self.sorted_aggregation_class_based_on_index_on(index_array)
  SortedBucketsAggregationClassFactory.build_class_based_on_index_on(index_array)
end

Instance Method Details

#aggregation_from_json(definition, value_class: Aggregation::Value, aggregation_class: Aggregation) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/artirix_data_models/aggregations_factory.rb', line 55

def aggregation_from_json(definition, value_class: Aggregation::Value, aggregation_class: Aggregation)
  builder_params = {
    aggregations_factory: self,
    definition: definition,
    aggregation_class: aggregation_class,
    value_class: value_class,
  }

  AggregationBuilder.new(builder_params).build
end

#build_all_from_raw_data(raw, collection_class = nil) ⇒ Object



50
51
52
53
# File 'lib/artirix_data_models/aggregations_factory.rb', line 50

def build_all_from_raw_data(raw, collection_class = nil)
  normalised = normalise_aggregations_data(raw)
  normalised.map { |definition| build_from_json definition, collection_class }
end

#build_from_json(aggregation, collection_class = nil) ⇒ Object

AGGREGATION BUILDING



46
47
48
# File 'lib/artirix_data_models/aggregations_factory.rb', line 46

def build_from_json(aggregation, collection_class = nil)
  get_loader(aggregation[:name], collection_class).call aggregation
end

#default_loaderObject



34
35
36
# File 'lib/artirix_data_models/aggregations_factory.rb', line 34

def default_loader
  proc { |aggregation| Aggregation.from_json aggregation }
end

#get_loader(aggregation_name, collection_class) ⇒ Object



38
39
40
41
42
# File 'lib/artirix_data_models/aggregations_factory.rb', line 38

def get_loader(aggregation_name, collection_class)
  @_loaders[collection_class.to_s][aggregation_name.to_s] ||
    @_loaders[DEFAULT_COLLECTION_CLASS_NAME][aggregation_name.to_s] ||
    default_loader
end

#set_loader(aggregation_name, collection_class = nil, loader = nil, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/artirix_data_models/aggregations_factory.rb', line 24

def set_loader(aggregation_name, collection_class = nil, loader = nil, &block)
  if block
    @_loaders[collection_class.to_s][aggregation_name.to_s] = block
  elsif loader.respond_to? :call
    @_loaders[collection_class.to_s][aggregation_name.to_s] = loader
  else
    raise ArgumentError, "no block and no loader given for key #{key}"
  end
end

#setup_configObject

SETUP AND CONFIG MANAGEMENT



20
21
22
# File 'lib/artirix_data_models/aggregations_factory.rb', line 20

def setup_config
  # To be Extended
end