class Fib::ElementPackage

Attributes

actions[R]
keys[R]
mutex[R]
origin_elements[R]
urls[R]

Public Class Methods

new(elements=[]) click to toggle source
# File lib/fib/element_package.rb, line 12
def initialize elements=[]

  @keys = {}
  @actions = {}
  @urls = Fib::Trie.new('.', nil)
  @origin_elements = elements.group_by{ |e| e.type }

  rebuild
end

Private Class Methods

merge(*packages) click to toggle source
# File lib/fib/element_package.rb, line 118
def merge *packages
  new (packages.reduce([]) { |a, e| a + e.origin_elements.values.flatten }).uniq
end

Public Instance Methods

+(package) click to toggle source
# File lib/fib/element_package.rb, line 42
def + package
  self.class.new (origin_elements.values.flatten + package.origin_elements.values.flatten).uniq
end
<<(element)
Alias for: set
append(*elements)
Alias for: mset
build() click to toggle source
# File lib/fib/element_package.rb, line 61
def build
  build_keys
  build_actions
  build_urls
end
find_action(controller, action) click to toggle source
# File lib/fib/element_package.rb, line 51
def find_action controller, action
  lazy_build
  actions&.dig(controller.to_s, action.to_s)
end
find_key(k) click to toggle source
# File lib/fib/element_package.rb, line 46
def find_key k
  lazy_build
  keys[k.to_sym]
end
find_url(url) click to toggle source
# File lib/fib/element_package.rb, line 56
def find_url url
  lazy_build
  urls&.dig(*url.gsub(/^\/|\/$/, "").split(/\//))
end
finish_build() click to toggle source
# File lib/fib/element_package.rb, line 74
def finish_build
  @mutex = false
end
lazy_build() click to toggle source
# File lib/fib/element_package.rb, line 67
def lazy_build
  return if !mutex

  build
  finish_build
end
mset(*elements) click to toggle source
# File lib/fib/element_package.rb, line 33
def mset *elements
  elements.flatten.each do |e|
    next unless e.is_a?(Fib::Element)
    set e
  end
end
Also aliased as: append
rebuild() click to toggle source
# File lib/fib/element_package.rb, line 78
def rebuild
  @mutex = true
end
set(element) click to toggle source
# File lib/fib/element_package.rb, line 22
def set element
  raise ParameterIsNotValid, "param must be Element" unless element.is_a?(Fib::Element)

  origin_elements[element.type] ||= []
  origin_elements[element.type] |= [element]

  rebuild
end
Also aliased as: <<

Private Instance Methods

build_actions() click to toggle source
# File lib/fib/element_package.rb, line 90
def build_actions
  return unless origin_elements.has_key? 'action'

  origin_elements['action'].each do |e|
    next unless e.core.is_a? Hash

    key = e.core[:controller].to_s
    actions[key] ||= {}
    actions[key][e.core[:action].to_s] = e
  end
end
build_keys() click to toggle source
# File lib/fib/element_package.rb, line 84
def build_keys
  return unless origin_elements.has_key? 'key'

  origin_elements['key'].each { |e| keys[e.core] = e }
end
build_urls() click to toggle source
# File lib/fib/element_package.rb, line 102
def build_urls
  return unless origin_elements.has_key? 'url'

  origin_elements['url'].each do |e|
    next unless e.core.is_a? String

    # 过滤首尾 / 并以 / 分割
    split_url = e.core.gsub(/^\/|\/$/, "").split(/\//)
    split_url.each_with_index.reduce(urls) do |i, (j, index)|
      t = i.add_sub j, (index == split_url.size - 1 ? e : nil)
      t
    end
  end
end