slotmachine 0.1.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
+ SHA1:
3
+ metadata.gz: 2281198bcb524876b314972ead2c826386193349
4
+ data.tar.gz: d740f4abfa0e853b81481bb9f7430b98d9f5d1ee
5
+ SHA512:
6
+ metadata.gz: 3f2ca18a9a64f4d4634fc90d27952a3e086f4748388bb462db31ce7ce6dfb8c81a86d1b98ae9e18d5df5c0b4c3d5c55f009cff8e31ad8ca331d18d34c107a5e3
7
+ data.tar.gz: bb5ce7aadc08b0e8ee655eef878093d92336e40af865f82bb5160f70b5c63bab6cf52fc141f9629999f6dae4663b38c6e3521982a53aec4d68742fcd4e27a06a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Cedric Feyaerts
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,144 @@
1
+ # SlotMachine
2
+
3
+ Slot Machine is a rails plugin to help you write clean, readable views. It hides the ugly css in partials away from your views.
4
+ Slot Machine transforms partials into helper methods. Each partial contains slots into which you can insert data, html or other partials.
5
+
6
+ ## Installation
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'slot_machine'
11
+ ```
12
+
13
+ And then execute:
14
+ ```bash
15
+ $ bundle
16
+ ```
17
+
18
+ Or install it yourself as:
19
+ ```bash
20
+ $ gem install slot_machine
21
+ ```
22
+
23
+ Create an initializer : `config/initializers/slot_machine.rb`
24
+ ```ruby
25
+ require "slot_machine"
26
+
27
+ # The path where your partials will be stored
28
+ # default is Rails.root.join('app/views/slots/_*')
29
+ SlotMachine::Config.path = MyEngine::Engine.root.join('app/views/slots/_*')
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ### The simplest case
35
+
36
+ Create your first partial (app/views/slots/_my_first_partial.html.slim)
37
+
38
+ ```slim
39
+ .my_first_partial #This is not required just an example
40
+ h2 = slots[:title]
41
+ h3 = slots[:subtitle]
42
+ .content
43
+ = slots[:content]
44
+ ```
45
+
46
+ You can now write in your view:
47
+
48
+ ```slim
49
+ = my_first_partial title: 'foo', subtitle: 'bar', content: 'awesome content'
50
+ ```
51
+
52
+ or more usefull
53
+
54
+ ```slim
55
+ = my_first_partial title: 'foo', subtitle: 'bar' do |mfp|
56
+ - mfp.slot :content
57
+ div this is great
58
+ ```
59
+ ### A more interesting example
60
+
61
+ So far you could do the same with a partial. I know let's try something better.
62
+
63
+ #### The slot main
64
+
65
+ First there is a special slot by default: `slots[main]`
66
+
67
+ _card.html.slim
68
+ ```slim
69
+ .card
70
+ .card__title = slots[:title]
71
+ .card__content
72
+ = slots[:main] # yep that's the one
73
+ ```
74
+
75
+ you can now do:
76
+
77
+ ```slim
78
+ = card title: 'foo' do
79
+ div this is great
80
+ ```
81
+
82
+ #### More then one slot
83
+
84
+ _modal.html.slim
85
+ ```slim
86
+ .modal
87
+ .modal__title = slots[:title]
88
+ .modal__content
89
+ = slots[:main]
90
+ .modal__footer
91
+ = slots[:footer]
92
+ ```
93
+
94
+ It allows you to write something like this:
95
+
96
+ ```slim
97
+ = modal title: 'foo' do |modal|
98
+ div hey look I'm a modal
99
+ - modal.slot :footer
100
+ div With a footer
101
+ ```
102
+
103
+ #### And combine
104
+
105
+ With the two partials from before (modal and card) we can now do this:
106
+
107
+ ```slim
108
+ = modal title: 'foo' do |modal|
109
+ = card title: 'a card in a modal do
110
+ div And wait there is still more
111
+
112
+ - modal.slot :footer
113
+ = card title: 'a card in a modal's foot do
114
+ div My example are getting a bit far fetched
115
+ ```
116
+
117
+ There is a shortener for that last one
118
+
119
+ ```slim
120
+ = modal title: 'foo' do |modal|
121
+ - modal.card slot: :main, title: 'a card in a modal do
122
+ div And wait there is still more
123
+
124
+ - modal.card slot: :footer title: 'a card in a modal's foot do
125
+ div My example are getting a bit far fetched
126
+ ```
127
+
128
+ You can also call the slot several times like thus:
129
+
130
+ ```slim
131
+ = modal title: 'foo' do |modal|
132
+ - modal.card slot: :footer title: 'first card'
133
+ - modal.card slot: :footer title: 'second card'
134
+ - modal.card slot: :footer title: 'third card in a modal'
135
+ ```
136
+
137
+ ## What else?
138
+
139
+ You noticed all my example are with slim. But it work great with yaml or erb.
140
+
141
+ I also name my partials with the [BEM notation](http://getbem.com/). I find it easier to organise them by components specialy if you already use this same notation with your css.
142
+
143
+ ## License
144
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SlotMachine'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,7 @@
1
+ # Main module for Slot Machine
2
+ module SlotMachine; end
3
+
4
+ require 'slot_machine/config'
5
+ require 'slot_machine/railtie'
6
+ require 'slot_machine/helper'
7
+ require 'slot_machine/application_controller_concern'
@@ -0,0 +1,9 @@
1
+ # Module to be included in controller to enable the helpers
2
+ # Usually in application_controller.rb
3
+ module SlotMachine::ApplicationControllerConcern
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ helper SlotMachine::Helper
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ # Config where is store all informations
2
+ module SlotMachine::Config
3
+ def self.read_slots
4
+ slots_array = Dir[path].map do |path|
5
+ basename = File.basename(path, File.extname(path))
6
+ basename = basename.split('.').first
7
+ basename.slice!(0)
8
+ method_name = basename.parameterize.underscore.to_sym
9
+ partial = File.join('slots', basename)
10
+ [method_name, partial]
11
+ end
12
+
13
+ @slots = Hash[slots_array]
14
+ end
15
+
16
+ def self.method_names
17
+ @slots.keys
18
+ end
19
+
20
+ def self.partial(method_name)
21
+ @slots[method_name]
22
+ end
23
+
24
+ def self.path
25
+ @path || Rails.root.join('app/views/slots/_*')
26
+ end
27
+
28
+ def self.path=(value)
29
+ @path = value
30
+ end
31
+ end
@@ -0,0 +1,77 @@
1
+ # Helper to be included in application_controller
2
+ module SlotMachine::Helper
3
+ # In the the view:
4
+ # `= foo label: 'toto' do 'something awesome' end`
5
+ #
6
+ # In the partial foo:
7
+ # `slots[:label] == label == 'toto'`
8
+ # `slots[:main] == main == 'something awesome'`
9
+ #
10
+ # so you should use `slots[:something]` when it could be unassigned
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ SlotMachine::Config.read_slots
15
+ SlotMachine::Config.method_names.each do |method_name|
16
+ define_method(method_name) do |locals = {}, &block|
17
+ # that is some weird shit but il allow to access all the local_assigns
18
+ # within the slots and still access the directly
19
+ locals[:slots] = locals
20
+
21
+ # Here we create the builder
22
+ # we execute the block while passing the builder
23
+ # all the slotted partial will be in the builder
24
+ # and activeview buffer is captured and slotted
25
+ # in the main slot
26
+ if block
27
+ builder = Builder.new(self)
28
+ captured = capture { block.call(builder) }
29
+ builder.append_slot(:main, captured)
30
+ locals[:slots] = locals[:slots].merge(builder.slots)
31
+ locals[:m] = builder
32
+ end
33
+ render SlotMachine::Config.partial(method_name), locals
34
+ end
35
+ end
36
+
37
+ # A builder for the different partial
38
+ class Builder
39
+ attr_internal :slots
40
+
41
+ def initialize(template)
42
+ @template = template
43
+ end
44
+
45
+ # attribute reader
46
+ def slots
47
+ @slots ||= Hash.new { |h, k| h[k] = ActiveSupport::SafeBuffer.new('') }
48
+ end
49
+
50
+ def slot?(name)
51
+ !slots[name].blank?
52
+ end
53
+
54
+ # append the named slot
55
+ def append_slot(name = :main, string = nil, &block)
56
+ slots[name].concat(@template.get_buffer(string, &block))
57
+ nil # why the fuck do I need this. It shouldn't put anything in the buffer but it does.
58
+ end
59
+ alias_method :slot, :append_slot
60
+
61
+ SlotMachine::Config.method_names.each do |method_name|
62
+ define_method(method_name) do |args = { slot: :main }, &block|
63
+ slot_name = args.delete(:slot)
64
+ rendered_partial = @template.send(method_name, args, &block)
65
+ append_slot(slot_name, rendered_partial)
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ # this one should be in the builder but it doesnt have the builder method
72
+ def get_buffer(string = nil, &block)
73
+ return string unless block
74
+
75
+ capture { yield }
76
+ end
77
+ end
@@ -0,0 +1,4 @@
1
+ class SlotMachine::Railtie < Rails::Railtie
2
+ config.to_prepare do
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module SlotMachine
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slotmachine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Cedric Feyaerts
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-06 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: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ description: It hide the ugly css and html from your views
28
+ email:
29
+ - c.feyaerts@adhoc-gti.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/slot_machine.rb
38
+ - lib/slot_machine/application_controller_concern.rb
39
+ - lib/slot_machine/config.rb
40
+ - lib/slot_machine/helper.rb
41
+ - lib/slot_machine/railtie.rb
42
+ - lib/slot_machine/version.rb
43
+ homepage: https://gitlab.com/cedricfeyaerts/slot-machine
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
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: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.6.13
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: A Simple gem to write readable views
67
+ test_files: []