melodiest 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00c23e4c16d216fa1999cc6ad4f21c7f4e4d7e6e
4
- data.tar.gz: 3cbde05684e9a5b653cd617e1614711322da8c0f
3
+ metadata.gz: 61a1ca9f0d801c00782ea2292bd6a76e68af04e1
4
+ data.tar.gz: 70dc5c6791cb7a7cc97fd6fb4ff0a96e7438d7f9
5
5
  SHA512:
6
- metadata.gz: 18e669f816f85dcaeaa2337d46f04c96c48c75927e80acb116a3ee4016cae80e63a3bafab49804d7b6a324b31caadb31bba6a8bd0b97c5d90ba2f28de4bfe977
7
- data.tar.gz: 0c8fa8028ac85f7b9d8cb01dcd9578163ddb16c5ff2d424ad56c2be123b08f0bf1cd8fb52ad27f39c15f19381560a516d6d3d6ed13422638d5069362a65ffa3f
6
+ metadata.gz: b24717b4ecfd760e49e7ee8522ee756ee0aae290f05c528e40c83687df3f3118703aecc175530f7b05aa8f3fcd31984133c659f1b0834f0183139eb69b03aaa7
7
+ data.tar.gz: 39816f9ac983ed88f7167daeb73e8791a40ff5d875301b21270561c3692bee43af1b2295ec257e86488857a76d99800196d44636fcf0fcaf57477f03d8b6145d
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'fakefs', '0.6.7', require: 'fakefs/safe'
2
4
 
3
5
  gemspec
data/Gemfile.lock CHANGED
@@ -1,16 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- melodiest (0.1.0)
4
+ melodiest (0.2.0)
5
5
  sinatra (= 1.4.5)
6
+ sinatra-contrib (= 1.4.2)
6
7
  thin (= 1.6.3)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
12
+ backports (3.6.4)
11
13
  daemons (1.1.9)
12
14
  diff-lcs (1.2.5)
13
- eventmachine (1.0.4)
15
+ eventmachine (1.0.7)
16
+ fakefs (0.6.7)
17
+ multi_json (1.10.1)
14
18
  rack (1.6.0)
15
19
  rack-protection (1.5.3)
16
20
  rack
@@ -32,6 +36,13 @@ GEM
32
36
  rack (~> 1.4)
33
37
  rack-protection (~> 1.4)
34
38
  tilt (~> 1.3, >= 1.3.4)
39
+ sinatra-contrib (1.4.2)
40
+ backports (>= 2.0)
41
+ multi_json
42
+ rack-protection
43
+ rack-test
44
+ sinatra (~> 1.4.0)
45
+ tilt (~> 1.3)
35
46
  thin (1.6.3)
36
47
  daemons (~> 1.0, >= 1.0.9)
37
48
  eventmachine (~> 1.0)
@@ -43,6 +54,7 @@ PLATFORMS
43
54
 
44
55
  DEPENDENCIES
45
56
  bundler (~> 1.7)
57
+ fakefs (= 0.6.7)
46
58
  melodiest!
47
59
  rack-test (= 0.6.3)
48
60
  rspec (= 3.1.0)
data/README.md CHANGED
@@ -16,40 +16,58 @@ gem 'melodiest'
16
16
  ```
17
17
 
18
18
  ### How to Use
19
+ #### Command
20
+ generate app in current directory
21
+ ```
22
+ melodiest -n my_app
23
+ ```
24
+ generate app in target directory
25
+ ```
26
+ melodiest -n my_app -d target/dir
27
+ ```
19
28
 
29
+ #### Example Code
20
30
  Because Melodiest is already required Sinatra, you don't have to require 'sinatra' anymore, just require 'melodiest'.
21
31
 
22
32
  `Melodiest::Application` is subclass from `Sinatra::Application` and by default is using configuration from `Melodiest::Setting.setup` method.
23
33
 
24
34
 
25
35
  ```ruby
26
- # mysinatraapp.rb
36
+ # my_app.rb
27
37
 
28
- require 'melodiest'
29
- require 'melodiest/auth'
38
+ require 'melodiest/auth/http'
30
39
 
31
- class App < Melodiest::Application
40
+ class App < Melodiest::Application'
41
+ configure do
42
+ # Load up database and such
43
+ end
44
+
32
45
  helpers Melodiest::Auth::Http
46
+ end
33
47
 
34
- get "/" do
35
- "hello world!"
36
- end
48
+ # app/routes/my_routes.rb
37
49
 
38
- get "/protected" do
39
- authorized! "myhttpauthusername", "myhttpauthpassword"
40
- end
50
+ get "/" do
51
+ "hello world!"
52
+ end
41
53
 
54
+ get "/protected" do
55
+ authorized! "myhttpauthusername", "myhttpauthpassword"
56
+ "welcome!"
42
57
  end
43
58
 
44
59
  ```
60
+ #### Run the server
61
+ ```
62
+ bundle exec rackup
63
+ ```
45
64
 
46
- #### Configuration
47
65
 
48
- Configuration
66
+ ### Configuration
49
67
 
50
- * `set :server, 'thin'`
68
+ See [melodiest/config.yml](https://github.com/kuntoaji/melodiest/blob/master/lib/melodiest/config.yml)
51
69
 
52
- #### Helpers
70
+ ### Helpers
53
71
 
54
72
  Helper methods
55
73
 
data/bin/melodiest ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/melodiest/command'
4
+
5
+ puts Melodiest::Command.parse ARGV
@@ -0,0 +1,53 @@
1
+ require 'optparse'
2
+ require_relative 'generator'
3
+
4
+ module Melodiest
5
+
6
+ class Command
7
+ def self.parse(options)
8
+ args = {name: nil, dir: nil}
9
+ result = ""
10
+
11
+ option_parser = OptionParser.new do |opts|
12
+ opts.banner = "Usage: melodiest [options]"
13
+
14
+ opts.on("-h", "--help", "Print this help") do
15
+ result = opts.to_s
16
+ end
17
+
18
+ opts.on("-v", "--version", "Show version") do
19
+ result = Melodiest::VERSION
20
+ end
21
+
22
+ opts.on("-nNAME", "--name=NAME", "generate app with name from this option") do |name|
23
+ args[:name] = name
24
+ end
25
+
26
+ opts.on("-dDIR", "--dir=DIR", "instead of current directory, generate app in target DIR") do |dir|
27
+ args[:dir] = dir
28
+ end
29
+
30
+ end
31
+
32
+ option_parser.parse! options
33
+
34
+ result = Command.run(args[:name], args[:dir]) unless args[:name].nil?
35
+
36
+ result
37
+ end
38
+
39
+ def self.run(app_name, target_dir)
40
+ generator = Melodiest::Generator.new app_name, target_dir
41
+
42
+ generator.generate_gemfile
43
+ generator.generate_bundle_config
44
+ generator.generate_app
45
+
46
+ msg = "#{app_name} is successfully generated"
47
+ msg << " in #{target_dir}" if target_dir
48
+
49
+ msg
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1 @@
1
+ server: thin
@@ -0,0 +1,61 @@
1
+ require 'fileutils'
2
+ require_relative 'version'
3
+
4
+ module Melodiest
5
+
6
+ class Generator
7
+ attr_accessor :destination, :app_name, :app_class_name
8
+
9
+ def initialize(app_name, destination=nil)
10
+ @app_name = app_name
11
+ @app_class_name = app_name.split("_").map{|s| s.capitalize }.join("")
12
+ destination = destination ? "#{destination}/#{@app_name}" : @app_name
13
+
14
+ unless File.directory?(destination)
15
+ FileUtils.mkdir_p(destination)
16
+ end
17
+
18
+ @destination = File.expand_path(destination)
19
+ end
20
+
21
+ def generate_gemfile
22
+ File.open "#{@destination}/Gemfile", "w" do |f|
23
+ f.write("source 'https://rubygems.org'\n\n")
24
+ f.write("gem 'melodiest', '#{Melodiest::VERSION}'")
25
+ end
26
+ end
27
+
28
+ def generate_bundle_config
29
+ File.open "#{@destination}/config.ru", "w" do |f|
30
+ f.write("require 'rubygems'\n")
31
+ f.write("require 'bundler'\n\n")
32
+ f.write("Bundler.require\n\n")
33
+ f.write("require './#{@app_name}'\n")
34
+ f.write("run #{@app_class_name}\n")
35
+ end
36
+ end
37
+
38
+ # https://github.com/sinatra/sinatra-book/blob/master/book/Organizing_your_application.markdown
39
+ def generate_app
40
+ File.open "#{@destination}/#{@app_name}.rb", "w" do |f|
41
+ f.write("class #{app_class_name} < Melodiest::Application\n")
42
+ f.write(" configure do\n")
43
+ f.write(" # Load up database and such\n")
44
+ f.write(" end\n")
45
+ f.write("end\n\n")
46
+ f.write("# Load all route files\n")
47
+ f.write("Dir[File.dirname(__FILE__) + \"/app/routes/**\"].each do |route|\n")
48
+ f.write(" require route\n")
49
+ f.write("end\n")
50
+ end
51
+
52
+ FileUtils.mkdir_p "#{@destination}/db/migrations"
53
+
54
+ app_dir = "#{@destination}/app"
55
+ ["", "/routes", "/models", "/views"].each do |dir|
56
+ FileUtils.mkdir "#{app_dir}/#{dir}"
57
+ end
58
+ end
59
+ end
60
+
61
+ end
@@ -1,7 +1,8 @@
1
1
  module Melodiest
2
2
  module Setting
3
3
  def setup
4
- set :server, 'thin'
4
+ register Sinatra::ConfigFile
5
+ config_file File.expand_path('../config.yml', __FILE__)
5
6
  end
6
7
  end
7
8
  end
@@ -1,3 +1,3 @@
1
1
  module Melodiest
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/melodiest.rb CHANGED
@@ -1,12 +1,16 @@
1
1
  require 'sinatra'
2
+ require 'sinatra/config_file'
3
+ require 'sinatra/reloader'
2
4
  require 'melodiest/setting'
3
5
 
4
6
  module Melodiest
5
7
  class Application < Sinatra::Application
6
8
  extend Setting
9
+ setup
7
10
 
8
- configure do
9
- setup
11
+ # http://www.sinatrarb.com/contrib/reloader.html
12
+ configure :development do
13
+ register Sinatra::Reloader
10
14
  end
11
15
  end
12
16
  end
data/melodiest.gemspec CHANGED
@@ -9,6 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.author = 'Kunto Aji Kristianto'
10
10
  s.email = 'kunto.aji.kr@slackware-id.org'
11
11
  s.files = `git ls-files -z`.split("\x0")
12
+ s.executables << 'melodiest'
12
13
  s.homepage = 'http://github.com/kuntoaji/melodiest'
13
14
  s.license = 'MIT'
14
15
 
@@ -16,5 +17,6 @@ Gem::Specification.new do |s|
16
17
  s.add_development_dependency 'rspec', '3.1.0'
17
18
  s.add_development_dependency 'rack-test', '0.6.3'
18
19
  s.add_runtime_dependency 'sinatra', '1.4.5'
20
+ s.add_runtime_dependency 'sinatra-contrib', '1.4.2'
19
21
  s.add_runtime_dependency 'thin', '1.6.3'
20
22
  end
@@ -1,5 +1,3 @@
1
- require_relative '../../lib/melodiest/auth'
2
-
3
1
  describe Melodiest::Auth::Http do
4
2
  it "respond to authorized!" do
5
3
  expect(Melodiest::Auth::Http.method_defined?(:authorized!)).to be_truthy
@@ -0,0 +1,42 @@
1
+ require_relative '../../lib/melodiest/command'
2
+ require 'fakefs/spec_helpers'
3
+
4
+ describe Melodiest::Command do
5
+ include FakeFS::SpecHelpers
6
+
7
+ describe "parse" do
8
+ it "has --help option" do
9
+ help = Melodiest::Command.parse %w(--help)
10
+
11
+ expect(help).to include "Usage: melodiest [options]"
12
+ expect(help).to include "Print this help"
13
+ end
14
+
15
+ it "has --version option" do
16
+ version = Melodiest::Command.parse %w(--version)
17
+
18
+ expect(version).to include Melodiest::VERSION
19
+ end
20
+
21
+ it "has --name option as required option to generate application" do
22
+ app = Melodiest::Command.parse %w(--name my_app)
23
+
24
+ expect(app).to include "my_app is successfully generated"
25
+ end
26
+
27
+ it "has --directory option as target directory" do
28
+ app = Melodiest::Command.parse %w(-n my_app --dir /tmp)
29
+
30
+ expect(app).to include "my_app is successfully generated in /tmp"
31
+ expect(Dir.exists?("/tmp/my_app")).to be_truthy
32
+ end
33
+
34
+ context "when has no --name option and only --dir option" do
35
+ it "does nothing" do
36
+ app = Melodiest::Command.parse %w(--dir /tmp/melodiest)
37
+
38
+ expect(app).to be_empty
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,88 @@
1
+ require_relative '../../lib/melodiest/generator'
2
+ require 'fakefs/spec_helpers'
3
+
4
+ describe Melodiest::Generator do
5
+ include FakeFS::SpecHelpers
6
+
7
+ let(:dest) { "/tmp/melodiest" }
8
+ let(:app) { "my_app" }
9
+ let(:generator) { Melodiest::Generator.new app, dest }
10
+
11
+ it "sets app_name" do
12
+ expect(generator.app_name).to eq app
13
+ end
14
+
15
+ it "sets app_class_name" do
16
+ expect(generator.app_class_name).to eq "MyApp"
17
+ end
18
+
19
+ it "has default destination path app_name" do
20
+ expect(Melodiest::Generator.new(app).destination).to eq File.expand_path(app)
21
+ end
22
+
23
+ it "sets new destination path even if it's not exist yet" do
24
+ expect("/tmp/melodiest/my_app").to eq "#{dest}/#{app}"
25
+ end
26
+
27
+ describe "#generate_gemfile" do
28
+ let(:gemfile) { "#{dest}/#{app}/Gemfile" }
29
+
30
+ it "should generate Gemfile with correct content" do
31
+ generator.generate_gemfile
32
+ file_content = File.read(gemfile)
33
+
34
+ expect(File.exists?(gemfile)).to be_truthy
35
+ expect(file_content).to include "source 'https://rubygems.org'"
36
+ expect(file_content).to include "gem 'melodiest', '#{Melodiest::VERSION}'"
37
+ end
38
+ end
39
+
40
+ describe "#generate_bundle_config" do
41
+ let(:bundle_config) { "#{dest}/#{app}/config.ru" }
42
+
43
+ it "should generate config.ru with correct content" do
44
+ generator.generate_bundle_config
45
+ file_content = File.read(bundle_config)
46
+
47
+ expect(File.exists?(bundle_config)).to be_truthy
48
+ expect(file_content).to include "require 'rubygems'"
49
+ expect(file_content).to include "require 'bundler'"
50
+ expect(file_content).to include "Bundler.require"
51
+ expect(file_content).to include "require './my_app'"
52
+ expect(file_content).to include "run MyApp"
53
+ end
54
+ end
55
+
56
+ describe "#generate_app" do
57
+ let(:target_dir) { "#{dest}/#{app}" }
58
+
59
+ it "generates <app_name>.rb" do
60
+ generator.generate_app
61
+ app_file = "#{target_dir}/my_app.rb"
62
+ file_content = File.read(app_file)
63
+
64
+ expected_file_content =
65
+ <<DOC
66
+ class MyApp < Melodiest::Application
67
+ configure do
68
+ # Load up database and such
69
+ end
70
+ end
71
+
72
+ # Load all route files
73
+ Dir[File.dirname(__FILE__) + "/app/routes/**"].each do |route|
74
+ require route
75
+ end
76
+ DOC
77
+
78
+ expect(File.exists?(app_file)).to be_truthy
79
+ expect(file_content).to eq expected_file_content
80
+ expect(Dir.exists?("#{target_dir}/db/migrations")).to be_truthy
81
+ expect(Dir.exists?("#{target_dir}/app")).to be_truthy
82
+ expect(Dir.exists?("#{target_dir}/app/routes")).to be_truthy
83
+ expect(Dir.exists?("#{target_dir}/app/models")).to be_truthy
84
+ expect(Dir.exists?("#{target_dir}/app/views")).to be_truthy
85
+ end
86
+ end
87
+
88
+ end
@@ -1,5 +1,3 @@
1
- require_relative '../../lib/melodiest/setting'
2
-
3
1
  describe Melodiest::Setting do
4
2
  it "respond to setup" do
5
3
  expect(Melodiest::Setting.method_defined?(:setup)).to be_truthy
@@ -1,9 +1,17 @@
1
- require_relative '../lib/melodiest'
2
-
3
1
  describe Melodiest::Application do
2
+ it "has config.yml" do
3
+ expect(File.exists?(File.expand_path('../../lib/melodiest/config.yml', __FILE__))).to be_truthy
4
+ end
5
+
4
6
  context ".settings" do
5
7
  it "use thin as server" do
6
8
  expect(Melodiest::Application.settings.server).to eq("thin")
7
9
  end
8
10
  end
11
+
12
+ context ".extensions" do
13
+ it "has reloader extension" do
14
+ expect(app.extensions.include?(Sinatra::Reloader)).to be_truthy
15
+ end
16
+ end
9
17
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  ENV['RACK_ENV'] = 'test'
2
2
  require 'rack/test'
3
+ require_relative '../lib/melodiest'
4
+ require_relative '../lib/melodiest/auth/http'
3
5
 
4
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
5
7
  RSpec.configure do |config|
@@ -87,3 +89,7 @@ def app
87
89
  Melodiest::Application
88
90
  end
89
91
 
92
+ def gem_root_path
93
+ File.expand_path "../../", __FILE__
94
+ end
95
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: melodiest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kunto Aji Kristianto
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.4.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra-contrib
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.4.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.4.2
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: thin
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -82,7 +96,8 @@ dependencies:
82
96
  version: 1.6.3
83
97
  description: Sinatra configuration boilerplate
84
98
  email: kunto.aji.kr@slackware-id.org
85
- executables: []
99
+ executables:
100
+ - melodiest
86
101
  extensions: []
87
102
  extra_rdoc_files: []
88
103
  files:
@@ -92,12 +107,18 @@ files:
92
107
  - Gemfile.lock
93
108
  - LICENSE.txt
94
109
  - README.md
110
+ - bin/melodiest
95
111
  - lib/melodiest.rb
96
- - lib/melodiest/auth.rb
112
+ - lib/melodiest/auth/http.rb
113
+ - lib/melodiest/command.rb
114
+ - lib/melodiest/config.yml
115
+ - lib/melodiest/generator.rb
97
116
  - lib/melodiest/setting.rb
98
117
  - lib/melodiest/version.rb
99
118
  - melodiest.gemspec
100
- - spec/melodiest/auth_spec.rb
119
+ - spec/melodiest/auth/http_spec.rb
120
+ - spec/melodiest/command_spec.rb
121
+ - spec/melodiest/generator_spec.rb
101
122
  - spec/melodiest/setting_spec.rb
102
123
  - spec/melodiest_spec.rb
103
124
  - spec/spec_helper.rb
@@ -121,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
142
  version: '0'
122
143
  requirements: []
123
144
  rubyforge_project:
124
- rubygems_version: 2.2.2
145
+ rubygems_version: 2.4.5
125
146
  signing_key:
126
147
  specification_version: 4
127
148
  summary: Sinatra configuration boilerplate
File without changes