Class: Docks::Containers::PatternLibrary
- Inherits:
-
Object
- Object
- Docks::Containers::PatternLibrary
- Defined in:
- lib/docks/containers/pattern_library_container.rb
Instance Attribute Summary collapse
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Instance Method Summary collapse
- #<<(pattern) ⇒ Object
- #[](pattern_name) ⇒ Object
- #find(descriptor) ⇒ Object
- #group_by(grouper, &block) ⇒ Object
- #groups(&block) ⇒ Object
- #has_pattern?(pattern_name) ⇒ Boolean
-
#initialize ⇒ PatternLibrary
constructor
A new instance of PatternLibrary.
- #summarized? ⇒ Boolean (also: #summary?)
- #summary ⇒ Object (also: #summarize)
- #summary! ⇒ Object (also: #summarize!)
- #to_json ⇒ Object
Constructor Details
#initialize ⇒ PatternLibrary
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
#patterns ⇒ Object (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
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?
57 |
# File 'lib/docks/containers/pattern_library_container.rb', line 57 def summarized?; @summary end |
#summary ⇒ Object 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_json ⇒ Object
23 |
# File 'lib/docks/containers/pattern_library_container.rb', line 23 def to_json; @patterns.to_json end |