Module: Rails::Generators::Actions

Defined in:
lib/generators/core_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recipesObject

Returns the value of attribute recipes.



6
7
8
# File 'lib/generators/core_extensions.rb', line 6

def recipes
  @recipes
end

#stategiesObject

Returns the value of attribute stategies.



5
6
7
# File 'lib/generators/core_extensions.rb', line 5

def stategies
  @stategies
end

#template_optionsObject (readonly)

Returns the value of attribute template_options.



7
8
9
# File 'lib/generators/core_extensions.rb', line 7

def template_options
  @template_options
end

Instance Method Details

#execute_stategiesObject



15
16
17
# File 'lib/generators/core_extensions.rb', line 15

def execute_stategies
  stategies.each {|stategy| stategy.call }
end

#gem_version(gem_name, version_name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/generators/core_extensions.rb', line 33

def gem_version(gem_name, version_name)
  new_gemfile = ""
  added = false
  File.open('Gemfile', 'r') do |file|
    new_gemline = %(gem "#{gem_name}") + (version_name ? %(, "#{version_name}") : '') + '\n'
    while (line = file.readline)
      if line.index("gem") == 0 && (line.include? gem_name)
        new_gemfile += new_gemline
        added = true
      else
        new_gemfile += line
      end
    end
    unless added
      new_gemfile += new_gemline
    end
  end
  File.open('Gemfile', 'w').write(new_gemfile)
end

#gemfileObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/core_extensions.rb', line 19

def gemfile()
  File.open(File.expand_path('Gemfile', WheelsGenerator::SOURCE_ROOT)) do |gemfile|
    while line = gemfile.readline
      line = line.split("#").first.strip
      if line.index('gem') == 0
        pcs = line.split(' ')
        gem = pcs[1].gsub(/[,"']/, '')
        version = pcs[2].try(:gsub, /[,"']/, '')
        gem_version gem, version
      end
    end
  end
end

#have_migration?(name) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/generators/core_extensions.rb', line 83

def have_migration?(name)
  !migrations.select{|t| t.include? name}.empty?
end

#initialize_templaterObject



9
10
11
12
13
# File 'lib/generators/core_extensions.rb', line 9

def initialize_templater
  self.recipes = []
  @stategies = []
  @template_options = {}
end

#load_optionsObject



53
54
55
56
# File 'lib/generators/core_extensions.rb', line 53

def load_options
  @template_options[:design] = ask("Which design framework? [none(default), compass]: ").downcase
  @template_options[:design] = "none" if @template_options[:design].nil?
end

#load_snippet(name, group) ⇒ Object

TODO: Refactor loading of files



65
66
67
68
# File 'lib/generators/core_extensions.rb', line 65

def load_snippet(name, group)
  path = File.expand_path name, snippet_path(group)
  File.read path
end

#load_template(name, group, match = {}) ⇒ Object



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

def load_template(name, group, match={})
  path = File.expand_path name, template_path(group)
  contents = File.read path
  match.each do |key, value|
    contents.gsub! "\%#{key.to_s}\%", value
  end
  contents
end

#migrationsObject



79
80
81
# File 'lib/generators/core_extensions.rb', line 79

def migrations
  Dir.new('db/migrate').entries
end

#recipe(name) ⇒ Object



58
59
60
61
# File 'lib/generators/core_extensions.rb', line 58

def recipe(name)
  recipes << name
  File.join File.dirname(__FILE__), 'recipes', "#{name}.rb"
end

#snippet_path(name) ⇒ Object



87
88
89
# File 'lib/generators/core_extensions.rb', line 87

def snippet_path(name)
  File.join(File.dirname(__FILE__), 'snippets', name)
end

#template_path(name) ⇒ Object



91
92
93
# File 'lib/generators/core_extensions.rb', line 91

def template_path(name)
  File.join(File.dirname(__FILE__), 'templates', name)
end