Class: DataShift::Headers

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/datashift/headers.rb

Overview

Acts as an array

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, idx = 0, headers = []) ⇒ Headers

Returns a new instance of Headers.



25
26
27
28
29
# File 'lib/datashift/headers.rb', line 25

def initialize(source, idx = 0, headers = [])
  @source = source
  @idx = idx
  @headers = headers
end

Instance Attribute Details

#idxObject (readonly)

Row Index



19
20
21
# File 'lib/datashift/headers.rb', line 19

def idx
  @idx
end

#sourceObject

Returns the value of attribute source.



16
17
18
# File 'lib/datashift/headers.rb', line 16

def source
  @source
end

Class Method Details

.klass_to_headers(klass) ⇒ Object

Factory for dealing with Active Record models and collections Catalogs the supplied Klass and builds set of expected/valid Headers for Klass



34
35
36
37
38
39
40
41
42
43
# File 'lib/datashift/headers.rb', line 34

def self.klass_to_headers(klass)

  headers = Headers.new(klass)

  headers.class_source_to_headers

  DataShift::Transformation::Remove.new.unwanted_headers(headers)

  headers
end

Instance Method Details

#add(source) ⇒ Object



81
82
83
# File 'lib/datashift/headers.rb', line 81

def add(source)
  @headers << Header.new(source: source)
end

#association_to_headers(model_method) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/datashift/headers.rb', line 68

def association_to_headers( model_method )

  configuration = DataShift::Configuration.call

  if(configuration.expand_associations)
    model_method.association_columns.each do |c|
      add "#{model_method.operator}::#{c.name}"
    end
  else
    add model_method.operator
  end
end

#class_source_to_headersObject

Helpers for dealing with source = class, usually an Active Record model Catalogs the supplied Klass and builds set of expected/valid Headers for Klass

Raises:

  • (SourceIsNotAClass)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/datashift/headers.rb', line 48

def class_source_to_headers

  raise SourceIsNotAClass, 'Cannot parse source for headers - source must be a Class' unless source.is_a?(Class)

  # TODO: This collection can now be sorted
  collection = ModelMethods::Manager.catalog_class(source)

  configuration = DataShift::Configuration.call

  collection.each do |mm|
    next if(DataShift::Transformation::Remove.new.association?(mm))

    if(mm.association_type?)
      association_to_headers(mm)
    else
      add mm.operator
    end if(configuration.op_type_in_scope?(mm))
  end if collection
end

#row_indexObject



89
90
91
# File 'lib/datashift/headers.rb', line 89

def row_index
  idx
end

#sourcesObject



85
86
87
# File 'lib/datashift/headers.rb', line 85

def sources
  @headers.collect(&:source)
end