Sha256: bb215fcb161e162c83b8e72919101ee78268f06051ccabc0edc68aa763300ce3

Contents?: true

Size: 1.15 KB

Versions: 10

Compression:

Stored size: 1.15 KB

Contents

module Lolita
	module Menu
		module NestedTree
			class BranchBuilder

				ROOT = "root"
				NONE = "none"

				def initialize(root, attributes)
					@root = root
					@attributes = {}
					(attributes || {}).each do |key,value|
						@attributes[key.to_sym] = value
					end
				end

				def root?
					@attributes[:item_id] == "root"
				end

				def value_for(attribute)
					convert(@attributes[attribute.to_sym])
				end

				def attribute_value_pairs
					@attributes.map do |attr, value|
						[mapping[attr], convert(value)]
					end
				end

				def attribute_value_pairs_hash
					hash = {}
					attribute_value_pairs.each do |pair|
						next if pair[0] == :item_id
						hash[pair[0]] = pair[1]
					end
					hash
				end

				private

				def mapping
					unless @mapping
						@mapping = {
							:left => :lft,
							:right => :rgt
						}
						class << @mapping
							def [](key)
								self.keys.include?(key) ? self.fetch(key) : key
							end
						end
					end
					@mapping
				end

				def convert(value)
					if value == ROOT
						@root.id
					elsif value == NONE || !value 
						nil
					else 
						value.to_i
					end
				end

			end
		end
	end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lolita-menu-0.3.6 lib/lolita-menu/nested_tree/branch_builder.rb
lolita-menu-0.3.5 lib/lolita-menu/nested_tree/branch_builder.rb
lolita-menu-0.3.4 lib/lolita-menu/nested_tree/branch_builder.rb
lolita-menu-0.3.2 lib/lolita-menu/nested_tree/branch_builder.rb
lolita-menu-0.3.1 lib/lolita-menu/nested_tree/branch_builder.rb
lolita-menu-0.3.0 lib/lolita-menu/nested_tree/branch_builder.rb
lolita-menu-0.2.2 lib/lolita-menu/nested_tree/branch_builder.rb
lolita-menu-0.2.1 lib/lolita-menu/nested_tree/branch_builder.rb
lolita-menu-0.2.0 lib/lolita-menu/nested_tree/branch_builder.rb
lolita-menu-0.1.7 lib/lolita-menu/nested_tree/branch_builder.rb