js_generator 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6ccd932f58631b5d8283c9efd1dcfcba05f98be590d217e2fff97400efa03c89
4
+ data.tar.gz: d5321b751356cd4b1ae09039ec1e262f6353845622cb5bab80a8f7ca85b5e7b2
5
+ SHA512:
6
+ metadata.gz: eb460c52a67baeecbb05256a19e0d5cd91414d9754a84676887407d4abc927e08d9c0235f12736fcfb835ab1d69a67d4111bc5fb0a075bc8c9d87bb858538f6e
7
+ data.tar.gz: 534eff68babe22e0df2831e70e7c02f9b6e9aa85484a8b29cb76b2daf4c2e119dc62c619c4a6cedb75e6381f1fe1d315435b7946aea8f1d6dc0813d2a57fca44
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-11-26
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at kenzou.kenzou104809@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 kenzo-tanaka
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # JsGenerator
2
+
3
+ This gem generates some JavaScript code with the specific rule.
4
+
5
+ # 📌 Dependency
6
+
7
+ - [dotenv](https://github.com/bkeepers/dotenv)
8
+
9
+ # 📚 How to work
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'js_generator'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Add this lines to `.env`:
22
+
23
+ ```text
24
+ # set your js top namespace
25
+ TOP_LEVEL_JS_NAMESPACE=Example
26
+ ```
27
+
28
+ Run the command like this:
29
+
30
+ ```shell
31
+ $ bundle exec js_generate admin/blogs/new
32
+ ```
33
+
34
+ This command generates these lines:
35
+
36
+ - Create the js file for the view like `app/javascript/packs/views/admin/blogs/new.js`.
37
+ - Write some codes below:
38
+ ```js
39
+ // application.js
40
+ window.Example.Admin = window.Example.Admin || {};
41
+ window.Example.Admin.Blogs = window.Example.Admin.Blogs || {};
42
+ import AdminBlogsNew from './views/admin/blogs/new';
43
+ window.Example.Admin.Blogs.New = window.Example.Admin.Blogs.New || {};
44
+ window.Example.Admin.Blogs.New = AdminBlogsNew;
45
+ ```
46
+ - Write `script` tag to the view file(e.g. `app/views/admin/blogs/new.html.erb`).
47
+ ```html
48
+ <script>
49
+ window.Example.Admin.Blogs.New();
50
+ </script>
51
+ ```
52
+
53
+ # 🔍 `js_generate` command
54
+
55
+ Here is the `js_generate` syntax. The `namespace` is optional.
56
+
57
+ ```shell
58
+ $ bundle exec js_generate [namespace(optional)]/[model_name]/[action_name]
59
+
60
+ # e.g. with namespace
61
+ $ bundle exec js_generate admin/blogs/new
62
+
63
+ # e.g.g without namespace
64
+ $ bundle exec js_generate blogs/index
65
+ ```
66
+
67
+ # 🖥 Development
68
+
69
+ ```shell
70
+ $ bin/setup
71
+ ```
72
+
73
+ ```shell
74
+ $ bundle exec rspec
75
+ ```
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "js_generator"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/js_generate ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/js_generator'
3
+ if ARGV.length != 1
4
+ raise ArgumentError.new("wrong number of arguments (given #{ARGV.length}, expected 1)")
5
+ end
6
+
7
+ args = ARGV[0].split('/')
8
+
9
+ if args.length == 2
10
+ setup_js = JsGenerator::SetupJs.new(model_name: args[0], action_name: args[1])
11
+ setup_js.run
12
+ else
13
+ setup_js = JsGenerator::SetupJs.new(namespace: args[0], model_name: args[1], action_name: args[2])
14
+ setup_js.run
15
+ end
16
+
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/js_generator/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "js_generator"
7
+ spec.version = JsGenerator::VERSION
8
+ spec.authors = ["kenzo-tanaka"]
9
+ spec.email = ["kenzou.kenzou104809@gmail.com"]
10
+
11
+ spec.summary = "Generate JavaScript codes with the specific rule."
12
+ spec.description = "Auto generate JavaScript codes with the specific rule."
13
+ spec.homepage = "https://github.com/kenzo-tanaka/js_generator"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/kenzo-tanaka/js_generator"
18
+ spec.metadata["changelog_uri"] = "https://github.com/kenzo-tanaka/js_generator/blob/main/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = `git ls-files -z LICENSE.txt *.md *.gemspec bin lib`.split("\x0")
23
+ spec.bindir = "bin"
24
+ spec.executables = spec.files.grep(%r{\Abin/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ # Uncomment to register a new dependency of your gem
28
+ spec.add_dependency "dotenv", "~> 2.7.6"
29
+
30
+ # For more information and examples about making a new gem, checkout our
31
+ # guide at: https://bundler.io/guides/creating_gem.html
32
+ end
@@ -0,0 +1,37 @@
1
+ require 'dotenv'
2
+ Dotenv.load
3
+ require_relative './syntax_builder/namespaced'
4
+ require_relative './syntax_builder/without_namespace'
5
+
6
+ module JsGenerator
7
+ class AppJs
8
+ attr_reader :namespace, :model_name, :action_name, :top_level_js_namespace
9
+
10
+ def initialize(setup_js)
11
+ @namespace = setup_js.namespace
12
+ @model_name = setup_js.model_name
13
+ @action_name = setup_js.action_name
14
+ @top_level_js_namespace = setup_js.top_level_js_namespace
15
+ end
16
+
17
+ def append_script
18
+ File.open("app/javascript/packs/application.js", 'a') do |f|
19
+ f << syntax_builder.script_for_append
20
+ end
21
+ end
22
+
23
+ def action_namespace
24
+ syntax_builder.action_namespace
25
+ end
26
+
27
+ private
28
+
29
+ def syntax_builder
30
+ if namespace.present?
31
+ SyntaxBuilder::Namespaced.new(self)
32
+ else
33
+ SyntaxBuilder::WithoutNamespaced.new(self)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,63 @@
1
+ module JsGenerator
2
+ class JsForView
3
+ attr_reader :namespace, :model_name, :action_name
4
+
5
+ def initialize(setup_js)
6
+ @namespace = setup_js.namespace
7
+ @model_name = setup_js.model_name
8
+ @action_name = setup_js.action_name
9
+ end
10
+
11
+ def create_file
12
+ make_view_dir
13
+ make_namespace_dir
14
+ make_model_dir
15
+ touch_file
16
+ end
17
+
18
+ private
19
+
20
+ def make_view_dir
21
+ Dir.mkdir(view_path) unless Dir.exist?(view_path)
22
+ end
23
+
24
+ def make_namespace_dir
25
+ return if Dir.exist?(namespace_path)
26
+ return if namespace.nil?
27
+
28
+ Dir.mkdir(namespace_path)
29
+ end
30
+
31
+ def make_model_dir
32
+ Dir.mkdir(model_path) unless Dir.exist?(model_path)
33
+ end
34
+
35
+ def touch_file
36
+ return if File.exist?(action_path)
37
+
38
+ File.open(action_path, 'w') { |f| f.write export_statement }
39
+ end
40
+
41
+ def view_path
42
+ "app/javascript/packs/views"
43
+ end
44
+
45
+ def namespace_path
46
+ "#{view_path}/#{namespace}"
47
+ end
48
+
49
+ def model_path
50
+ "#{namespace_path}/#{model_name.pluralize}"
51
+ end
52
+
53
+ def action_path
54
+ "#{model_path}/#{action_name}.js"
55
+ end
56
+
57
+ def export_statement
58
+ <<~TEXT
59
+ export default() => {}
60
+ TEXT
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,39 @@
1
+ require_relative './syntax_builder_base'
2
+
3
+ module JsGenerator
4
+ module SyntaxBuilder
5
+ class Namespaced < SyntaxBuilderBase
6
+ def action_namespace
7
+ "window.#{top_level_js_namespace}.#{namespace.capitalize}.#{model_name.capitalize.pluralize}.#{action_name.capitalize}"
8
+ end
9
+
10
+ def script_for_append
11
+ <<~TEXT
12
+ #{define_namespace(custom_namespace)}
13
+ #{define_namespace(model_namespace)}
14
+ import #{import_name} from '#{import_path}';
15
+ #{action_namespace} = #{action_namespace} || {};
16
+ #{action_namespace} = #{import_name};
17
+ TEXT
18
+ end
19
+
20
+ private
21
+
22
+ def custom_namespace
23
+ "window.#{top_level_js_namespace}.#{namespace.capitalize}"
24
+ end
25
+
26
+ def model_namespace
27
+ "window.#{top_level_js_namespace}.#{namespace.capitalize}.#{model_name.capitalize.pluralize}"
28
+ end
29
+
30
+ def import_path
31
+ "./views/#{namespace}/#{model_name.pluralize}/#{action_name}"
32
+ end
33
+
34
+ def import_name
35
+ "#{namespace.capitalize}#{model_name.capitalize.pluralize}#{action_name.capitalize}"
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ module JsGenerator
2
+ module SyntaxBuilder
3
+ class SyntaxBuilderBase
4
+ attr_reader :namespace, :model_name, :action_name, :top_level_js_namespace
5
+
6
+ def initialize(app_js)
7
+ @namespace = app_js.namespace
8
+ @model_name = app_js.model_name
9
+ @action_name = app_js.action_name
10
+ @top_level_js_namespace = app_js.top_level_js_namespace
11
+ end
12
+
13
+ private
14
+
15
+ def define_namespace(text)
16
+ File.read("app/javascript/packs/application.js").include?(text) ? nil : "#{text} = #{text} || {};"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ require_relative './syntax_builder_base'
2
+
3
+ module JsGenerator
4
+ module SyntaxBuilder
5
+ class WithoutNamespaced < SyntaxBuilderBase
6
+ def action_namespace
7
+ "window.#{top_level_js_namespace}.#{model_name.capitalize.pluralize}.#{action_name.capitalize}"
8
+ end
9
+
10
+ def script_for_append
11
+ <<~TEXT
12
+ #{define_namespace(model_namespace)}
13
+ import #{import_name} from '#{import_path}';
14
+ #{action_namespace} = #{action_namespace} || {};
15
+ #{action_namespace} = #{import_name};
16
+ TEXT
17
+ end
18
+
19
+ private
20
+
21
+ def model_namespace
22
+ "window.#{top_level_js_namespace}.#{model_name.capitalize.pluralize}"
23
+ end
24
+
25
+ def import_path
26
+ "./views/#{model_name.pluralize}/#{action_name}"
27
+ end
28
+
29
+ def import_name
30
+ "#{model_name.capitalize.pluralize}#{action_name.capitalize}"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsGenerator
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,30 @@
1
+ module JsGenerator
2
+ class ViewFile
3
+ attr_reader :namespace, :model_name, :action_name, :setup_js
4
+
5
+ def initialize(setup_js)
6
+ @namespace = setup_js.namespace
7
+ @model_name = setup_js.model_name
8
+ @action_name = setup_js.action_name
9
+ @setup_js = setup_js
10
+ end
11
+
12
+ def append_script
13
+ File.open(file_path, 'a') { |f| f << script_tag }
14
+ end
15
+
16
+ private
17
+
18
+ def file_path
19
+ "app/views/#{namespace}/#{model_name.pluralize}/#{action_name}.html.erb"
20
+ end
21
+
22
+ def script_tag
23
+ <<~TEXT
24
+ <script>
25
+ #{AppJs.new(setup_js).action_namespace}();
26
+ </script>
27
+ TEXT
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "js_generator/version"
4
+ require_relative 'js_generator/js_for_view'
5
+ require_relative 'js_generator/app_js'
6
+ require_relative 'js_generator/view_file'
7
+ require 'active_support/inflector'
8
+
9
+ module JsGenerator
10
+ class Error < StandardError; end
11
+ class SetupJs
12
+ attr_reader :namespace, :model_name, :action_name
13
+ attr_accessor :top_level_js_namespace
14
+
15
+ def initialize(namespace: nil, model_name:, action_name:)
16
+ @namespace = namespace
17
+ @model_name = model_name
18
+ @action_name = action_name
19
+ @top_level_js_namespace = ENV['TOP_LEVEL_JS_NAMESPACE']
20
+ end
21
+
22
+ def run
23
+ if top_level_js_namespace.nil?
24
+ raise Error.new('Please set TOP_LEVEL_JS_NAMESPACE in .env')
25
+ end
26
+
27
+ JsForView.new(self).create_file
28
+ AppJs.new(self).append_script
29
+ ViewFile.new(self).append_script
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - kenzo-tanaka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.6
27
+ description: Auto generate JavaScript codes with the specific rule.
28
+ email:
29
+ - kenzou.kenzou104809@gmail.com
30
+ executables:
31
+ - console
32
+ - js_generate
33
+ - setup
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - CHANGELOG.md
38
+ - CODE_OF_CONDUCT.md
39
+ - LICENSE.txt
40
+ - README.md
41
+ - bin/console
42
+ - bin/js_generate
43
+ - bin/setup
44
+ - js_generator.gemspec
45
+ - lib/js_generator.rb
46
+ - lib/js_generator/app_js.rb
47
+ - lib/js_generator/js_for_view.rb
48
+ - lib/js_generator/syntax_builder/namespaced.rb
49
+ - lib/js_generator/syntax_builder/syntax_builder_base.rb
50
+ - lib/js_generator/syntax_builder/without_namespace.rb
51
+ - lib/js_generator/version.rb
52
+ - lib/js_generator/view_file.rb
53
+ homepage: https://github.com/kenzo-tanaka/js_generator
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ homepage_uri: https://github.com/kenzo-tanaka/js_generator
58
+ source_code_uri: https://github.com/kenzo-tanaka/js_generator
59
+ changelog_uri: https://github.com/kenzo-tanaka/js_generator/blob/main/CHANGELOG.md
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.4.0
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.2.22
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Generate JavaScript codes with the specific rule.
79
+ test_files: []