Class: DataShift::GeneratorBase

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

Direct Known Subclasses

CsvGenerator, ExcelGenerator, MappingGenerator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ GeneratorBase

Returns a new instance of GeneratorBase.



14
15
16
17
18
# File 'lib/generators/generator_base.rb', line 14

def initialize(filename)
  @filename = filename
  @headers = []
  @remove_list =[]
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



12
13
14
# File 'lib/generators/generator_base.rb', line 12

def filename
  @filename
end

#headersObject

Returns the value of attribute headers.



12
13
14
# File 'lib/generators/generator_base.rb', line 12

def headers
  @headers
end

#remove_listObject

Returns the value of attribute remove_list.



12
13
14
# File 'lib/generators/generator_base.rb', line 12

def remove_list
  @remove_list
end

Class Method Details

.rails_columnsObject



20
21
22
# File 'lib/generators/generator_base.rb', line 20

def self.rails_columns
  @rails_standard_columns ||= [:id, :created_at, :created_on, :updated_at, :updated_on]
end

Instance Method Details

#prep_remove_list(options) ⇒ Object

Take options and create a list of symbols to remove from headers Rails columns like id, created_at etc are added to the remove list by default Specify :include_rails to keep them in



82
83
84
85
86
87
88
# File 'lib/generators/generator_base.rb', line 82

def prep_remove_list( options )
  remove_list = [ *options[:remove] ].compact.collect{|x| x.to_s.downcase.to_sym }

  remove_list += GeneratorBase::rails_columns unless(options[:include_rails])

  remove_list
end

#prepare_model_headers(method_details_mgr, options = {}) ⇒ Object

Parse options and build collection of headers for a method_details_mgr wrapping a class based on association requirements,

Default is to include everything

  • :exclude - Association TYPE(s) to exclude completely.

    Possible association_type values are given by MethodDetail::supported_types_enum
    ... [:assignment, :belongs_to, :has_one, :has_many]
    
  • :remove - Array of header names to remove

Rails DB columns like id, created_at, updated_at are removed by default

  • :include_rails - Specify to keep Rails columns in mappings


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generators/generator_base.rb', line 41

def prepare_model_headers(method_details_mgr, options = {})

  work_list = MethodDetail::supported_types_enum.to_a - [ *options[:exclude] ]

  @headers = []

  work_list.each do |assoc_type|
    method_details_for_assoc_type = method_details_mgr.get_list_of_method_details(assoc_type)

    next if(method_details_for_assoc_type.nil? || method_details_for_assoc_type.empty?)

    method_details_for_assoc_type.each do |md|
      #comparable_association = md.operator.to_s.downcase.to_sym
      #i = remove_list.index { |r| r == comparable_association }
      #(i) ? remove_list.delete_at(i) : @headers << "#{md.operator}"
      @headers << md.operator
    end
  end

  remove_headers(options)

end

#remove_headers(options) ⇒ Object

Parse options and remove headers Specify columns to remove with : options Rails columns like id, created_at are removed by default, to keep them in specify options



71
72
73
74
75
76
# File 'lib/generators/generator_base.rb', line 71

def remove_headers(options)
  remove_list = prep_remove_list( options )

  #TODO - more efficient way ?
  headers.delete_if { |h| remove_list.include?( h.to_sym ) } unless(remove_list.empty?)
end