lite-component 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/_config.yml ADDED
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-leap-day
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Component
5
+ module ComponentHelper
6
+
7
+ def component(name, attrs = nil, &block)
8
+ name = name.component_path if name.respond_to?(:component_path)
9
+ klass = "#{name}_component".classify.constantize.new(self, attrs, &block)
10
+ klass.render
11
+ end
12
+
13
+ end
14
+ end
15
+ end
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 'lite/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 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,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ class ComponentGenerator < Rails::Generators::NamedBase
6
+
7
+ class_option :skip_erb, type: :boolean, default: false
8
+ class_option :skip_css, type: :boolean, default: false
9
+ class_option :skip_js, type: :boolean, default: false
10
+
11
+ source_root File.expand_path('../templates', __FILE__)
12
+
13
+ def create_component_file
14
+ template('install.rb.erb', "app/components/#{name}_component.rb")
15
+ end
16
+
17
+ def create_erb_file
18
+ return if options['skip_erb']
19
+
20
+ name_parts = name.split('/')
21
+ file_parts = name_parts[0..-2]
22
+ file_parts << "_#{name_parts.last}.html.erb"
23
+
24
+ create_file("app/views/components/#{file_parts.join('/')}")
25
+ end
26
+
27
+ def copy_javascript_file
28
+ return if options['skip_js']
29
+
30
+ copy_file('install.js', "app/assets/javascripts/components/#{name}.js")
31
+ end
32
+
33
+ def copy_stylesheet_file
34
+ return if options['skip_css']
35
+
36
+ copy_file('install.scss', "app/assets/stylesheets/components/#{name}.scss")
37
+ end
38
+
39
+ end
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching component here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,2 @@
1
+ class <%= name.classify %>Component < <%= ApplicationComponent.to_s rescue 'Lite::Component::Base' %>
2
+ end
@@ -0,0 +1,3 @@
1
+ // Place all the styles related to the component here.
2
+ // They will automatically be included in application.css.
3
+ // You can use Sass (SCSS) here: https://sass-lang.com/
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ %w[version errors application engine element base].each do |filename|
4
+ require "lite/component/#{filename}"
5
+ end
6
+
7
+ require 'generators/component_generator'
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Component
5
+
6
+ def self.names
7
+ components_ext = '_component.rb'
8
+ components_dir = "#{path}/"
9
+ components_glob = path.join("**/*#{components_ext}")
10
+
11
+ Dir.glob(components_glob).map { |name| name.sub(components_dir, '').chomp(components_ext) }
12
+ end
13
+
14
+ def self.path
15
+ Rails.root.join('app/components')
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Component
5
+ class Base < Lite::Component::Element
6
+
7
+ class << self
8
+
9
+ def component_name
10
+ component_path.split('/').last
11
+ end
12
+
13
+ def component_path
14
+ name.chomp('Component').underscore
15
+ end
16
+
17
+ def model_name
18
+ ActiveModel::Name.new(Lite::Component::Base)
19
+ end
20
+
21
+ end
22
+
23
+ def render
24
+ @view.render(partial: to_partial_path, object: self)
25
+ end
26
+
27
+ def to_partial_path
28
+ "components/#{self.class.component_path}"
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Component
5
+ class Element
6
+
7
+ include ActiveModel::Validations
8
+
9
+ def initialize(view, attributes = nil, &block)
10
+ initialize_attributes(attributes || {})
11
+ initialize_elements
12
+
13
+ @view = view
14
+ @yield = block_given? ? @view.capture(self, &block) : nil
15
+
16
+ validate!
17
+ rescue ActiveModel::ValidationError => e
18
+ raise Lite::Component::ValidationError, e.message
19
+ end
20
+
21
+ class << self
22
+
23
+ def attribute(name, default: nil)
24
+ attributes[name] = { default: default }
25
+ define_method_or_raise(name) { get_instance_variable(name) }
26
+ end
27
+
28
+ def attributes
29
+ @attributes ||= {}
30
+ end
31
+
32
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
33
+ # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
34
+ def element(name, multiple: false, &config)
35
+ plural_name = name.to_s.pluralize.to_sym if multiple
36
+
37
+ elements[name] = {
38
+ multiple: plural_name || false,
39
+ class: Class.new(Element, &config)
40
+ }
41
+
42
+ define_method_or_raise(name) do |attributes = nil, &block|
43
+ return get_instance_variable(multiple ? plural_name : name) unless attributes || block
44
+
45
+ element = self.class.elements[name][:class].new(@view, attributes, &block)
46
+
47
+ if multiple
48
+ get_instance_variable(plural_name) << element
49
+ else
50
+ set_instance_variable(name, element)
51
+ end
52
+ end
53
+
54
+ return if !multiple || name == plural_name
55
+
56
+ define_method_or_raise(plural_name) { get_instance_variable(plural_name) }
57
+ end
58
+ # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
59
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
60
+
61
+ def elements
62
+ @elements ||= {}
63
+ end
64
+
65
+ def model_name
66
+ ActiveModel::Name.new(Lite::Component::Element)
67
+ end
68
+
69
+ private
70
+
71
+ def define_method_or_raise(method_name, &block)
72
+ return define_method(method_name, &block) unless method_defined?(method_name.to_sym)
73
+
74
+ raise Lite::Component::BuildError, "Method #{method_name.inspect} already exists"
75
+ end
76
+
77
+ end
78
+
79
+ def to_s
80
+ @yield
81
+ end
82
+
83
+ alias render to_s
84
+
85
+ protected
86
+
87
+ def initialize_attributes(attributes)
88
+ self.class.attributes.each do |name, options|
89
+ set_instance_variable(
90
+ name,
91
+ attributes[name] || (options[:default] && options[:default].dup)
92
+ )
93
+ end
94
+ end
95
+
96
+ def initialize_elements
97
+ self.class.elements.each do |name, options|
98
+ if (plural_name = options[:multiple])
99
+ set_instance_variable(plural_name, [])
100
+ else
101
+ set_instance_variable(name, nil)
102
+ end
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def get_instance_variable(name)
109
+ instance_variable_get(:"@#{name}")
110
+ end
111
+
112
+ def set_instance_variable(name, value)
113
+ instance_variable_set(:"@#{name}", value)
114
+ end
115
+
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/engine'
4
+
5
+ module Lite
6
+ module Component
7
+ class Engine < ::Rails::Engine
8
+
9
+ isolate_namespace Lite::Component
10
+
11
+ initializer('lite-frontend.setup', group: :all) do |app|
12
+ app.paths['config'] << File.join(config.root, 'app')
13
+ app.paths['config'] << File.join(config.root, 'vendor')
14
+ end
15
+
16
+ initializer('lite-component.asset_path') do |app|
17
+ app.config.assets.paths << Lite::Component.path if app.config.respond_to?(:assets)
18
+ end
19
+
20
+ initializer('lite-component.view_helpers') do
21
+ ActiveSupport.on_load(:action_controller) do
22
+ helper Lite::Component::ComponentHelper
23
+ end
24
+ end
25
+
26
+ initializer('lite-component.view_paths') do
27
+ ActiveSupport.on_load(:action_controller) do
28
+ append_view_path Lite::Component.path
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Component
5
+
6
+ class BuildError < StandardError; end
7
+ class ValidationError < StandardError; end
8
+
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Component
5
+
6
+ VERSION ||= '1.0.0'
7
+
8
+ end
9
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'lite/component/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'lite-component'
9
+ spec.version = Lite::Component::VERSION
10
+ spec.authors = ['Juan Gomez']
11
+ spec.email = %w[j.gomez@drexed.com]
12
+
13
+ spec.summary = 'Generate component from collections of data points'
14
+ spec.homepage = 'http://drexed.github.io/lite-component'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata.merge(
21
+ 'allowed_push_host' => 'https://rubygems.org',
22
+ 'changelog_uri' => 'https://github.com/drexed/lite-component/blob/master/CHANGELOG.md',
23
+ 'homepage_uri' => spec.homepage,
24
+ 'source_code_uri' => 'https://github.com/drexed/lite-component'
25
+ )
26
+ else
27
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
28
+ 'public gem pushes.'
29
+ end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the ComponentGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
36
+ spec.bindir = 'exe'
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = %w[lib]
39
+
40
+ spec.add_runtime_dependency 'rails', '>= 5.1.0'
41
+
42
+ spec.add_development_dependency 'bundler'
43
+ spec.add_development_dependency 'fasterer'
44
+ spec.add_development_dependency 'generator_spec'
45
+ spec.add_development_dependency 'rake'
46
+ spec.add_development_dependency 'rspec'
47
+ spec.add_development_dependency 'rubocop'
48
+ spec.add_development_dependency 'rubocop-performance'
49
+ spec.add_development_dependency 'rubocop-rspec'
50
+ end
@@ -0,0 +1,7 @@
1
+ <% Lite::Component.names.each do |name| %>
2
+ <% begin %>
3
+ <% require_asset("components/#{name}") %>
4
+ <% rescue Sprockets::FileNotFound %>
5
+ <%# Do nothing %>
6
+ <% end %>
7
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <% Lite::Component.names.each do |name| %>
2
+ <% begin %>
3
+ <% require_asset("components/#{name}") %>
4
+ <% rescue Sprockets::FileNotFound %>
5
+ <%# Do nothing %>
6
+ <% end %>
7
+ <% end %>
metadata ADDED
@@ -0,0 +1,199 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lite-component
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Juan Gomez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fasterer
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: generator_spec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-performance
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - j.gomez@drexed.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".fasterer.yml"
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".rubocop.yml"
150
+ - ".travis.yml"
151
+ - CHANGELOG.md
152
+ - CODE_OF_CONDUCT.md
153
+ - Gemfile
154
+ - Gemfile.lock
155
+ - LICENSE.txt
156
+ - README.md
157
+ - Rakefile
158
+ - _config.yml
159
+ - app/helpers/lite/component/component_helper.rb
160
+ - bin/console
161
+ - bin/setup
162
+ - lib/generators/component_generator.rb
163
+ - lib/generators/templates/install.js
164
+ - lib/generators/templates/install.rb.erb
165
+ - lib/generators/templates/install.scss
166
+ - lib/lite/component.rb
167
+ - lib/lite/component/application.rb
168
+ - lib/lite/component/base.rb
169
+ - lib/lite/component/element.rb
170
+ - lib/lite/component/engine.rb
171
+ - lib/lite/component/errors.rb
172
+ - lib/lite/component/version.rb
173
+ - lite-component.gemspec
174
+ - vendor/assets/javascripts/lite-component.js.erb
175
+ - vendor/assets/stylesheets/lite-component.css.erb
176
+ homepage: http://drexed.github.io/lite-component
177
+ licenses:
178
+ - MIT
179
+ metadata: {}
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ required_rubygems_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ requirements: []
195
+ rubygems_version: 3.0.6
196
+ signing_key:
197
+ specification_version: 4
198
+ summary: Generate component from collections of data points
199
+ test_files: []