slotmachine 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2281198bcb524876b314972ead2c826386193349
4
- data.tar.gz: d740f4abfa0e853b81481bb9f7430b98d9f5d1ee
3
+ metadata.gz: 2888bc896a4e6518d88c01b1fa1e9ecf39b07dd2
4
+ data.tar.gz: cb08aec43d4b45d705ca312b2c2a8dab1537d536
5
5
  SHA512:
6
- metadata.gz: 3f2ca18a9a64f4d4634fc90d27952a3e086f4748388bb462db31ce7ce6dfb8c81a86d1b98ae9e18d5df5c0b4c3d5c55f009cff8e31ad8ca331d18d34c107a5e3
7
- data.tar.gz: bb5ce7aadc08b0e8ee655eef878093d92336e40af865f82bb5160f70b5c63bab6cf52fc141f9629999f6dae4663b38c6e3521982a53aec4d68742fcd4e27a06a
6
+ metadata.gz: ece189ab4344b53364c7aafa4097691c4803e83ccd2ce8cf74e967572681d235eabf25a16a17bef2a777da3997188826c5aa31889088e48d80f15a00fcba21bd
7
+ data.tar.gz: cde30b1568177c73af6085c74d50bc83920bd9d36248e05c17c4dd06515b53a2e147a24d9434271749c79003287d1f02b1fce5a0a72a5339957278477a54cc90
File without changes
data/README.md CHANGED
@@ -29,6 +29,13 @@ require "slot_machine"
29
29
  SlotMachine::Config.path = MyEngine::Engine.root.join('app/views/slots/_*')
30
30
  ```
31
31
 
32
+ And then in your application controller
33
+ ```ruby
34
+ include SlotMachine::ControllerConcern
35
+ helper slot_machine_helper(path: 'you_can_override_the_path_of_the_slots_here/_*')
36
+ ```
37
+
38
+
32
39
  ## Usage
33
40
 
34
41
  ### The simplest case
@@ -2,6 +2,4 @@
2
2
  module SlotMachine; end
3
3
 
4
4
  require 'slot_machine/config'
5
- require 'slot_machine/railtie'
6
- require 'slot_machine/helper'
7
- require 'slot_machine/application_controller_concern'
5
+ require 'slot_machine/controller_concern'
@@ -1,12 +1,13 @@
1
1
  # Config where is store all informations
2
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))
3
+ def self.read_slots(custom_path = nil)
4
+ slots_array = Dir[custom_path || path].map do |p|
5
+ basename = File.basename(p, File.extname(p))
6
6
  basename = basename.split('.').first
7
7
  basename.slice!(0)
8
8
  method_name = basename.parameterize.underscore.to_sym
9
- partial = File.join('slots', basename)
9
+ partial_path = File.dirname(p).split('/views/').last
10
+ partial = File.join(partial_path, basename)
10
11
  [method_name, partial]
11
12
  end
12
13
 
@@ -24,8 +25,4 @@ module SlotMachine::Config
24
25
  def self.path
25
26
  @path || Rails.root.join('app/views/slots/_*')
26
27
  end
27
-
28
- def self.path=(value)
29
- @path = value
30
- end
31
28
  end
@@ -0,0 +1,102 @@
1
+ # Helper to be included in application_controller
2
+ module SlotMachine::ControllerConcern
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
+ # A builder class base for the builder of the different partials
14
+ class Builder
15
+ attr_internal :slots
16
+
17
+ def initialize(template)
18
+ @template = template
19
+ end
20
+
21
+ # attribute reader
22
+ def slots
23
+ @slots ||= Hash.new { |h, k| h[k] = ActiveSupport::SafeBuffer.new('') }
24
+ end
25
+
26
+ def slot?(name)
27
+ !slots[name].blank?
28
+ end
29
+
30
+ # append the named slot
31
+ def append_slot(name = :main, string = nil, &block)
32
+ slots[name].concat(@template.get_buffer(string, &block))
33
+ nil # why the fuck do I need this. It shouldn't put anything in the buffer but it does.
34
+ end
35
+ alias_method :slot, :append_slot
36
+ end
37
+
38
+ class_methods do
39
+ # create the module to be called as a helper
40
+ # Typically :
41
+ # ```
42
+ # include SlotMachine::ControllerConcern
43
+ # helper slot_machine_create_module(my_path)
44
+ # ````
45
+ def slot_machine_helper(path: nil)
46
+ return @helper_module unless @helper_module.nil?
47
+
48
+ # Read slot on the custom path
49
+ slots = SlotMachine::Config.read_slots(path)
50
+ method_names = slots.keys
51
+
52
+ # Instantiate the new module
53
+ @helper_module = Module.new
54
+
55
+ # Create a child builder class with all the methods
56
+ builder_class = Class.new(Builder)
57
+
58
+ builder_class.class_eval do
59
+ method_names.each do |method_name|
60
+ define_method(method_name) do |args = { slot: :main }, &block|
61
+ slot_name = args.delete(:slot)
62
+ rendered_partial = @template.send(method_name, args, &block)
63
+ append_slot(slot_name, rendered_partial)
64
+ end
65
+ end
66
+ end
67
+
68
+ # Add 1 method per partial in the module as well
69
+ method_names.each do |method_name|
70
+ @helper_module.module_eval do
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
+ define_method(method_name) do |locals = {}, &block|
78
+ # that is some weird shit but il allow to access all the local_assigns
79
+ # within the slots and still access the directly
80
+ locals[:slots] = locals
81
+
82
+ # Here we create the builder
83
+ # we execute the block while passing the builder
84
+ # all the slotted partial will be in the builder
85
+ # and activeview buffer is captured and slotted
86
+ # in the main slot
87
+ if block
88
+ builder = builder_class.new(self)
89
+ captured = capture { block.call(builder) }
90
+ builder.append_slot(:main, captured)
91
+ locals[:slots] = locals[:slots].merge(builder.slots)
92
+ locals[:m] = builder
93
+ end
94
+ render slots[method_name], locals
95
+ end
96
+ end
97
+ end
98
+
99
+ @helper_module
100
+ end
101
+ end
102
+ end
@@ -1,3 +1,3 @@
1
1
  module SlotMachine
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -0,0 +1 @@
1
+ require 'slot_machine'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slotmachine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cedric Feyaerts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-06 00:00:00.000000000 Z
11
+ date: 2019-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,22 +24,21 @@ dependencies:
24
24
  - - ">"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0
27
- description: It hide the ugly css and html from your views
27
+ description: Hides the ugly css and html from your views
28
28
  email:
29
29
  - c.feyaerts@adhoc-gti.com
30
30
  executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
- - MIT-LICENSE
34
+ - LICENSE
35
35
  - README.md
36
36
  - Rakefile
37
37
  - lib/slot_machine.rb
38
- - lib/slot_machine/application_controller_concern.rb
39
38
  - lib/slot_machine/config.rb
40
- - lib/slot_machine/helper.rb
41
- - lib/slot_machine/railtie.rb
39
+ - lib/slot_machine/controller_concern.rb
42
40
  - lib/slot_machine/version.rb
41
+ - lib/slotmachine.rb
43
42
  homepage: https://gitlab.com/cedricfeyaerts/slot-machine
44
43
  licenses:
45
44
  - MIT
@@ -1,9 +0,0 @@
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
@@ -1,77 +0,0 @@
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
@@ -1,4 +0,0 @@
1
- class SlotMachine::Railtie < Rails::Railtie
2
- config.to_prepare do
3
- end
4
- end