element_component 0.2.0 → 0.5.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
  SHA256:
3
- metadata.gz: d0f12674a760bdf815e6ae25fccbadb96032dacc122eca06c9ad7335b91fe226
4
- data.tar.gz: d912a66d464b5f0ead36c41faa7e20d12b5bbd56871f3112b922e11a9bc83aa9
3
+ metadata.gz: 9ebf642383684b4ac3419d63b6538ab4ddccc14c6275ad9913e2aa4d6fd14938
4
+ data.tar.gz: 1bfd4f8d575c2f92804c7c62868e3c303ca8a01f6238addee9d4bf93cd3b6d68
5
5
  SHA512:
6
- metadata.gz: 25affca2d6a9f235d515060f943ebff6901fc2590f89a0b9782f4c43ce9f3d143cbcb8a2be976db48478328fbcdc903ba401bbeaa98901564f8f8f5f05f249a5
7
- data.tar.gz: 19e19dccf1862858c47520a4afa3f0bf8aa364461dda3f30243c1d0400a49cdbb8a283c7aba3975a6cbb439d2f8aa7cf8cea5691663183cdc23c4237afbc5dc4
6
+ metadata.gz: 99e132b0e8a5e168dfb9bae745cb46c235a3249ef42e61263bbbd4ae384472523ac5d110a3dc3afe12fec1a0436defd795afdd227f36efa11f515d4eb64a52f1
7
+ data.tar.gz: 5d721e3aa5aefc886c74eea0a04c94e9d408417b6e5660ef6976e5f8ea603e13f1c2c4ef9b559e256532e195e289f14ebe9cf60820cd33efbc492da731133296
data/.rspec CHANGED
@@ -1,3 +1,3 @@
1
1
  --format documentation
2
- --require spec_helper
3
2
  --color
3
+ --require spec_helper
data/.rubocop.yml CHANGED
@@ -1,20 +1,22 @@
1
- # Mude para a versão de Ruby a ser utilizada e inclua aqui outros arquivos que
2
- # você considera importante que o RuboCop não execute como: node_modules, etc..
3
1
  AllCops:
4
- TargetRubyVersion: 3.1.2
5
- Exclude:
6
- - 'bin/**/*'
2
+ TargetRubyVersion: 3.1
3
+ SuggestExtensions: false
4
+ NewCops: enable
7
5
 
8
6
  Documentation:
9
7
  Enabled: false
10
8
 
11
- Style/FrozenStringLiteralComment:
12
- Enabled: false
13
-
14
9
  Metrics/MethodLength:
15
10
  Enabled: false
16
- Metrics/AbcSize:
17
- Enabled: false
18
11
 
19
12
  Metrics/BlockLength:
20
- ExcludedMethods: ['describe', 'context', 'feature', 'scenario', 'let']
13
+ Enabled: false
14
+
15
+ Style/MissingRespondToMissing:
16
+ Enabled: false
17
+
18
+ Style/StringLiterals:
19
+ EnforcedStyle: double_quotes
20
+
21
+ Style/StringLiteralsInInterpolation:
22
+ EnforcedStyle: double_quotes
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021 TODO: Write your name
3
+ Copyright (c) 2023 João Paulo Correia
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,36 +1,101 @@
1
1
  # ElementComponent
2
2
 
3
- Build html
3
+ A lightweight and flexible HTML builder for Ruby. `ElementComponent` provides a simple, object-oriented way to construct HTML structures programmatically, allowing for dynamic attribute management and content nesting.
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```bash
10
+ bundle add element_component
11
+ ```
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ ```bash
16
+ gem install element_component
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Basic Usage
22
+
23
+ Create a simple element and render it to HTML:
8
24
 
9
25
  ```ruby
10
- gem 'element_component'
26
+ p = ElementComponent::Element.new("p", class: "text-bold")
27
+ p.add_content("Hello, World!")
28
+ puts p.render
29
+ # => <p class="text-bold">Hello, World!</p>
11
30
  ```
12
31
 
13
- And then execute:
32
+ ### Nesting Elements
14
33
 
15
- $ bundle install
34
+ You can nest elements by adding another `ElementComponent::Element` instance as content:
16
35
 
17
- Or install it yourself as:
36
+ ```ruby
37
+ div = ElementComponent::Element.new("div", class: "container")
38
+ h1 = ElementComponent::Element.new("h1")
39
+ h1.add_content("Welcome")
18
40
 
19
- $ gem install element_component
41
+ div.add_content(h1)
42
+ div.add_content("This is a simple HTML builder.")
20
43
 
21
- ## Usage
44
+ puts div.render
45
+ # => <div class="container"><h1>Welcome</h1>This is a simple HTML builder.</div>
46
+ ```
47
+
48
+ ### Attribute Management
49
+
50
+ `ElementComponent` allows for easy manipulation of HTML attributes:
51
+
52
+ ```ruby
53
+ btn = ElementComponent::Element.new("button", class: "btn", type: "button")
22
54
 
55
+ # Add more values to an attribute (e.g., adding another class)
56
+ btn.add_attribute(class: "btn-primary")
57
+
58
+ # Reset attributes and set new ones
59
+ btn.add_attribute!(id: "submit-btn", type: "submit")
60
+
61
+ # Remove an attribute
62
+ btn.remove_attribute(:type)
63
+
64
+ # Remove a specific value from an attribute
65
+ btn.remove_attribute_value(:class, "btn-primary")
23
66
  ```
24
- maker = El::Maker.new
25
- form = maker.form(attribute: { class: 'has-background-color', method: 'GET', action: '/', turbo: false }) do |form|
26
- input = maker.input(attribute: { type: 'text', name: 'email', value: nil })
27
- form.add_content input
28
67
 
29
- button = maker.button(content: 'Save', attribute: { type: 'submit'})
30
- div = maker.div(content: button, attribute: { class: 'buttons' })
68
+ ### Self-Closing Tags
31
69
 
32
- form.add_content div
33
- end
70
+ You can specify if an element should have a closing tag:
34
71
 
35
- puts form.build
72
+ ```ruby
73
+ img = ElementComponent::Element.new("img", closing_tag: false, src: "image.png", alt: "Logo")
74
+ puts img.render
75
+ # => <img src="image.png" alt="Logo">
36
76
  ```
77
+
78
+ ### Rendering Hooks
79
+
80
+ `ElementComponent` supports `before_render`, `after_render`, and `around_render` hooks if implemented in a subclass or by extending an instance.
81
+
82
+ ## Development
83
+
84
+ 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.
85
+
86
+ 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).
87
+
88
+ ## Roadmap
89
+
90
+ - [ ] Support for Caching
91
+ - [ ] Pre-built Bulma components
92
+ - [ ] Pre-built Bootstrap components
93
+ - [ ] Enhanced DSL for nested structures
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joaopaulocorreia/element_component.
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,8 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
5
 
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
8
- task default: :spec
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,108 @@
1
+ module ElementComponent
2
+ class Element
3
+ attr_reader :element, :attributes, :contents, :html
4
+
5
+ def initialize(element, closing_tag: true, **attribute)
6
+ @element = element
7
+ @closing_tag = closing_tag
8
+ @html = ""
9
+
10
+ add_attribute!(attribute)
11
+ reset_contents!
12
+ end
13
+
14
+ def add_content!(content)
15
+ reset_contents!
16
+
17
+ add_content(content)
18
+ end
19
+
20
+ def add_content(content = nil, &block)
21
+ if block_given?
22
+ @contents.push(block)
23
+ else
24
+ @contents.push(content)
25
+ end
26
+
27
+ self
28
+ end
29
+
30
+ def add_attribute!(hash_attr)
31
+ reset_attributes!
32
+
33
+ add_attribute(hash_attr)
34
+ end
35
+
36
+ def add_attribute(hash_attr)
37
+ hash_attr.each_key do |attr|
38
+ @attributes[attr] = [] unless @attributes.key?(attr)
39
+
40
+ hash_attr[attr]
41
+ .to_s
42
+ .split
43
+ .each do |value|
44
+ @attributes[attr].push(value)
45
+ end
46
+ end
47
+
48
+ self
49
+ end
50
+
51
+ def remove_attribute(attribute)
52
+ @attributes = @attributes.except(attribute)
53
+ end
54
+
55
+ def remove_attribute_value(attribute, value)
56
+ attributes[attribute].delete(value)
57
+ end
58
+
59
+ def reset_contents!
60
+ @contents = []
61
+ end
62
+
63
+ def reset_attributes!
64
+ @attributes = {}
65
+ end
66
+
67
+ def new_element(*args, **kargs) = Element.new(*args, **kargs)
68
+
69
+ def render
70
+ before_render if respond_to? 'before_render'
71
+
72
+ if respond_to? 'around_render'
73
+ around_render { build }
74
+ else
75
+ build
76
+ end
77
+
78
+ after_render(@html) if respond_to? 'after_render'
79
+
80
+ @html
81
+ end
82
+
83
+ private
84
+
85
+ def build
86
+ @html << "<#{@element}"
87
+ @html << (mount_attributes.empty? ? ">" : " #{mount_attributes}>")
88
+ @html << mount_content
89
+ @html << "</#{@element}>" if @closing_tag
90
+ @html
91
+ end
92
+
93
+ def mount_attributes
94
+ @attributes.map { |attr| "#{attr[0].to_sym}=\"#{attr[1].join(" ")}\"" }.join(" ")
95
+ end
96
+
97
+ def mount_content
98
+ @contents.map do |content|
99
+ case content
100
+ in Element
101
+ content.render
102
+ else
103
+ content.to_s
104
+ end
105
+ end.join
106
+ end
107
+ end
108
+ end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module ElementComponent
4
- VERSION = '0.2.0'
2
+ VERSION = "0.5.0"
5
3
  end
@@ -1,10 +1,7 @@
1
- # frozen_string_literal: true
1
+ require_relative "element_component/version"
2
+ require_relative "element_component/element"
2
3
 
3
- # Core elements
4
- require_relative 'element_component/version'
5
- require_relative 'element_component/core/element'
6
- require_relative 'element_component/core/maker'
7
-
8
- module El
4
+ module ElementComponent
9
5
  class Error < StandardError; end
6
+ # Your code goes here...
10
7
  end
metadata CHANGED
@@ -1,16 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: element_component
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Paulo Correia
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2023-10-26 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
- description:
12
+ description: HTML Builder
14
13
  email:
15
14
  - joaopaulocorreia1010@gmail.com
16
15
  executables: []
@@ -19,22 +18,18 @@ extra_rdoc_files: []
19
18
  files:
20
19
  - ".rspec"
21
20
  - ".rubocop.yml"
22
- - Gemfile
23
- - Gemfile.lock
24
21
  - LICENSE.txt
25
22
  - README.md
26
23
  - Rakefile
27
- - bin/console
28
- - bin/setup
29
24
  - lib/element_component.rb
30
- - lib/element_component/core/element.rb
31
- - lib/element_component/core/maker.rb
25
+ - lib/element_component/element.rb
32
26
  - lib/element_component/version.rb
33
- homepage:
27
+ homepage: https://github.com/joaopaulocorreia/element_component
34
28
  licenses:
35
29
  - MIT
36
- metadata: {}
37
- post_install_message:
30
+ metadata:
31
+ homepage_uri: https://github.com/joaopaulocorreia/element_component
32
+ rubygems_mfa_required: 'true'
38
33
  rdoc_options: []
39
34
  require_paths:
40
35
  - lib
@@ -42,15 +37,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
42
37
  requirements:
43
38
  - - ">="
44
39
  - !ruby/object:Gem::Version
45
- version: 2.6.0
40
+ version: 3.1.0
46
41
  required_rubygems_version: !ruby/object:Gem::Requirement
47
42
  requirements:
48
43
  - - ">="
49
44
  - !ruby/object:Gem::Version
50
45
  version: '0'
51
46
  requirements: []
52
- rubygems_version: 3.4.10
53
- signing_key:
47
+ rubygems_version: 4.0.6
54
48
  specification_version: 4
55
- summary: Element Component
49
+ summary: HTML Builder
56
50
  test_files: []
data/Gemfile DELETED
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- # Specify your gem's dependencies in element_component.gemspec
6
- gemspec
7
-
8
- gem 'debug'
9
- gem 'rake', '~> 13.0'
10
- gem 'rspec', '~> 3.0'
11
- gem 'rubocop'
data/Gemfile.lock DELETED
@@ -1,65 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- element_component (0.1.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- ast (2.4.2)
10
- debug (1.5.0)
11
- irb (>= 1.3.6)
12
- reline (>= 0.2.7)
13
- diff-lcs (1.5.0)
14
- io-console (0.5.11)
15
- irb (1.4.1)
16
- reline (>= 0.3.0)
17
- parallel (1.22.1)
18
- parser (3.1.2.0)
19
- ast (~> 2.4.1)
20
- rainbow (3.1.1)
21
- rake (13.0.6)
22
- regexp_parser (2.3.1)
23
- reline (0.3.1)
24
- io-console (~> 0.5)
25
- rexml (3.2.5)
26
- rspec (3.11.0)
27
- rspec-core (~> 3.11.0)
28
- rspec-expectations (~> 3.11.0)
29
- rspec-mocks (~> 3.11.0)
30
- rspec-core (3.11.0)
31
- rspec-support (~> 3.11.0)
32
- rspec-expectations (3.11.0)
33
- diff-lcs (>= 1.2.0, < 2.0)
34
- rspec-support (~> 3.11.0)
35
- rspec-mocks (3.11.1)
36
- diff-lcs (>= 1.2.0, < 2.0)
37
- rspec-support (~> 3.11.0)
38
- rspec-support (3.11.0)
39
- rubocop (1.28.2)
40
- parallel (~> 1.10)
41
- parser (>= 3.1.0.0)
42
- rainbow (>= 2.2.2, < 4.0)
43
- regexp_parser (>= 1.8, < 3.0)
44
- rexml
45
- rubocop-ast (>= 1.17.0, < 2.0)
46
- ruby-progressbar (~> 1.7)
47
- unicode-display_width (>= 1.4.0, < 3.0)
48
- rubocop-ast (1.17.0)
49
- parser (>= 3.1.1.0)
50
- ruby-progressbar (1.11.0)
51
- unicode-display_width (2.1.0)
52
-
53
- PLATFORMS
54
- ruby
55
- x86_64-linux
56
-
57
- DEPENDENCIES
58
- debug
59
- element_component!
60
- rake (~> 13.0)
61
- rspec (~> 3.0)
62
- rubocop
63
-
64
- BUNDLED WITH
65
- 2.1.2
data/bin/console DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'element_component'
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/setup DELETED
@@ -1,8 +0,0 @@
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
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module El
4
- # represent the element base
5
- class Element
6
- attr_reader :element, :attributes, :contents
7
-
8
- def initialize(element, content: [], attribute: {}, closing_tag: true)
9
- @element = element
10
- @closing_tag = closing_tag
11
- @objects = []
12
-
13
- reset_attributes!
14
- reset_contents!
15
-
16
- if content.is_a? Array
17
- content.each { |item| add_content item }
18
- else
19
- add_content content
20
- end
21
-
22
- attribute.each_key { |key| add_attribute key, attribute[key] }
23
- end
24
-
25
- def add_content(content, reset: false)
26
- reset_contents! if reset
27
- @contents.push(content)
28
-
29
- content
30
- end
31
-
32
- def add_attribute(attribute, value, reset: false)
33
- attribute = attribute.to_sym
34
-
35
- @attributes.delete attribute if reset
36
-
37
- return @attributes[attribute].push(value) if @attributes.key?(attribute)
38
-
39
- @attributes[attribute] = [value]
40
- end
41
-
42
- def remove_attribute!(attribute)
43
- attribute = attribute.to_sym
44
- @attributes = @attributes.except(attribute)
45
- end
46
-
47
- def remove_attribute_value(attribute, value)
48
- attribute = attribute.to_sym
49
- attributes[attribute].delete(value)
50
- end
51
-
52
- def reset_contents!
53
- @contents = []
54
- end
55
-
56
- def reset_attributes!
57
- @attributes = {}
58
- end
59
-
60
- def build
61
- html = "<#{@element}"
62
- html << (mount_attributes.empty? ? '>' : " #{mount_attributes}>")
63
-
64
- html << mount_content
65
-
66
- html << "</#{@element}>" if @closing_tag
67
- end
68
-
69
- private
70
-
71
- def mount_attributes
72
- @attributes.map { |attr| "#{attr[0].to_sym}=\"#{attr[1].join(' ')}\"" }.join(' ')
73
- end
74
-
75
- def mount_content
76
- @contents.map { |content| content.is_a?(Element) ? content.build : content.to_s }.join
77
- end
78
- end
79
- end
@@ -1,9 +0,0 @@
1
- module El
2
- class Maker
3
- def method_missing(method, **args, &block)
4
- new_element = El::Element.new(method.to_s.gsub('_', '-'), **args)
5
- block.call new_element if block_given?
6
- new_element
7
- end
8
- end
9
- end