class Expo
Constants
- CALL_ORDER
- TRIVIAL_PRESENTER
Public Class Methods
new(presenter=nil, &block)
click to toggle source
# File lib/expo.rb, line 12 def initialize(presenter=nil, &block) @presenter = presenter @block = block @message_map = {} @subs = {} @collections = {} @inner_calls = [] @id = self.object_id end
Public Instance Methods
aside(*messages)
click to toggle source
# File lib/expo.rb, line 67 def aside(*messages) array, hash = parse_arguments(messages) @context.merge!(hasherize(array) {|msg| @preso.send(msg) }) hash.each do |k,v| @context[v] = @preso.send(k) end nil end
collection(key, collection, item_expo)
click to toggle source
# File lib/expo.rb, line 91 def collection(key, collection, item_expo) @inner_calls << { method: :collection_inner, args: [key, collection, item_expo], block: nil } nil end
expo(object, context={})
click to toggle source
# File lib/expo.rb, line 35 def expo(object, context={}) @object = object @context = context if object.nil? return nil end @preso = Proxy.new( object, (@presenter || TRIVIAL_PRESENTER).present(object) ) if !@block return {} end block_return = self.instance_exec @object, context, &@block @inner_calls.each do |call| self.send(call[:method], *call[:args], &call[:block]) end exposition = {} @message_map.each do |k,v| exposition[v] = @preso.send(k) end exposition.merge!(@subs) exposition.merge!(@collections) self.reset if block_return exposition.merge(block_return) else exposition end end
expo_collection(objects, context={})
click to toggle source
# File lib/expo.rb, line 22 def expo_collection(objects, context={}) objects.map.with_index do |object, index| Expo.new(@presenter, &@block).expo(object, context.merge({index: index})) end end
expose(*messages)
click to toggle source
# File lib/expo.rb, line 76 def expose(*messages) array, hash = parse_arguments(messages) @message_map.merge!(hasherize(array).merge(hash)) nil end
reset()
click to toggle source
# File lib/expo.rb, line 28 def reset @message_map = {} @subs = {} @collections = {} @inner_calls = [] end
sub_expo(key, object, sub_expo_passed)
click to toggle source
# File lib/expo.rb, line 82 def sub_expo(key, object, sub_expo_passed) @inner_calls << { method: :sub_expo_inner, args: [key, object, sub_expo_passed], block: nil } nil end
Private Instance Methods
collection_inner(key, collection, item_expo)
click to toggle source
# File lib/expo.rb, line 122 def collection_inner(key, collection, item_expo) recurse_expo = item_expo sub_id = item_expo.send(:get_id) if sub_id == @id recurse_expo = Expo.new(@presenter, &@block).send(:set_id, @id) end if !collection @collections[key] = nil return nil end collection_exposition = [] collection.each.with_index do |item, index| collection_exposition << recurse_expo.expo(item, @context.merge({ "#{key}_index".to_sym => index })) end @collections[key] = collection_exposition nil end
get_id()
click to toggle source
# File lib/expo.rb, line 107 def get_id @id end
hasherize(array, &block)
click to toggle source
# File lib/expo.rb, line 142 def hasherize(array, &block) block ||= ->(e) { e } Hash[*(array.map{|e| [e, block.call(e)] }.flatten(1))] end
parse_arguments(args)
click to toggle source
# File lib/expo.rb, line 147 def parse_arguments(args) last = args.pop array = args hash = {} if last.is_a?(Hash) hash = last elsif last array << last end [array, hash] end
set_id(id)
click to toggle source
# File lib/expo.rb, line 102 def set_id(id) @id = id self end
sub_expo_inner(key, object, sub_expo_passed)
click to toggle source
# File lib/expo.rb, line 111 def sub_expo_inner(key, object, sub_expo_passed) recurse_expo = sub_expo_passed sub_id = sub_expo_passed.send(:get_id) if sub_id == @id recurse_expo = Expo.new(@presenter, &@block).send(:set_id, @id) end sub = recurse_expo.expo(object, @context) @subs[key] = sub if sub nil end