Class: Yarf::ModelConfig

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, hash) ⇒ ModelConfig

Returns a new instance of ModelConfig.



7
8
9
10
11
12
13
# File 'lib/yarf/model_config.rb', line 7

def initialize(model_class, hash)
  @model_class = model_class
  @name = model_class.name
  @skip = !!hash["skip"]
  @ignored_columns = hash["ignored_columns"] || []
  @dependencies = hash["dependencies"] || []
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



6
7
8
# File 'lib/yarf/model_config.rb', line 6

def dependencies
  @dependencies
end

#ignored_columnsObject (readonly)

Returns the value of attribute ignored_columns.



6
7
8
# File 'lib/yarf/model_config.rb', line 6

def ignored_columns
  @ignored_columns
end

#model_classObject (readonly)

Returns the value of attribute model_class.



6
7
8
# File 'lib/yarf/model_config.rb', line 6

def model_class
  @model_class
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/yarf/model_config.rb', line 6

def name
  @name
end

Instance Method Details

#delete_allObject



27
28
29
30
# File 'lib/yarf/model_config.rb', line 27

def delete_all
  # puts "deleting #{name}"
  model_class.delete_all
end

#destroy_allObject



32
33
34
35
# File 'lib/yarf/model_config.rb', line 32

def destroy_all
  puts "destroying #{name}"
  model_class.destroy_all
end

#load_fixtures(fixtures_dir) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yarf/model_config.rb', line 37

def load_fixtures(fixtures_dir)
  puts model_class.all.map(&:inspect)
  [:create, :save].each{|a| model_class.skip_callback(a)}
  path = File.join(fixtures_dir, name.underscore + ".yml")
  unless File.readable?(path)
    Rails.logger.warn("fixture #{path} not found for #{name}")
    return
  end
  begin
    rows = YAML.load_file(path)
    rows.each do |row|
      model_class.connection.insert_fixture(row, model_class.table_name)
    end
  ensure
    [:create, :save].each{|a| model_class.set_callback(a)}
  end
end

#selected_columnsObject



23
24
25
# File 'lib/yarf/model_config.rb', line 23

def selected_columns
  @selected_columns ||= model_class.columns.reject{|c| ignored_columns.include?(c.name)}
end

#skip?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/yarf/model_config.rb', line 15

def skip?
  @skip
end

#target?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/yarf/model_config.rb', line 19

def target?
  not(skip?) and not(@model_class.abstract_class?)
end