Module: YDD

Defined in:
lib/ydd.rb,
lib/ydd/yaml_db.rb,
lib/ydd/application.rb,
lib/ydd/data_manager.rb,
lib/ydd/schema_manager.rb,
lib/ydd/serialization_helper.rb

Defined Under Namespace

Modules: SerializationHelper, YamlDB Classes: Application, DataManager, Error, SchemaManager

Class Method Summary collapse

Class Method Details

.check_files!(dir, *files) ⇒ Object



93
94
95
96
97
98
# File 'lib/ydd.rb', line 93

def self.check_files!(dir, *files)
  files.each do |file|
    path = File.join(dir, file)
    raise Error, "#{file} doesn't exist in #{dir}" if !File.readable?(path)
  end
end

.configuration_from(file) ⇒ Object



56
57
58
59
60
# File 'lib/ydd.rb', line 56

def self.configuration_from(file)
  require 'erb'
  parsed = ERB.new(File.read(file)).result
  YAML.load(parsed)[env]
end

.connect(specification) ⇒ Object



67
68
69
# File 'lib/ydd.rb', line 67

def self.connect(specification)
  ActiveRecord::Base.establish_connection(specification)
end

.connect_from(path) ⇒ Object

Raises:



62
63
64
65
# File 'lib/ydd.rb', line 62

def self.connect_from(path)
  raise Error, "Invalid database config at #{path}" if !File.exists?(path)
  connect configuration_from(path)
end

.connectionObject



52
53
54
# File 'lib/ydd.rb', line 52

def self.connection
  ActiveRecord::Base.connection
end

.dump(directory) ⇒ Object



71
72
73
74
75
# File 'lib/ydd.rb', line 71

def self.dump(directory)
  FileUtils.mkdir_p directory
  SchemaManager.dump File.join(directory, "schema.rb") unless skip_schema?
  DataManager.dump   File.join(directory, "data.yml")  unless skip_data?
end

.envObject



48
49
50
# File 'lib/ydd.rb', line 48

def self.env
  @@env ||= (ENV['YDD_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development')
end

.env=(value) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/ydd.rb', line 40

def self.env=(value)
  if value.blank?
    @@env = nil
  else
    @@env = value
  end
end

.load(directory) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ydd.rb', line 77

def self.load(directory)
  if !File.directory?(directory)
    raise Error, "Please provide a valid directory - #{directory} doesn't exist."
  end

  unless skip_schema?
    check_files! directory, "schema.rb"
    SchemaManager.load File.join(directory, "schema.rb")
  end

  unless skip_data?
    check_files! directory, "data.yml"
    DataManager.load   File.join(directory, "data.yml")
  end
end

.schema_tablesObject



24
25
26
# File 'lib/ydd.rb', line 24

def self.schema_tables
  [ 'schema_info', 'schema_migrations' ]
end

.skip_data?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/ydd.rb', line 28

def self.skip_data?
  !!skip_data
end

.skip_schema?Boolean

Returns:

  • (Boolean)


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

def self.skip_schema?
  !!skip_schema
end

.tablesObject



36
37
38
# File 'lib/ydd.rb', line 36

def self.tables
  @@tables ||= nil
end

.tables=(value) ⇒ Object



32
33
34
# File 'lib/ydd.rb', line 32

def self.tables=(value)
  @@tables = value.blank? ? nil : Array(value).join(",").split(",")
end