petro 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f5fb5a0076bf0e8384f4a2fdd22a74f11782e20b
4
+ data.tar.gz: 7e082856643d4f03b3a69058d84d28bbefa330a0
5
+ SHA512:
6
+ metadata.gz: 46b1a327a892dcbbebed0f701c7a100cb44257e7142e8df7358641949c015f856b372fe3ec11e6dd0c11390a9bada16e8ced0eb293eb7754797ddc3b8ab33103
7
+ data.tar.gz: 7998c664275bb2663ce80e8cae5f0254d9549b3ae22541ea06b59c589282c472009d0ba4d6bba1a4d36d2c26187222be63dca0deadd7418ef8347db8853a19b5
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014-2015 Robert Evans
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ # Petro
2
+
3
+ Work in Progress. User Beware.
4
+
5
+ Reads in a hash and uses that to create rails commands that can be ran to generate code.
6
+
7
+ ## Usage
8
+
9
+ ```
10
+ group :development do
11
+ gem 'petro', github: 'revans/petro'
12
+ end
13
+ ```
14
+
15
+ Look at the test/assets/feature.yml file to get an idea of the structure. It's a yaml file that is
16
+ generated into a hash before it's passed to petro to generate the rails commands.
@@ -0,0 +1,6 @@
1
+ require 'active_support/all'
2
+ require 'petro/version'
3
+ require 'petro/formatter/controller'
4
+ require 'petro/formatter/model'
5
+ require 'petro/format_feature'
6
+
@@ -0,0 +1,77 @@
1
+
2
+ class FormatFeature
3
+ attr_reader :features
4
+
5
+ def initialize(options)
6
+ @features = build_features(options)
7
+ end
8
+
9
+
10
+
11
+ # What do you want??
12
+ #
13
+ # * All I care about is the code to generate controllers
14
+ # and models.
15
+ #
16
+ # * A feature is both a controller and model, if they both
17
+ # exist.
18
+ #
19
+ def build_features(options)
20
+ options['features'].map do |f|
21
+ Feature.new(f['name'], f)
22
+ end
23
+ end
24
+
25
+
26
+
27
+ class Feature
28
+ attr_reader :controller, :model, :name
29
+ def initialize(name, options)
30
+ @name = name
31
+ @controller = ::Formatter::Controller.new(options.fetch('controller', {}))
32
+ @model = ::Formatter::Model.new(options.fetch('model', {}))
33
+ end
34
+
35
+
36
+ def has_model?
37
+ @model.attributes.present?
38
+ end
39
+
40
+ def has_controller?
41
+ @controller.has_methods?
42
+ end
43
+
44
+ def scaffold?
45
+ has_model? && has_controller?
46
+ end
47
+
48
+ def code
49
+ return scaffold_code if scaffold?
50
+ [model_code, controller_code].compact.join(' && ')
51
+ end
52
+
53
+
54
+ def scaffold_code
55
+ "rails g scaffold #{name.pluralize} #{model.attributes}".strip
56
+ end
57
+
58
+ def model_code
59
+ "rails g model #{name.singularize} #{model.attributes}".strip
60
+ end
61
+
62
+
63
+ def controller_code
64
+ if controller.all_methods?
65
+ "rails g scaffold_controller #{name.pluralize}".strip
66
+ elsif controller.has_methods?
67
+ "rails g controller #{name.pluralize} #{controller_methods}".strip
68
+ end
69
+ end
70
+
71
+
72
+ def controller_methods
73
+ controller.valid_methods.join(' ')
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,43 @@
1
+
2
+ module Formatter
3
+ class Controller
4
+ attr_reader :methods
5
+
6
+ def initialize(methods)
7
+ @methods = methods.fetch('methods', [])
8
+ end
9
+
10
+
11
+ def valid_methods
12
+ @valid_methods ||= @methods.map do |key, value|
13
+ key if value
14
+ end.compact
15
+ end
16
+
17
+ def has_methods?
18
+ methods.present?
19
+ end
20
+
21
+ def all_methods?
22
+ all_methods == valid_methods
23
+ end
24
+
25
+
26
+ private
27
+
28
+ def build_generate_command
29
+ if all_methods?
30
+ "rails g scaffold_controller #{name}"
31
+ else
32
+ "rails g controller #{name} #{valid_methods.join(' ')}"
33
+ end
34
+ end
35
+
36
+
37
+ def all_methods
38
+ %w|index show new create edit update destroy|
39
+ end
40
+
41
+
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+
2
+ module Formatter
3
+ class Model
4
+ def initialize(attributes)
5
+ @attributes = attributes.fetch('attributes', [])
6
+ end
7
+
8
+ def attributes
9
+ build_code
10
+ end
11
+
12
+ private
13
+
14
+ def build_code
15
+ @attributes.map do |att|
16
+ "#{att.first}:#{(att.last || 'string')}"
17
+ end.compact.join(' ')
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ module Petro
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+ PRE = nil
7
+
8
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
9
+ end
10
+
11
+ def self.gem_version
12
+ ::Gem::Version.new(VERSION::STRING)
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'gem_version'
2
+
3
+ module Petro
4
+ def self.version
5
+ gem_version
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: petro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Robert Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.4'
55
+ description:
56
+ email: robert@codewranglers.org
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - MIT-LICENSE
62
+ - README.md
63
+ - lib/petro.rb
64
+ - lib/petro/format_feature.rb
65
+ - lib/petro/formatter/controller.rb
66
+ - lib/petro/formatter/model.rb
67
+ - lib/petro/gem_version.rb
68
+ - lib/petro/version.rb
69
+ homepage: http://www.codewranglers.org
70
+ licenses:
71
+ - MIT-LICENSE
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements:
88
+ - none
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.5
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Reads a hash, generates Rails commands that can be ran to generate rails
94
+ code
95
+ test_files: []