Class: Fibman::ElementPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/fibman/element_package.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements = []) ⇒ ElementPackage

Returns a new instance of ElementPackage.



12
13
14
15
16
17
18
19
20
# File 'lib/fibman/element_package.rb', line 12

def initialize elements=[]

  @keys = {}
  @actions = {}
  @urls = Fibman::Trie.create
  @origin_elements = elements.group_by{ |e| e.type }

  rebuild
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



10
11
12
# File 'lib/fibman/element_package.rb', line 10

def actions
  @actions
end

#keysObject (readonly)

Returns the value of attribute keys.



10
11
12
# File 'lib/fibman/element_package.rb', line 10

def keys
  @keys
end

#mutexObject (readonly)

Returns the value of attribute mutex.



10
11
12
# File 'lib/fibman/element_package.rb', line 10

def mutex
  @mutex
end

#origin_elementsObject (readonly)

Returns the value of attribute origin_elements.



10
11
12
# File 'lib/fibman/element_package.rb', line 10

def origin_elements
  @origin_elements
end

#urlsObject (readonly)

Returns the value of attribute urls.



10
11
12
# File 'lib/fibman/element_package.rb', line 10

def urls
  @urls
end

Class Method Details

.merge(*packages) ⇒ Object



118
119
120
# File 'lib/fibman/element_package.rb', line 118

def merge *packages
  new (packages.reduce([]) { |a, e| a + e.origin_elements.values.flatten }).uniq
end

Instance Method Details

#+(package) ⇒ Object



42
43
44
# File 'lib/fibman/element_package.rb', line 42

def + package
  self.class.new (origin_elements.values.flatten + package.origin_elements.values.flatten).uniq
end

#buildObject



61
62
63
64
65
# File 'lib/fibman/element_package.rb', line 61

def build
  build_keys
  build_actions
  build_urls
end

#find_action(controller, action) ⇒ Object



51
52
53
54
# File 'lib/fibman/element_package.rb', line 51

def find_action controller, action
  lazy_build
  actions&.dig(controller.to_s, action.to_s)
end

#find_key(k) ⇒ Object



46
47
48
49
# File 'lib/fibman/element_package.rb', line 46

def find_key k
  lazy_build
  keys[k.to_sym]
end

#find_url(url) ⇒ Object



56
57
58
59
# File 'lib/fibman/element_package.rb', line 56

def find_url url
  lazy_build
  urls&.dig(*url.gsub(/^\/|\/$/, "").split(/\//))
end

#finish_buildObject



74
75
76
# File 'lib/fibman/element_package.rb', line 74

def finish_build
  @mutex = false
end

#lazy_buildObject



67
68
69
70
71
72
# File 'lib/fibman/element_package.rb', line 67

def lazy_build
  return if !mutex

  build
  finish_build
end

#mset(*elements) ⇒ Object Also known as: append



33
34
35
36
37
38
# File 'lib/fibman/element_package.rb', line 33

def mset *elements
  elements.flatten.each do |e|
    next unless e.is_a?(Fibman::Element)
    set e
  end
end

#rebuildObject



78
79
80
# File 'lib/fibman/element_package.rb', line 78

def rebuild
  @mutex = true
end

#set(element) ⇒ Object Also known as: <<



22
23
24
25
26
27
28
29
# File 'lib/fibman/element_package.rb', line 22

def set element
  raise ParameterIsNotValid, "param must be Element" unless element.is_a?(Fibman::Element)

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

  rebuild
end