Files

Class Index [+]

Quicksearch

UbiquoDesign::Structure

Public Class Methods

clear(context = nil) click to toggle source

Cleans the current structure

  context: possible context for multiple structures
    # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 42
42:       def clear(context = nil)
43:         with_scope(context) { current_base.clear }
44:       end
concat_merge(original) click to toggle source

Performs a merge concatenating the results sharing keys

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 135
135:       def concat_merge original
136:         {}.tap do |result|
137:           original.each do |element|
138:             element.each_pair do |key, value|
139:               if value.is_a?(Array)
140:                 result[key] ||= []
141:                 result[key].concat value
142:                 merge_if_equals(result[key])
143:               else
144:                 result[key] = value
145:               end
146:             end
147:           end
148:         end
149:       end
current_base() click to toggle source

Returns the current base array, given the applied scopes

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 115
115:       def current_base
116:         current = @@structures
117:         @@scopes.each do |scope|
118:           next unless scope
119:           if !struct = find_in_scope(current, scope)
120:             current << (struct = {scope => []})
121:           end
122:           current = struct.values.first
123:         end
124:         current
125:       end
define(context = nil, &block) click to toggle source

Starts the definition of an structure

  context: possible context to create multiple structures
    # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 11
11:       def define(context = nil, &block)
12:         with_scope(context) do
13:           yield_inside(&block)
14:         end
15:       end
filter(filters, base = nil) click to toggle source

Given a set of filters, returns the appropiate information

  filters:  can be either nil (no filter) or a hash of conditions
            in the form {:filter => value}
  base:     base set of results to filter. Defaults to current_base
     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 76
 76:       def filter(filters, base = nil)
 77:         results = {}
 78:         base ||= current_base
 79:         case filters
 80:         when Hash
 81:           results = []
 82:           base.each do |element|
 83:             element.keys.each do |element_key|
 84:               # retrieve the filters keys that may limit this element_key
 85:               applicable_keys = filters.keys.select do |key|
 86:                 scope_name(key) == element_key
 87:               end
 88: 
 89:               # if there are filters to apply, compare its value
 90:               unless applicable_keys.blank?
 91:                 found = find_in_scope(
 92:                   element[element_key],
 93:                   filters[applicable_keys.first]
 94:                 )
 95:                 results.concat(found.values.flatten) if found
 96:               else
 97:                 results << element
 98:               end
 99:             end
100:           end
101:           # call recursively while it's filtering
102:           results = filter(filters, results) unless results == base
103:         else
104:           results = base
105:         end
106:         results
107:       end
find_in_scope(scope, element) click to toggle source

Returns the hash scope element in the scope, if exists

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 128
128:       def find_in_scope scope, element
129:         scope.select do |struct|
130:           struct.keys.include?(element)
131:         end.first
132:       end
get(*args) click to toggle source

Returns a hash with the required structure

  args: can either be
        nil (default): returns the whole structure as stored
        a hash, in this case it's interpreted as a filter
        [context, hash] to use a non-default context to filter
    # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 22
22:       def get(*args)
23:         result = case args[0]
24:         when String, Symbol
25:           with_scope(args[0].to_sym) do
26:             filter(args[1])
27:           end
28:         else
29:           filter(args[0])
30:         end
31:         concat_merge(result)
32:       end
merge_if_equals(array) click to toggle source

Given an array of hashes, will merge them if they share the key

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 152
152:       def merge_if_equals(array)
153:         merged = []
154:         keymap = {}
155:         array.each do |hash|
156:           key = hash.keys.first
157:           if i = keymap[key]
158:             merged[i].values.first.concat hash.values.first
159:           else
160:             keymap[key] = merged.size
161:             merged << hash
162:           end
163:         end
164:         array.replace(merged)
165:       end
method_missing(method, *args, &block) click to toggle source

Catches all the possible calls and stores them

    # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 52
52:       def method_missing(method, *args, &block)
53:         scope = scope_name(method)
54:         with_scope(scope) do
55:           store_definition args, &block
56:         end
57:       end
scope_name(name) click to toggle source

Homogenizes a scope name to a pluralized symbol

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 110
110:       def scope_name name
111:         name.to_s.pluralize.to_sym
112:       end
store_definition(args, &block) click to toggle source

Stores an invocation definition

    # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 60
60:       def store_definition args, &block
61:         base = current_base
62:         options = args.extract_options!
63:         args.inject(base) do |acc, element|
64:           container = {element => []}
65:           container[element] << {:options => options} unless options.empty?
66:           (acc << container).tap do
67:             with_scope(element, &block) if block_given?
68:           end
69:         end
70:       end
with_scope(scope) {|ensure @@scopes.pop| ...} click to toggle source

Sets the current context during a declaration

    # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 35
35:       def with_scope(scope)
36:         (@@scopes ||= []) << scope
37:         yield ensure @@scopes.pop
38:       end
yield_inside(&block) click to toggle source

Yields a block with this module binding

    # File vendor/plugins/ubiquo_design/lib/ubiquo_design/structure.rb, line 47
47:       def yield_inside(&block)
48:         block.bind(self).call
49:       end

Disabled; run with $DEBUG to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.