Class: SlotMachine::Service
- Inherits:
-
Object
- Object
- SlotMachine::Service
- Defined in:
- lib/slot_machine/service.rb
Class Method Summary collapse
Instance Method Summary collapse
- #read_slots(custom_path = nil) ⇒ Object
- #render_slot(template, partial, locals = {}, &block) ⇒ Object
Class Method Details
.instance ⇒ Object
2 3 4 5 6 |
# File 'lib/slot_machine/service.rb', line 2 def self.instance return @instance unless @instance.nil? @instance = new end |
Instance Method Details
#read_slots(custom_path = nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/slot_machine/service.rb', line 30 def read_slots(custom_path = nil) globs = (custom_path || Rails.root.join('app/views/slots/_*')) globs = [globs] unless globs.is_a?(Array) paths_array = globs.map do |g| Dir[g] end.flatten slots_array = paths_array.map do |p| basename = File.basename(p, File.extname(p)) basename = basename.split('.').first basename.slice!(0) method_name = basename.parameterize.underscore.to_sym partial_path = File.dirname(p).split('/views/').last partial = File.join(partial_path, basename) [method_name, partial] end Hash[slots_array] end |
#render_slot(template, partial, locals = {}, &block) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/slot_machine/service.rb', line 8 def render_slot(template, partial, locals = {}, &block) # that is some weird shit but il allow to access all the local_assigns # within the slots and still access the directly locals[:slots] = locals # Here we create the builder # we execute the block while passing the builder # all the slotted partial will be in the builder # and activeview buffer is captured and slotted # in the main slot if block builder = SlotMachine::Builder.new(template) captured = template.capture { yield(builder) } builder.append_slot(:main, captured) locals[:slots] = locals[:slots].merge(builder.slots) locals[:m] = builder end # Render le partial template.render partial, locals end |