angus 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/bin/angus CHANGED
@@ -8,6 +8,12 @@ bin_file = Pathname.new(__FILE__).realpath
8
8
  # add self to libpath
9
9
  $:.unshift File.expand_path('../../lib', bin_file)
10
10
 
11
- require 'angus/command'
11
+ require 'angus/commands/angus_command'
12
+ require 'angus/commands/demo_command'
13
+ require 'angus/commands/help_command'
14
+ require 'angus/commands/new_service_command'
15
+ require 'angus/commands/resource_command'
16
+ require 'angus/commands/scaffold_command'
17
+ require 'angus/commands/server_command'
12
18
 
13
- Angus::Command.start
19
+ Angus::AngusCommand.start
@@ -0,0 +1,11 @@
1
+ require 'erb'
2
+ require 'thor'
3
+ require 'thor/runner'
4
+
5
+ require_relative 'command_processor'
6
+
7
+ module Angus
8
+ class AngusCommand < ::Thor::Runner
9
+
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'erb'
2
+ require 'thor'
3
+ require 'thor/actions'
4
+ require_relative '../generator/thor/new_project'
5
+ require_relative '../generator/thor/resource'
6
+
7
+ module Angus
8
+ class CommandProcessor < Thor
9
+ include Thor::Actions
10
+
11
+ RESOURCES_DIR = 'resources'
12
+ DEFINITIONS_DIR = 'definitions'
13
+ SERVICES_DIR = 'services'
14
+
15
+ no_tasks do
16
+ def new_service(name, config = {})
17
+ new_project(name, config)
18
+ end
19
+
20
+ def new_resource(name, actions, config = {})
21
+ resource(name, actions, config)
22
+ end
23
+
24
+ end
25
+ end
26
+ end
27
+
28
+ Angus::CommandProcessor.source_root(File.join(File.dirname(File.expand_path(__FILE__)), '..', 'generator', 'templates'))
@@ -0,0 +1,26 @@
1
+ require_relative 'angus_command'
2
+
3
+ module Angus
4
+ class DemoCommand < Thor::Group
5
+
6
+ def demo
7
+ project_name = 'demo'
8
+
9
+ command_processor.new_service(project_name, {demo: true})
10
+
11
+ command_processor.run("cd #{project_name}")
12
+ Dir.chdir(File.join(Dir.pwd, project_name))
13
+
14
+ command_processor.resource('user', ['index'], {demo: true})
15
+ end
16
+
17
+ private
18
+
19
+ def command_processor
20
+ @command_processor ||= Angus::CommandProcessor.new
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+ Angus::AngusCommand.register(Angus::DemoCommand, 'demo', 'demo [NAME]', 'Generate an empty angus project')
@@ -0,0 +1,21 @@
1
+ module Angus
2
+ class HelpCommand < Thor::Group
3
+
4
+ def help
5
+ puts <<-HELP
6
+ Usages:
7
+ angus new [API NAME] # This generate an empty angus project.
8
+
9
+ angus demo # This generate an demo angus project.
10
+
11
+ Examples:
12
+ angus new great-api
13
+
14
+ This generates a skeletal angus installation in great-api.
15
+ HELP
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+ Angus::AngusCommand.register(Angus::HelpCommand, 'help', 'help', 'Help using the angus command')
@@ -0,0 +1,18 @@
1
+ module Angus
2
+ class NewServiceCommand < Thor::Group
3
+
4
+ argument :name, desc: 'Name of the resource', type: :string
5
+ def new
6
+ command_processor.new_service(self.name)
7
+ end
8
+
9
+ private
10
+
11
+ def command_processor
12
+ @command_processor ||= Angus::CommandProcessor.new
13
+ end
14
+
15
+ end
16
+ end
17
+
18
+ Angus::AngusCommand.register(Angus::NewServiceCommand, 'new', 'new', 'Generate a new service')
@@ -0,0 +1,19 @@
1
+ module Angus
2
+ class ResourceCommand < Thor::Group
3
+
4
+ argument :name, desc: 'Name of the resource', type: :string
5
+ argument :actions, desc: 'Actions to be generated', type: :array, optional: true
6
+ def resource
7
+ command_processor.new_resource(self.name, self.actions)
8
+ end
9
+
10
+ private
11
+
12
+ def command_processor
13
+ @command_processor ||= Angus::CommandProcessor.new
14
+ end
15
+
16
+ end
17
+ end
18
+
19
+ Angus::AngusCommand.register(Angus::ResourceCommand, 'resource', 'resource [NAME] [ACTIONS]', 'Generate a new resource')
@@ -0,0 +1,20 @@
1
+ require_relative 'angus_command'
2
+
3
+ module Angus
4
+ class ScaffoldCommand < Thor::Group
5
+
6
+ argument :name, desc: 'Name of the resource', type: :string
7
+ def scaffold
8
+ command_processor.resource(self.name, %w(create update destroy show index), {scaffold: true})
9
+ end
10
+
11
+ private
12
+
13
+ def command_processor
14
+ @command_processor ||= Angus::CommandProcessor.new
15
+ end
16
+
17
+ end
18
+ end
19
+
20
+ Angus::AngusCommand.register(Angus::ScaffoldCommand, 'scaffold', 'scaffold [NAME]', 'Generate a scaffold of a given model')
@@ -0,0 +1,17 @@
1
+ module Angus
2
+ class ServerCommand < Thor::Group
3
+
4
+ def server
5
+ command_processor.run('rackup', verbose: false)
6
+ end
7
+
8
+ private
9
+
10
+ def command_processor
11
+ @command_processor ||= Angus::CommandProcessor.new
12
+ end
13
+
14
+ end
15
+ end
16
+
17
+ Angus::AngusCommand.register(Angus::ServerCommand, 'server', 'server', 'Run the angus server')
@@ -0,0 +1,6 @@
1
+ <% if is_demo? %>
2
+ ResourceNotFound:
3
+ status_code: 404
4
+ level: error
5
+ description: 'Resource Not Found'
6
+ <% end %>
@@ -1,21 +1,24 @@
1
- <% resource_actions.each do |action_name| %>
2
- <%= action_name%>:
3
- name:
4
- description: |
5
-
6
- path:
7
- method:
1
+ <% unless actions.nil? %>
2
+ <% actions.each do |action_name| %>
3
+ <%= action_name %>:
4
+ name: '<%= action_name %>'
5
+ description: '<%= action_description(action_name) %>'
6
+ path: '<%= action_path(action_name) %>'
7
+ method: '<%= action_method(action_name) %>'
8
+ <% unless is_demo? %>
8
9
  uri:
9
10
  - element:
10
11
  description:
12
+ <% end %>
11
13
 
12
14
  response:
13
- - element:
14
- description:
15
- required:
16
- type:
15
+ - element: '<%= response_element(action_name) %>'
16
+ description: '<%= response_description(action_name) %>'
17
+ required: <%= response_required(action_name) %>
18
+ type: <%= response_type(action_name) %>
17
19
 
18
20
  messages:
19
- - key:
20
- description:
21
+ - key: <%= message_key(action_name) %>
22
+ description: '<%= message_description(action_name) %>'
23
+ <% end %>
21
24
  <% end %>
@@ -0,0 +1,11 @@
1
+ <% if is_demo? %>
2
+ user_profile:
3
+ - field: id
4
+ description: User identifier
5
+ required: true
6
+ type: integer
7
+ - field: name
8
+ description: User name
9
+ required: true
10
+ type: string
11
+ <% end %>
@@ -1,6 +1,11 @@
1
1
  class <%= classify(resource_name) %> < Angus::BaseResource
2
- <% resource_actions.each do |action_name| %>
3
- def <%= action_name %>
2
+ <% unless actions.nil? %>
3
+ <% actions.each do |action_name| %>
4
+ def
5
+
6
+ {users: [{id: 1, name: 'User one'}, {id: 2, name: 'User two'}]}
7
+ <% end %>
4
8
  end
5
9
  <% end %>
10
+ <% end %>
6
11
  end
@@ -0,0 +1,36 @@
1
+ module Angus
2
+ module FileHandler
3
+
4
+ # Override if you want a custom file mapping.
5
+ def mapping
6
+ {}
7
+ end
8
+
9
+ def is_erb?(file)
10
+ file.end_with?('.erb')
11
+ end
12
+
13
+ def copy_erb_file(file, name, base_path = nil)
14
+ base_path = name if base_path.nil?
15
+
16
+ tmp_file = Tempfile.new(File.basename(file))
17
+
18
+ source = File.expand_path(base.find_in_source_paths(file.to_s))
19
+ content = ERB.new(File.binread(source)).result(binding)
20
+
21
+ File.open(tmp_file.path, 'w') { |f| f << content }
22
+ tmp_file.close
23
+
24
+ base.copy_file(tmp_file.path, File.join(base_path, filename_resolver(file, name)))
25
+ end
26
+
27
+ def filename_resolver(file, app_name)
28
+ if mapping[file].nil?
29
+ file.gsub('.erb', '')
30
+ else
31
+ mapping[file].call(self, app_name)
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,65 @@
1
+ module Angus
2
+ module HelperMethods
3
+
4
+ def pluralize(word)
5
+ uncountable = %w(equipment information rice money species series fish sheep)
6
+ rules = []
7
+
8
+ irregular = {
9
+ 'person' => 'people',
10
+ 'man' => 'men',
11
+ 'child' => 'children',
12
+ 'sex' => 'sexes',
13
+ 'move' => 'moves'
14
+ }
15
+
16
+ irregular.each do |word, replacement|
17
+ rules << [word, replacement]
18
+ rules << [replacement, replacement]
19
+ end
20
+
21
+ rules << [/^(ox)$/i , '\1en' ]
22
+ rules << [/(quiz)$/i , '\1zes' ]
23
+ rules << [/([m|l])ouse$/i , '\1ice' ]
24
+ rules << [/(matr|vert|ind)ix|ex$/i, '\1ices' ]
25
+ rules << [/(x|ch|ss|sh)$/i , '\1es' ]
26
+ rules << [/([^aeiouy]|qu)ies$/i , '\1y' ]
27
+ rules << [/([^aeiouy]|qu)y$/i , '\1ies' ]
28
+ rules << [/(hive)$/i , '\1s' ]
29
+ rules << [/(?:([^f])fe|([lr])f)$/i, '\1\2ves']
30
+ rules << [/sis$/i , 'ses' ]
31
+ rules << [/([ti])um$/i , '\1a' ]
32
+ rules << [/(buffal|tomat)o$/i , '\1oes' ]
33
+ rules << [/(bu)s$/i , '\1ses' ]
34
+ rules << [/(alias|status)$/i , '\1es' ]
35
+ rules << [/(octop|vir)us$/i , '\1i' ]
36
+ rules << [/(ax|test)is$/i , '\1es' ]
37
+ rules << [/s$/i , 's' ]
38
+ rules << [/$/ , 's' ]
39
+
40
+ result = word.to_s.dup
41
+
42
+ if word.empty? || uncountable.include?(result.downcase[/\b\w+\Z/])
43
+ result
44
+ else
45
+ rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
46
+ result
47
+ end
48
+ end
49
+
50
+ def classify(string)
51
+ string.sub(/^[a-z\d]*/) { $&.capitalize }.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
52
+ end
53
+
54
+ def underscore(camel_cased_word)
55
+ word = camel_cased_word.to_s.dup
56
+ word.gsub!(/::/, '/')
57
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
58
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
59
+ word.tr!('-', '_')
60
+ word.downcase!
61
+ word
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,92 @@
1
+ require_relative 'helper_methods'
2
+ require_relative 'file_handler'
3
+
4
+ class Thor
5
+ module Actions
6
+
7
+ # Creates a new project.
8
+ #
9
+ # @param [String] name The name of the resource.
10
+ # @param [Hash] config
11
+ #
12
+ # @example
13
+ #
14
+ # new_project 'great-api'
15
+ #
16
+ def new_project(name, config = {})
17
+ action NewProject.new(self, name, config)
18
+ end
19
+
20
+ class NewProject #:nodoc:
21
+ include Angus::HelperMethods
22
+ include Angus::FileHandler
23
+
24
+ FILE_MAPPINGS = {
25
+ 'services/service.rb.erb' => -> command, app_name { File.join(Angus::CommandProcessor::SERVICES_DIR, "#{command.underscore(command.classify(app_name))}.rb") },
26
+ 'resources/resource.rb.erb' => -> command, name { "#{command.underscore(command.classify(name))}.rb" },
27
+ 'definitions/operations.yml.erb' => -> command, _ { File.join(command.resource_name, 'operations.yml') }
28
+ }
29
+
30
+ attr_accessor :base, :config, :app_name
31
+
32
+ # Creates a new project.
33
+ #
34
+ # @param [Thor::Base] base A Thor::Base instance
35
+ def initialize(base, name, config)
36
+ @base, @config = base, { :verbose => true }.merge(config)
37
+
38
+ self.app_name = name
39
+ end
40
+
41
+ def invoke!
42
+ base.empty_directory(app_name)
43
+
44
+ new_app_directories.each do |directory|
45
+ base.empty_directory(File.join(app_name, directory))
46
+ end
47
+
48
+ new_app_files.each do |file|
49
+ if is_erb?(file)
50
+ copy_erb_file(file, app_name)
51
+ else
52
+ base.copy_file(file, File.join(app_name, file))
53
+ end
54
+ end
55
+ end
56
+
57
+ def revoke!
58
+
59
+ end
60
+
61
+ def mapping
62
+ FILE_MAPPINGS
63
+ end
64
+
65
+ protected
66
+
67
+ def is_generating_demo?
68
+ @config[:demo]
69
+ end
70
+
71
+ def new_app_directories
72
+ [
73
+ Angus::CommandProcessor::DEFINITIONS_DIR,
74
+ Angus::CommandProcessor::RESOURCES_DIR,
75
+ Angus::CommandProcessor::SERVICES_DIR
76
+ ]
77
+ end
78
+
79
+ def new_app_files
80
+ %w(
81
+ Gemfile.erb
82
+ config.ru.erb
83
+ services/service.rb.erb
84
+ definitions/messages.yml.erb
85
+ definitions/representations.yml.erb
86
+ definitions/service.yml.erb
87
+ )
88
+ end
89
+
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,141 @@
1
+ require_relative 'helper_methods'
2
+ require_relative 'file_handler'
3
+
4
+ class Thor
5
+ module Actions
6
+
7
+ # Creates a new resource.
8
+ #
9
+ # @param [String] name The name of the resource.
10
+ # @param [Hash] config
11
+ #
12
+ # @example
13
+ #
14
+ # resource 'user', 'resouces', 'definitions'
15
+ #
16
+ def resource(name, actions, config = {})
17
+ action Resource.new(self, name, actions, config)
18
+ end
19
+
20
+ class Resource #:nodoc:
21
+ include Angus::HelperMethods
22
+ include Angus::FileHandler
23
+
24
+ FILE_MAPPINGS = {
25
+ 'resources/resource.rb.erb' => -> command, name { "#{command.underscore(command.classify(name))}.rb" },
26
+ 'definitions/operations.yml.erb' => -> command, _ { File.join(command.underscore(command.resource_name), 'operations.yml') }
27
+ }
28
+
29
+ attr_accessor :base, :config, :resource_name, :actions,
30
+ :resource_directory, :definition_directory, :service_directory
31
+
32
+ # Creates a new resource.
33
+ #
34
+ # @param [Thor::Base] base A Thor::Base instance
35
+ def initialize(base, name, actions, config)
36
+ @base, @config = base, { :verbose => true }.merge(config)
37
+
38
+ self.resource_name = pluralize(name)
39
+ self.actions = actions
40
+ self.resource_directory = File.join(Dir.pwd, Angus::CommandProcessor::RESOURCES_DIR)
41
+ self.definition_directory = File.join(Dir.pwd, Angus::CommandProcessor::DEFINITIONS_DIR)
42
+ self.service_directory = File.join(Dir.pwd, Angus::CommandProcessor::SERVICES_DIR)
43
+ end
44
+
45
+ def invoke!
46
+ base.empty_directory(resource_directory)
47
+ base.empty_directory(definition_directory)
48
+
49
+ copy_erb_file('resources/resource.rb.erb', underscore(resource_name), resource_directory)
50
+ copy_erb_file('definitions/operations.yml.erb', underscore(resource_name), definition_directory)
51
+ insert_into_services(" register :#{underscore(resource_name)}\n")
52
+ end
53
+
54
+ def revoke!
55
+
56
+ end
57
+
58
+ protected
59
+
60
+ def action_description(action_name)
61
+ action_name if is_demo?
62
+ end
63
+
64
+ def action_path(action_name)
65
+ if is_demo?
66
+ '/users'
67
+ elsif is_scaffold?
68
+ case action_name
69
+ when 'show', 'update', 'destroy'
70
+ "#{underscore(resource_name)}/:id"
71
+ else
72
+ "#{underscore(resource_name)}"
73
+ end
74
+ end
75
+ end
76
+
77
+ def action_method(action_name)
78
+ if is_demo?
79
+ 'get'
80
+ elsif is_scaffold?
81
+ case action_name
82
+ when 'create'
83
+ 'post'
84
+ when 'update'
85
+ 'put'
86
+ when 'destroy'
87
+ 'delete'
88
+ else
89
+ 'get'
90
+ end
91
+ end
92
+ end
93
+
94
+ def response_element(action_name)
95
+ 'users' if is_demo?
96
+ end
97
+
98
+ def response_description(action_name)
99
+ 'Users profiles' if is_demo?
100
+ end
101
+
102
+ def response_required(action_name)
103
+ true if is_demo?
104
+ end
105
+
106
+ def response_type(action_name)
107
+ 'user_profile' if is_demo?
108
+ end
109
+
110
+ def message_key(action_name)
111
+ 'ResourceNotFound' if is_demo?
112
+ end
113
+
114
+ def message_description(action_name)
115
+ 'Resource Not Found' if is_demo?
116
+ end
117
+
118
+ def mapping
119
+ FILE_MAPPINGS
120
+ end
121
+
122
+ def is_scaffold?
123
+ config[:scaffold]
124
+ end
125
+
126
+ def is_demo?
127
+ config[:demo]
128
+ end
129
+
130
+ def insert_into_services(content)
131
+ config = {}
132
+ config.merge!(:after => /def configure\n|def configure .*\n/)
133
+
134
+ file = Dir[File.join(service_directory, '*.*')].first
135
+
136
+ base.insert_into_file(file, *([content] << config))
137
+ end
138
+
139
+ end
140
+ end
141
+ end
data/lib/angus/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Angus
2
- VERSION = '0.0.7'
3
- end
2
+ VERSION = '0.0.8'
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-12-30 00:00:00.000000000 Z
14
+ date: 2014-01-03 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: thor
@@ -213,9 +213,16 @@ files:
213
213
  - lib/angus/marshallings/marshalling.rb
214
214
  - lib/angus/marshallings/exceptions.rb
215
215
  - lib/angus/marshallings/base.rb
216
- - lib/angus/generator.rb
217
216
  - lib/angus/status_codes.rb
218
217
  - lib/angus/version.rb
218
+ - lib/angus/commands/angus_command.rb
219
+ - lib/angus/commands/new_service_command.rb
220
+ - lib/angus/commands/server_command.rb
221
+ - lib/angus/commands/resource_command.rb
222
+ - lib/angus/commands/scaffold_command.rb
223
+ - lib/angus/commands/demo_command.rb
224
+ - lib/angus/commands/command_processor.rb
225
+ - lib/angus/commands/help_command.rb
219
226
  - lib/angus/base_actions.rb
220
227
  - lib/angus/request_handler.rb
221
228
  - lib/angus/renders/html_render.rb
@@ -226,12 +233,15 @@ files:
226
233
  - lib/angus/generator/templates/README.md
227
234
  - lib/angus/generator/templates/config.ru.erb
228
235
  - lib/angus/generator/templates/services/service.rb.erb
229
- - lib/angus/generator/templates/definitions/representations.yml
230
236
  - lib/angus/generator/templates/definitions/service.yml.erb
231
237
  - lib/angus/generator/templates/definitions/operations.yml.erb
232
- - lib/angus/generator/templates/definitions/messages.yml
238
+ - lib/angus/generator/templates/definitions/representations.yml.erb
239
+ - lib/angus/generator/templates/definitions/messages.yml.erb
240
+ - lib/angus/generator/thor/new_project.rb
241
+ - lib/angus/generator/thor/resource.rb
242
+ - lib/angus/generator/thor/helper_methods.rb
243
+ - lib/angus/generator/thor/file_handler.rb
233
244
  - lib/angus/base_resource.rb
234
- - lib/angus/command.rb
235
245
  - lib/angus/response.rb
236
246
  - lib/angus/exceptions.rb
237
247
  - lib/angus/middleware/exception_handler.rb
data/lib/angus/command.rb DELETED
@@ -1,27 +0,0 @@
1
- require 'erb'
2
- require 'thor'
3
-
4
- require_relative 'generator'
5
-
6
- module Angus
7
- class Command < Thor
8
-
9
- desc 'new [NAME]', 'Generate a new service'
10
- def new(name)
11
- generator.new_service(name)
12
- end
13
-
14
- desc 'resource [NAME]', 'Generate a new resource'
15
- method_option :actions, aliases: '-a', type: :array, desc: 'Generate the given actions for the resource'
16
- def resource(name)
17
- generator.resource(name)
18
- end
19
-
20
- private
21
-
22
- def generator
23
- @generator ||= Angus::Generator.new
24
- end
25
-
26
- end
27
- end
@@ -1,141 +0,0 @@
1
- require 'erb'
2
- require 'thor'
3
- require 'thor/actions'
4
-
5
- module Angus
6
- class Generator < Thor
7
- include Thor::Actions
8
-
9
- RESOURCES_DIR = 'resources'
10
- DEFINITIONS_DIR = 'definitions'
11
- SERVICES_DIR = 'services'
12
-
13
- NEW_APP_DIRECTORIES = %W(#{DEFINITIONS_DIR} #{RESOURCES_DIR} #{SERVICES_DIR})
14
-
15
- NEW_APP_FILES = %w(
16
- Gemfile.erb
17
- config.ru.erb
18
- services/service.rb.erb
19
- definitions/messages.yml
20
- definitions/representations.yml
21
- definitions/service.yml.erb
22
- )
23
-
24
- FILE_MAPPINGS = {
25
- 'services/service.rb.erb' => -> command, app_name { File.join(SERVICES_DIR, "#{command.underscore(command.classify(app_name))}.rb") },
26
- 'resources/resource.rb.erb' => -> command, name { "#{command.underscore(command.classify(name))}.rb" },
27
- 'definitions/operations.yml.erb' => -> command, _ { File.join(command.resource_name, 'operations.yml') }
28
- }
29
-
30
- no_commands do
31
- def new_service(name)
32
- app_name(name)
33
-
34
- empty_directory(app_name)
35
-
36
- NEW_APP_DIRECTORIES.each do |directory|
37
- empty_directory(File.join(app_name, directory))
38
- end
39
-
40
- NEW_APP_FILES.each do |file|
41
- if is_erb?(file)
42
- copy_erb_file(file, app_name)
43
- else
44
- copy_file(file, File.join(app_name, file))
45
- end
46
- end
47
- end
48
-
49
- def resource(name)
50
- resource_name(name)
51
- resource_actions(options[:actions])
52
-
53
- empty_directory(RESOURCES_DIR)
54
- empty_directory(DEFINITIONS_DIR)
55
-
56
- copy_erb_file('resources/resource.rb.erb', resource_name, RESOURCES_DIR)
57
- copy_erb_file('definitions/operations.yml.erb', resource_name, DEFINITIONS_DIR)
58
- insert_into_services(" register :#{resource_name}\n")
59
- end
60
-
61
- def app_name(name = nil)
62
- if name.nil?
63
- @app_name
64
- else
65
- @app_name = name
66
- end
67
- end
68
-
69
- def resource_name(name = nil)
70
- if name.nil?
71
- @resource_name
72
- else
73
- @resource_name = name
74
- end
75
- end
76
-
77
- def resource_actions(actions = nil)
78
- if actions.nil?
79
- @resource_actions || []
80
- else
81
- @resource_actions = actions
82
- end
83
- end
84
-
85
- def classify(string)
86
- string.sub(/^[a-z\d]*/) { $&.capitalize }.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
87
- end
88
-
89
- def underscore(camel_cased_word)
90
- word = camel_cased_word.to_s.dup
91
- word.gsub!(/::/, '/')
92
- word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
93
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
94
- word.tr!('-', '_')
95
- word.downcase!
96
- word
97
- end
98
- end
99
-
100
- private
101
-
102
- def is_erb?(file)
103
- file.end_with?('.erb')
104
- end
105
-
106
- def copy_erb_file(file, name, base_path = nil)
107
- base_path = name if base_path.nil?
108
-
109
- tmp_file = Tempfile.new(File.basename(file))
110
-
111
- source = File.expand_path(find_in_source_paths(file.to_s))
112
- content = ERB.new(File.binread(source)).result(binding)
113
-
114
- File.open(tmp_file.path, 'w') { |f| f << content }
115
- tmp_file.close
116
-
117
- copy_file(tmp_file.path, File.join(base_path, filename_resolver(file, name)))
118
- end
119
-
120
- def insert_into_services(content)
121
- config = {}
122
- config.merge!(:after => /def configure\n|def configure .*\n/)
123
-
124
- file = Dir[File.join(Dir.pwd, SERVICES_DIR, '*.*')].first
125
-
126
- insert_into_file(file, *([content] << config))
127
- end
128
-
129
- def filename_resolver(file, app_name)
130
- if FILE_MAPPINGS[file].nil?
131
- file.gsub('.erb', '')
132
- else
133
- FILE_MAPPINGS[file].call(self, app_name)
134
- end
135
- end
136
-
137
- end
138
- end
139
-
140
- Angus::Generator.source_root(File.join(File.dirname(File.expand_path(__FILE__)), 'generator',
141
- 'templates'))