Class: Docks::Containers::PatternLibrary

Inherits:
Object
  • Object
show all
Defined in:
lib/docks/containers/pattern_library_container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePatternLibrary

Returns a new instance of PatternLibrary.



6
7
8
9
# File 'lib/docks/containers/pattern_library_container.rb', line 6

def initialize
  @patterns = {}
  @summary = false
end

Instance Attribute Details

#patternsObject (readonly)

Returns the value of attribute patterns.



4
5
6
# File 'lib/docks/containers/pattern_library_container.rb', line 4

def patterns
  @patterns
end

Instance Method Details

#<<(pattern) ⇒ Object



11
12
13
# File 'lib/docks/containers/pattern_library_container.rb', line 11

def <<(pattern)
  @patterns[pattern.name.to_s] ||= pattern
end

#[](pattern_name) ⇒ Object



15
16
17
# File 'lib/docks/containers/pattern_library_container.rb', line 15

def [](pattern_name)
  @patterns[Docks.pattern_id(pattern_name)]
end

#find(descriptor) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/docks/containers/pattern_library_container.rb', line 47

def find(descriptor)
  descriptor = Descriptor.new(descriptor, assume: :pattern)
  pattern = @patterns[descriptor.pattern]
  return false if pattern.nil?

  symbol = pattern.find(descriptor)
  symbol = nil if !symbol || symbol == pattern
  ::OpenStruct.new(pattern: pattern, symbol: symbol)
end

#group_by(grouper, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/docks/containers/pattern_library_container.rb', line 25

def group_by(grouper, &block)
  grouped_patterns = {}
  grouper = grouper.to_sym

  @patterns.each do |name, pattern|
    grouper_value = pattern.send(grouper)
    next if grouper_value.nil?
    grouped_patterns[grouper_value] ||= []
    grouped_patterns[grouper_value] << pattern
  end

  if block_given?
    grouped_patterns.each(&block)
  else
    grouped_patterns
  end
end

#groups(&block) ⇒ Object



43
44
45
# File 'lib/docks/containers/pattern_library_container.rb', line 43

def groups(&block)
  group_by(:group, &block)
end

#has_pattern?(pattern_name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/docks/containers/pattern_library_container.rb', line 19

def has_pattern?(pattern_name)
  !self[pattern_name].nil?
end

#summarized?Boolean Also known as: summary?

Returns:

  • (Boolean)


57
# File 'lib/docks/containers/pattern_library_container.rb', line 57

def summarized?; @summary end

#summaryObject Also known as: summarize



60
61
62
63
64
65
66
67
# File 'lib/docks/containers/pattern_library_container.rb', line 60

def summary
  return self if summarized?

  summary = self.class.new
  summary.instance_variable_set(:@summary, true)
  summary.instance_variable_set(:@patterns, Hash[@patterns.map { |name, pattern| [name, pattern.summary] }])
  summary
end

#summary!Object Also known as: summarize!



70
71
72
73
# File 'lib/docks/containers/pattern_library_container.rb', line 70

def summary!
  @summary = true
  @patterns = Hash[@patterns.map { |name, pattern| [name, pattern.summary] }]
end

#to_jsonObject



23
# File 'lib/docks/containers/pattern_library_container.rb', line 23

def to_json; @patterns.to_json end