Class: Migratrix::Extractions::Extraction

Inherits:
Object
  • Object
show all
Includes:
Loggable, ValidOptions
Defined in:
lib/migratrix/extractions/extraction.rb

Overview

base class for extraction

Direct Known Subclasses

ActiveRecord, NoOp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Extraction

Returns a new instance of Extraction.



12
13
14
15
# File 'lib/migratrix/extractions/extraction.rb', line 12

def initialize(name, options={})
  @options = options.deep_copy
  self.source = options[:source] if options[:source]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/migratrix/extractions/extraction.rb', line 8

def name
  @name
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/migratrix/extractions/extraction.rb', line 8

def options
  @options
end

#sourceObject

Returns the value of attribute source.



8
9
10
# File 'lib/migratrix/extractions/extraction.rb', line 8

def source
  @source
end

Instance Method Details

#execute_extract(source, options = {}) ⇒ Object

Execute the extraction and return the result set.

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/migratrix/extractions/extraction.rb', line 88

def execute_extract(source, options={})
  raise NotImplementedError
end

#extract(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/migratrix/extractions/extraction.rb', line 17

def extract(options={})
  options = options.deep_copy
  options[:where] = Array(options[:where]) + Array(@options[:where])
  options = @options.merge(options).symbolize_keys

  # TODO: Raise error if self.abstract? DANGER/NOTE that this is
  # the "default strategy" for extraction, and may need to be
  # extracted to a strategy object.

  src = obtain_source(self.source, options)
  src = process_source(src, options)
  execute_extract(src, options)
end

#handle_limit(source, limit) ⇒ Object

Apply limit clause to source, return new source.

Raises:

  • (NotImplementedError)


73
74
75
# File 'lib/migratrix/extractions/extraction.rb', line 73

def handle_limit(source, limit)
  raise NotImplementedError
end

#handle_offset(source, offset) ⇒ Object

Apply offset clause to source, return new source.

Raises:

  • (NotImplementedError)


78
79
80
# File 'lib/migratrix/extractions/extraction.rb', line 78

def handle_offset(source, offset)
  raise NotImplementedError
end

#handle_order(source, order) ⇒ Object

Apply order clause to source, return new source.

Raises:

  • (NotImplementedError)


83
84
85
# File 'lib/migratrix/extractions/extraction.rb', line 83

def handle_order(source, order)
  raise NotImplementedError
end

#handle_where(source, where) ⇒ Object

Apply where clause to source, return new source.

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/migratrix/extractions/extraction.rb', line 68

def handle_where(source, where)
  raise NotImplementedError
end

#obtain_source(source, options = {}) ⇒ Object

First step in extraction is to take the given source and turn it into something that the filter chain can used. The ActiveRecord extraction uses a legacy model class as its source so it can simply return its source. A CSV or Yaml extraction here might need to read the entire file contents and returns the full, unfiltered data source.

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/migratrix/extractions/extraction.rb', line 63

def obtain_source(source, options={})
  raise NotImplementedError
end

#process_source(source, options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/migratrix/extractions/extraction.rb', line 31

def process_source(source, options)
  if options[:where]
    options[:where].each do |where|
      source = handle_where(source, where)
    end
  end
  source = handle_order(source, options[:order]) if options[:order]
  source = handle_limit(source, options[:limit]) if options[:limit]
  source = handle_offset(source, options[:offset]) if options[:offset]
  source
end