hexlet_code 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7fb76f5b6de1af01d33280b02e6c4c6c0f0371f04d1016a56ac55f4ceb5cd4e7
4
+ data.tar.gz: 8f493647d4fb2e2166132add6e80e031ee72e1d909a1964ee3beaeb07d2531b7
5
+ SHA512:
6
+ metadata.gz: e516618e8aa0d06d89d3e04f20296ef1d02a9d19fb5573c827066e8550239fa72e2f444288797b0ebc57b0623056ad7cf8276319506e8a53b70886d054c0c51d
7
+ data.tar.gz: 7d066e67250edf313ce253e82ddeb7059b3f69bace69c608c60204391b2f5953d42f8a10f75cf4a67d3253c1eb3463c58d112d9971751ef65d9a65fcd839fac7
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 3.2.2
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: single_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: single_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Style/Documentation:
17
+ Enabled: false
@@ -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 xvanx84@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) 2024 ivanz
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/Makefile ADDED
@@ -0,0 +1,10 @@
1
+ install: # установить зависимости
2
+ bundle install
3
+
4
+ lint: # запустить линтер
5
+ bundle exec rubocop
6
+
7
+ test: # запустить тесты
8
+ bundle exec rake test
9
+
10
+ .PHONY: test
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # HexletCode
2
+
3
+ [![CI status](https://github.com/EvanBrightside/rails-project-63/actions/workflows/main.yml/badge.svg)](https://github.com/EvanBrightside/rails-project-63/actions/workflows/main.yml)
4
+ [![Hexlet Check](https://github.com/EvanBrightside/rails-project-63/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/EvanBrightside/rails-project-63/actions/workflows/hexlet-check.yml)
5
+
6
+ Form Generator – DSL that makes it easy to generate forms.
7
+ This gem takes care of tasks that would normally require writing a lot of boilerplate code.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add hexlet_code --git 'https://github.com/EvanBrightside/rails-project-63.git'
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install specific_install
18
+ $ gem specific_install -l 'https://github.com/EvanBrightside/rails-project-63.git'
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ require 'hexlet_code'
24
+
25
+ User = Struct.new(:name, :job, :gender, keyword_init: true)
26
+ user = User.new name: 'Mitch', job: 'Github', gender: 'm'
27
+
28
+ HexletCode.form_for user, url: '#' do |f|
29
+ f.input :job, as: :text, rows: 50, cols: 50
30
+ end
31
+
32
+ # <form action="#" method="post">
33
+ # <label for="job">Job</label>
34
+ # <textarea name="job" cols="50" rows="50">Github</textarea>
35
+ # </form>
36
+
37
+ user = User.new job: 'Github'
38
+ HexletCode.form_for user, url: '#' do |f|
39
+ f.input :name
40
+ f.input :job
41
+ f.submit 'Wow'
42
+ end
43
+
44
+ # <form action="#" method="post">
45
+ # <label for="name">Name</label>
46
+ # <input name="name" type="text" value="">
47
+ # <label for="job">Job</label>
48
+ # <input name="job" type="text" value="Github">
49
+ # <input type="submit" value="Wow">
50
+ # </form>
51
+ ```
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/EvanBrightside/rails-project-63. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/EvanBrightside/rails-project-63/blob/main/CODE_OF_CONDUCT.md).
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
66
+
67
+ ## Code of Conduct
68
+
69
+ Everyone interacting in the HexletCode project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/EvanBrightside/rails-project-63/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'minitest/test_task'
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'form_tags/string_tag'
4
+ require_relative 'form_tags/text_tag'
5
+ require_relative 'form_tags/label_tag'
6
+ require_relative 'form_tags/submit_tag'
7
+
8
+ module HexletCode
9
+ autoload(:Tag, 'hexlet_code/tag.rb')
10
+
11
+ class FormBuilder
12
+ def initialize(data)
13
+ @data = data
14
+ @fields = []
15
+
16
+ yield self if block_given?
17
+ end
18
+
19
+ def label(name)
20
+ FormTags::LabelTag.new(name:)
21
+ end
22
+
23
+ def input(name, **options)
24
+ input_type = options[:as] || :string
25
+ check_errors(name)
26
+ clean_attributes = { name:, value: @data[name] }.merge(options.except(:as))
27
+ class_name = "HexletCode::FormTags::#{input_type.capitalize}Tag"
28
+ tag = Object.const_get(class_name).new(clean_attributes)
29
+
30
+ @fields.push(label(name), tag)
31
+ tag
32
+ end
33
+
34
+ def submit(value = 'Save')
35
+ tag = FormTags::SubmitTag.new(value:)
36
+ @fields << tag
37
+ tag
38
+ end
39
+
40
+ def build
41
+ @fields
42
+ end
43
+
44
+ private
45
+
46
+ def check_errors(name)
47
+ @data.public_send(name)
48
+ rescue NoMethodError => e
49
+ puts "\n No method error: #{e}"
50
+ raise e
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HexletCode
4
+ class FormPresenter
5
+ def initialize(form, options = {})
6
+ @form = form
7
+ @url = options[:url] || '#'
8
+ @method = options[:method] || 'post'
9
+ @class = options[:class] || ''
10
+ end
11
+
12
+ def render_html
13
+ result = "<form action=\"#{@url}\" method=\"#{@method}\""
14
+ result << " class=\"#{@class}\"" unless @class.nil? || @class.empty?
15
+ result << '>'
16
+ result << content unless @form.nil? || @form.empty?
17
+ result << "\n</form>"
18
+ end
19
+
20
+ private
21
+
22
+ def content
23
+ result = ''
24
+ result += "\n"
25
+ result += @form.map do |node|
26
+ tag_params = [node.tag, node.attributes]
27
+ tag_params << node.block unless node.block.nil?
28
+ " #{Tag.build(*tag_params)}"
29
+ end.join("\n")
30
+ result
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HexletCode
4
+ module FormTags
5
+ class BaseTag
6
+ attr_reader :tag, :block
7
+ attr_accessor :value, :attributes
8
+
9
+ TAG = ''
10
+ DEFAULT_ATTRIBUTES = {}.freeze
11
+
12
+ def initialize(attributes = {})
13
+ @tag = self.class::TAG
14
+ @value = attributes[:value]
15
+ @attributes = {}.merge(self.class::DEFAULT_ATTRIBUTES, attributes)
16
+ end
17
+
18
+ # def build
19
+ # Tag.build(@tag, @attributes)
20
+ # end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'tag_with_block'
4
+
5
+ module HexletCode
6
+ module FormTags
7
+ class LabelTag < TagWithBlock
8
+ TAG = 'label'
9
+
10
+ def initialize(attributes = {})
11
+ super
12
+ name = attributes[:name]
13
+ @attributes = {}.merge(self.class::DEFAULT_ATTRIBUTES, { for: name })
14
+ @block = -> { name.capitalize }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_tag'
4
+
5
+ module HexletCode
6
+ module FormTags
7
+ class StringTag < BaseTag
8
+ TAG = 'input'
9
+ DEFAULT_ATTRIBUTES = { name:, type: 'text' }.freeze
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'tag_with_block'
4
+
5
+ module HexletCode
6
+ module FormTags
7
+ class SubmitTag < BaseTag
8
+ TAG = 'input'
9
+ DEFAULT_ATTRIBUTES = { type: 'submit', value: @value }.freeze
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HexletCode
4
+ module FormTags
5
+ class TagWithBlock < BaseTag
6
+ def initialize(attributes = {})
7
+ super
8
+ @attributes = {}.merge(self.class::DEFAULT_ATTRIBUTES, attributes.except(:value))
9
+ @block = -> { @value }
10
+ end
11
+
12
+ # def build
13
+ # Tag.build(@tag, @attributes) { @block.call }
14
+ # end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'tag_with_block'
4
+
5
+ module HexletCode
6
+ module FormTags
7
+ class TextTag < TagWithBlock
8
+ TAG = 'textarea'
9
+ DEFAULT_ATTRIBUTES = { name:, cols: 20, rows: 40 }.freeze
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HexletCode
4
+ class Tag
5
+ SINGLE_TAGS = %w[area base br col command embed hr img input keygen link meta param source track wbr].freeze
6
+
7
+ def self.build(tag_name, attributes = {}, block = nil)
8
+ result = "<#{tag_name}"
9
+ attributes.each { |attr_name, attr_value| result << " #{attr_name}=\"#{attr_value}\"" } if attributes.any?
10
+ result << '>'
11
+ body_tag(tag_name, yield, result) if block_given?
12
+ content_tag(tag_name, block, result) unless SINGLE_TAGS.include?(tag_name)
13
+ result
14
+ end
15
+
16
+ class << self
17
+ private
18
+
19
+ def content_tag(tag_name, block, result)
20
+ content = if block_given?
21
+ yield
22
+ elsif !block.nil? && block.instance_of?(Proc)
23
+ block.call
24
+ end
25
+ result << "#{content}</#{tag_name}>"
26
+ end
27
+
28
+ def body_tag(tag_name, body, result)
29
+ case body
30
+ when String
31
+ result << body.to_s
32
+ when Array
33
+ body.each { |field| result << "\n #{field}" }
34
+ when Symbol
35
+ result_tag = tag_name == 'label' ? body.to_s : "\n #{body}"
36
+ result << result_tag
37
+ end
38
+ result
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HexletCode
4
+ VERSION = '0.2.0'
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'hexlet_code/version'
4
+
5
+ module HexletCode
6
+ autoload(:FormBuilder, 'hexlet_code/form_builder.rb')
7
+ autoload(:FormPresenter, 'hexlet_code/form_presenter.rb')
8
+
9
+ class Error < StandardError; end
10
+
11
+ def self.form_for(data, options = {}, &)
12
+ raw_form = FormBuilder.new(data, &).build
13
+ FormPresenter.new(raw_form, options).render_html
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ module HexletCode
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hexlet_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Zabrodin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem takes care of tasks that would normally require writing a lot
14
+ of boilerplate code.
15
+ email:
16
+ - xvanx84@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rubocop.yml"
22
+ - CODE_OF_CONDUCT.md
23
+ - LICENSE.txt
24
+ - Makefile
25
+ - README.md
26
+ - Rakefile
27
+ - lib/hexlet_code.rb
28
+ - lib/hexlet_code/form_builder.rb
29
+ - lib/hexlet_code/form_presenter.rb
30
+ - lib/hexlet_code/form_tags/base_tag.rb
31
+ - lib/hexlet_code/form_tags/label_tag.rb
32
+ - lib/hexlet_code/form_tags/string_tag.rb
33
+ - lib/hexlet_code/form_tags/submit_tag.rb
34
+ - lib/hexlet_code/form_tags/tag_with_block.rb
35
+ - lib/hexlet_code/form_tags/text_tag.rb
36
+ - lib/hexlet_code/tag.rb
37
+ - lib/hexlet_code/version.rb
38
+ - sig/hexlet_code.rbs
39
+ homepage: https://github.com/EvanBrightside/rails-project-63
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/EvanBrightside/rails-project-63
44
+ rubygems_mfa_required: 'true'
45
+ source_code_uri: https://github.com/EvanBrightside/rails-project-63
46
+ changelog_uri: https://github.com/EvanBrightside/rails-project-63
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 3.2.2
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.5.6
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Form Generator – DSL that makes it easy to generate forms.
66
+ test_files: []