Module: Seabright::Views::ClassMethods

Defined in:
lib/redis_object/ext/views.rb

Instance Method Summary collapse

Instance Method Details

#named_view(name, *fields) ⇒ Object



57
58
59
# File 'lib/redis_object/ext/views.rb', line 57

def named_view(name,*fields)
	named_views[name] = normalize_field_options(fields)
end

#named_viewsObject



61
62
63
# File 'lib/redis_object/ext/views.rb', line 61

def named_views
	@named_views ||= {}
end

#normalize_field_options(fields) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/redis_object/ext/views.rb', line 65

def normalize_field_options(fields)
	fields.flatten!
	fields.uniq!

	options = {}
	if fields.last.is_a?(Hash) # assume an option hash
		options.merge!(fields.slice!(fields.size - 1, 1)[0])
	end

	# assign a the method as a symbol to be exclusively invoked on view
	# so instead of returning a hash on view, it will return only what was
	# produced by calling the method.
	if options.keys.size > 0 and options[:method]
		out = options[:method].to_sym
	else
		hash = fields.select {|f| f.is_a?(Hash) }.inject({},:merge)
		out = {}
		if (h = hash.select {|k,v| !v.is_a?(Proc) }) && h.count > 0
			out[:hashes] = h
		end
		if (h = hash.select {|k,v| v.is_a?(Proc) }) && h.count > 0
			out[:procs] = h
		end
		if (h = fields.select {|o| o.is_a?(String) || o.is_a?(Symbol) }) && h.count > 0
			out[:fields] = h
		end
	end
	out
end