Module: Cardio

Defined in:
lib/cardio.rb

Class Method Summary collapse

Class Method Details

.add_path(path, options = {}) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/cardio.rb', line 81

def add_path path, options={}
  root = options.delete(:root) || gem_root
  gem_path = File.join( root, path )
  with = options.delete(:with)
  with = with ? File.join(root, with) : gem_path
  paths[path] = Rails::Paths::Path.new(paths, gem_path, with, options)
end

.assume_migrated_upto_version(type) ⇒ Object



104
105
106
107
108
# File 'lib/cardio.rb', line 104

def assume_migrated_upto_version type
  Cardio.schema_mode(type) do
    ActiveRecord::Schema.assume_migrated_upto_version Cardio.schema(type), Cardio.migration_paths(type)
  end
end

.cacheObject



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

def cache
  @@cache ||= ::Rails.cache
end

.delete_tmp_files(id = nil) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/cardio.rb', line 118

def delete_tmp_files id=nil
  dir = Cardio.paths['files'].existent.first + '/tmp'
  dir += "/#{id}" if id
  FileUtils.rm_rf dir, :secure=>true
rescue
  Rails.logger.info "failed to remove tmp files"
end

.future_stampObject



89
90
91
92
# File 'lib/cardio.rb', line 89

def future_stamp
  ## used in test data
  @@future_stamp ||= Time.local 2020,1,1,0,0,0
end

.gem_rootObject



77
78
79
# File 'lib/cardio.rb', line 77

def gem_root
  CARD_GEM_ROOT
end

.migration_paths(type) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/cardio.rb', line 94

def migration_paths type
  list = paths["db/migrate#{schema_suffix type}"].to_a
  if type == :deck_cards
    list += Card::Loader.mod_dirs.map do |p|
      Dir.glob "#{p}/db/migrate_cards"
    end.flatten
  end
  list
end

.rootObject



73
74
75
# File 'lib/cardio.rb', line 73

def root
  @@config.root
end

.schema(type = nil) ⇒ Object



135
136
137
# File 'lib/cardio.rb', line 135

def schema type=nil
  File.read( schema_stamp_path type ).strip
end

.schema_mode(type) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/cardio.rb', line 126

def schema_mode type
  new_suffix = Cardio.schema_suffix type
  original_suffix = ActiveRecord::Base.table_name_suffix

  ActiveRecord::Base.table_name_suffix = new_suffix
  yield
  ActiveRecord::Base.table_name_suffix = original_suffix
end

.schema_stamp_path(type) ⇒ Object



139
140
141
142
143
144
# File 'lib/cardio.rb', line 139

def schema_stamp_path type
  root_dir = ( type == :deck_cards ? root : gem_root )
  stamp_dir = ENV['SCHEMA_STAMP_PATH'] || File.join( root_dir, 'db' )

  File.join stamp_dir, "version#{ schema_suffix(type) }.txt"
end

.schema_suffix(type) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/cardio.rb', line 110

def schema_suffix type
  case type
  when :core_cards then '_core_cards'
  when :deck_cards then '_deck_cards'
  else ''
  end
end

.set_config(config) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cardio.rb', line 24

def set_config config
  @@config, @@root = config, config.root

  config.autoload_paths += Dir["#{gem_root}/mod/*/lib/**/"]
  config.autoload_paths += Dir["#{gem_root}/lib/**/"]
  config.autoload_paths += Dir["#{root}/mod/*/lib/**/"]

  config.read_only             = !!ENV['WAGN_READ_ONLY']
  config.allow_inline_styles   = false

  config.recaptcha_public_key  = nil
  config.recaptcha_private_key = nil
  config.recaptcha_proxy       = nil

  config.cache_store           = :file_store, 'tmp/cache'
  config.override_host         = nil
  config.override_protocol     = nil

  config.no_authentication     = false
  config.files_web_path        = 'files'

  config.max_char_count        = 200
  config.max_depth             = 20
  config.email_defaults        = nil

  config.token_expiry          = 2.days
  config.revisions_per_page    = 10
  config.space_last_in_multispace = true
  config.closed_search_limit   = 50
end

.set_paths(paths) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cardio.rb', line 56

def set_paths paths
  @@paths = paths
  add_path 'tmp/set', :root => root
  add_path 'tmp/set_pattern', :root => root

  add_path 'mod'
  add_path "db"
  add_path 'db/migrate'
  add_path "db/migrate_core_cards"
  add_path "db/migrate_deck_cards", :root => root, :with => 'db/migrate_cards'
  add_path "db/seeds", :with => "db/seeds.rb"

  add_path 'config/initializers',  :glob => '**/*.rb'
  paths['config/initializers'] << "#{gem_root}/mod/**{,/*/**}/initializers"
  paths['config/initializers'] << "#{root}/mod/**{,/*/**}/initializers"
end