Module: GemSuit::Application::Actions::InstanceMethods

Defined in:
lib/gem_suit/application/actions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



192
193
194
195
196
197
198
# File 'lib/gem_suit/application/actions.rb', line 192

def method_missing(method, *args)
  if locals.include? method
    locals[method]
  else
    super
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



27
28
29
# File 'lib/gem_suit/application/actions.rb', line 27

def config
  @config
end

#verboseObject

Returns the value of attribute verbose.



27
28
29
# File 'lib/gem_suit/application/actions.rb', line 27

def verbose
  @verbose
end

Instance Method Details

#copy(source, destination) ⇒ Object



172
173
174
175
# File 'lib/gem_suit/application/actions.rb', line 172

def copy(source, destination)
  log :copying, "#{source} -> #{destination}"
  FileUtils.cp expand_path(source), expand_path(destination)
end

#create(string) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gem_suit/application/actions.rb', line 143

def create(string)
  new_files = []

  ["shared", "rails-#{rails_version}"].each do |dir|
    path = File.expand_path dir, templates_path
    next unless File.exists? path
    root = Pathname.new path
    Dir[File.expand_path(string, root.realpath)].each do |file|
      next if File.directory? file
      next if skip? :create, file
      begin
        @relative_path = Pathname.new(file).relative_path_from(root).to_s
        @locals        = nil
        new_files << @relative_path unless new_file?(expand_path(@relative_path)) || File.exists?(stashed(@relative_path))
        log :creating, expand_path(@relative_path)
        template file, expand_path(@relative_path), :verbose => false
      ensure
        @relative_path = nil
      end
    end
  end

  unless new_files.empty?
    File.open(expand_path(".new_files"), "a") do |file|
      file << new_files.collect{|x| "#{x}\n"}.join("")
    end
  end
end

#delete(string) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gem_suit/application/actions.rb', line 110

def delete(string)
  Dir[expand_path(string)].each do |file|
    next if skip? :deleting, file
    log :deleting, file
    File.delete file
  end

  dirname = expand_path File.dirname(string)
  return unless File.exists?(dirname)

  Dir.glob("#{dirname}/*", File::FNM_DOTMATCH) do |file|
    return unless %w(. ..).include? File.basename(file)
  end

  log :deleting, dirname
  Dir.delete dirname
end

#generate(*args) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/gem_suit/application/actions.rb', line 177

def generate(*args)
  return if skip? :generate, args.first
  command = case rails_version
            when 2
              "script/generate"
            when 3
              "rails g"
            end

  execute "#{command} #{args.join(" ")}"
  @ran_generator = true
end

#localsObject



45
46
47
# File 'lib/gem_suit/application/actions.rb', line 45

def locals
  @locals ||= (locals_for_template @relative_path if @relative_path) || {}
end

#locals_for_template(path) ⇒ Object



37
38
39
# File 'lib/gem_suit/application/actions.rb', line 37

def locals_for_template(path)
  # Implement in subclass
end

#restore(string) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/gem_suit/application/actions.rb', line 100

def restore(string)
  Dir[expand_path(string)].each do |file|
    next unless File.exists? stashed(file)
    next if skip? :restore, file
    delete original(file)
    log :restoring, stashed(file)
    File.rename stashed(file), original(file)
  end
end

#restore_all(force = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gem_suit/application/actions.rb', line 49

def restore_all(force = nil)
  if @prepared
    unless force
      log "Cannot (non-forced) restore files after having prepared the test application" unless force.nil?
      return
    end
  end

  return if restore_files == false

  if File.exists?(new_files = expand_path(".new_files"))
    File.readlines(new_files).each do |line|
      delete line.strip
    end
    File.delete new_files
  end

  restore "app/models/**/*.#{STASHED_EXT}"
  restore "app/views/**/*.#{STASHED_EXT}"
  restore "db/**/*.#{STASHED_EXT}"
  restore "public/**/*.#{STASHED_EXT}"
  restore "test/**/*.#{STASHED_EXT}"
  restore "**/*.#{STASHED_EXT}"
  true
end

#restore_filesObject



29
30
31
# File 'lib/gem_suit/application/actions.rb', line 29

def restore_files
  # Implement in subclass
end

#skip(action, string) ⇒ Object



91
92
93
# File 'lib/gem_suit/application/actions.rb', line 91

def skip(action, string)
  ((@skipped_files ||= {})[action] ||= []) << string
end

#skip?(action, string) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/gem_suit/application/actions.rb', line 95

def skip?(action, string)
  return false if @skipped_files.nil? || @skipped_files[action].nil?
  !!@skipped_files[action].detect{|x| File.fnmatch? expand_path(x), string}
end

#stash(string) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/gem_suit/application/actions.rb', line 134

def stash(string)
  Dir[expand_path(string)].each do |file|
    next if new_file?(file) || File.exists?(stashed(file))
    next if skip? :stash, file
    log :stashing, original(file)
    File.rename original(file), stashed(file)
  end
end

#stash_allObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gem_suit/application/actions.rb', line 75

def stash_all
  return if stash_files == false
  stash "Gemfile.lock"
  ["shared", "rails-#{rails_version}"].each do |dir|
    dir_path = File.expand_path dir, templates_path
    next unless File.exists? dir_path
    root = Pathname.new dir_path
    Dir[File.expand_path("**/*", root.realpath)].each do |file|
      next if File.directory? file
      path = Pathname.new file
      write path.relative_path_from(root).to_s
    end
  end
  true
end

#stash_filesObject



33
34
35
# File 'lib/gem_suit/application/actions.rb', line 33

def stash_files
  # Implement in subclass
end

#write(string) ⇒ Object



128
129
130
131
132
# File 'lib/gem_suit/application/actions.rb', line 128

def write(string)
  return if skip? :write, string
  stash string
  create string
end