Class: Sudojs::Generators::ClassGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Helpers
Defined in:
lib/generators/sudojs/class/class_generator.rb

Constant Summary

Constants included from Helpers

Helpers::CFG

Instance Method Summary collapse

Methods included from Helpers

#camelize, #css_extension, #html_extension, #js_extension, #namespace, #pascalize, #skip_css?, #which_sudo

Instance Method Details

#copy_class_templateObject

expand the template to <destination>



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/generators/sudojs/class/class_generator.rb', line 77

def copy_class_template
  if @is_application
    # a more definitive indicator
    js_path = @js_app_path
    css_path = @css_app_path
    @path_to_object = namespace
  else
    # the user passed option takes precedence
    js_path = "#{@js_dir || @js_path}/#{@c_name}"
    css_path = "#{@css_views_path}/#{@c_name}"
    @path_to_object = [namespace, '.', @c_camel].join('')
    # make sure the directory is present (create if not)
    unless skip_css?
      ensure_cname_dir(js_path, css_path)
    else
      ensure_cname_dir(js_path)
    end
  end

  # js
  if js_extension == '.js.coffee'
    template 'class.coffee.erb', "#{js_path}/#{@a_name}.js.coffee"
  else
    template 'class.js.erb', "#{js_path}/#{@a_name}#{js_extension}"
  end

  # css
  unless skip_css?
    # new, blank file
    create_file "#{css_path}/#{@a_name}#{css_extension}"
  end
end

#create_or_modify_manifestObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/generators/sudojs/class/class_generator.rb', line 110

def create_or_modify_manifest
  js_pre = '//= '
  css_pre = ' *= '
  if @is_application
    req = "require views/application/#{@a_name}"
  else
    # user passed  option can take precedence
    req = "require #{@js_dir || @js_path}/#{@c_name}/#{@a_name}"
  end
  filename = "app/assets/javascripts/manifests/#{@c_name}.js"
  css_filename = "app/assets/stylesheets/manifests/#{@c_name}.css"
  
  line = js_pre + req
  css_line = css_pre + req + "\n"

  # js
  if File.exist? filename
    # inject_into_file not needed here as we do not use require_tree
    append_to_file filename, line
    gsub_file filename, /^$\n/, ''
  else
    template 'manifest.js.erb', filename
    gsub_file filename, /^$\n/, ''
  end

  # css
  unless skip_css?
    if File.exist? css_filename
      inject_into_file css_filename, css_line, :before => '*/'
      gsub_file css_filename, /^$\n/, ''
    else
      template 'manifest.css.erb', css_filename
      gsub_file css_filename, /^$\n/, ''
    end
  end
end

#modify_view_if_existsObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/generators/sudojs/class/class_generator.rb', line 147

def modify_view_if_exists
  filename = "app/views/#{@c_name}/#{@a_name}.html#{html_extension}"
  if File.exist? filename
    # TODO other pre-processors may need entries here
    if js_extension == '.js.coffee'
      comment = '#'
    else
      comment = '//'
    end
    note = "#{comment} Optional invocation of your new Class.\n#{comment} Place this into a jQuery document ready block."
    content = "\n#{note}\n#{comment} #{@path_to_object}.#{@a_camel} = new #{@path_to_object}.#{@a_pascal}(#{@args});"
    append_to_file filename, content
  end
end

#set_varsObject



15
16
17
18
19
20
21
22
23
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/sudojs/class/class_generator.rb', line 15

def set_vars
  # recognized sudo.js class objects. unrecognized names will
  # be considered 'custom' classes
  @class_names = {
    'Base' => '_.Base',
    'Model' => '_.Model',
    'Container' => '_.Container',
    'DataView' => '_.DataView',
    'View' => '_.View',
    'ViewController' => '_.ViewController'
  }

  @js_app_path = 'app/assets/javascripts/views/application'
  @css_app_path = 'app/assets/stylesheets/views/application'
  @js_model_path = 'app/assets/javascripts/models'
  @js_views_path = 'app/assets/javascripts/views'
  @css_views_path = 'app/assets/stylesheets/views'

  # was the js_dir option passed
  if options[:js_dir] != ''
    @js_dir = "app/assets/javascripts/#{options[:js_dir]}"
  else 
    @js_dir = nil
  end

  ary = route.split('#')
  @c_name = ary[0]
  @c_camel = camelize(@c_name)
  # is this an application level object?
  @is_application = @c_name == 'application'
  # 'action' name not always applicable but thats ok
  @a_name = ary[1]
  @a_camel = camelize @a_name
  # class invocations with `new` should always use a PascalCased name
  @a_pascal = pascalize @a_name
  # either inherits from a sudo.js class or a custom one
  @parent = @class_names[name] || name
  # the arguments vary in the templates for class types
  # also we can make some assumptions about paths
  case @parent
    when '_.Base', '_.Container'
      @args = 'data'
      @js_path = @js_views_path
      @js_req = 'views'
    when '_.Model'
      @args = 'data'
      @js_path = @js_model_path
      @js_req = 'models'
    when '_.View', '_.ViewController', 'DataView'
      @args = 'el, data'
      @js_path = @js_views_path
      @js_req = 'views'
    else
      #default to unknown args and views path
      @args = '/* args */'
      @js_path = @js_views_path
      @js_req = 'views'
  end

end