Class: Docks::Containers::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/docks/containers/base_container.rb

Overview

Public: the base container for symbols. This class should be inherited from for all other symbol containers. Its most important feature is that it normalizes synonymous tags so that any tag can be called on a container and the result will be returned as expected.

Direct Known Subclasses

Pattern, Symbol

Instance Method Summary collapse

Constructor Details

#initialize(details = {}) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
22
23
24
25
26
# File 'lib/docks/containers/base_container.rb', line 17

def initialize(details = {})
  if details.kind_of?(Base)
    details.delete(:symbol_type)
    details = details.to_h
  end

  details = Tags.join_synonymous_tags(details)
  @details = details
  @summary = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/docks/containers/base_container.rb', line 61

def method_missing(meth, *args, &block)
  stripped = meth.to_s.sub("=", "").to_sym
  has_tag = Tags.has_tag?(stripped)

  if stripped != meth && has_tag
    self[stripped] = args.first
  elsif has_tag
    fetch(meth, nil)
  else
    super(meth, *args, &block)
  end
end

Instance Method Details

#==(other_container) ⇒ Object



31
32
33
# File 'lib/docks/containers/base_container.rb', line 31

def ==(other_container)
  self.class == other_container.class && @details == other_container.instance_variable_get(:@details)
end

#[](tag) ⇒ Object



40
41
42
# File 'lib/docks/containers/base_container.rb', line 40

def [](tag)
  fetch(tag, nil)
end

#[]=(tag, new_value) ⇒ Object



44
45
46
47
# File 'lib/docks/containers/base_container.rb', line 44

def []=(tag, new_value)
  tag = Tags.base_tag_name(tag)
  @details[tag] = new_value unless tag.nil?
end

#delete(tag) ⇒ Object



49
50
51
# File 'lib/docks/containers/base_container.rb', line 49

def delete(tag)
  @details.delete(Tags.base_tag_name(tag))
end

#fetch(tag, *args) ⇒ Object



53
54
55
# File 'lib/docks/containers/base_container.rb', line 53

def fetch(tag, *args)
  @details.fetch(Tags.base_tag_name(tag), *args)
end

#find(descriptor) ⇒ Object



87
88
89
90
# File 'lib/docks/containers/base_container.rb', line 87

def find(descriptor)
  descriptor = Descriptor.new(descriptor)
  matches_exactly?(descriptor) && self
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/docks/containers/base_container.rb', line 74

def respond_to?(meth)
  Tags.has_tag?(meth.to_s.sub("=", "")) || super
end

#summarized?Boolean Also known as: summary?

Returns:

  • (Boolean)


78
# File 'lib/docks/containers/base_container.rb', line 78

def summarized?; @summary end

#summaryObject



81
82
83
84
85
# File 'lib/docks/containers/base_container.rb', line 81

def summary
  summary = self.class.new(name: self.name)
  summary.instance_variable_set(:@summary, true)
  summary
end

#tagsObject



57
58
59
# File 'lib/docks/containers/base_container.rb', line 57

def tags
  @details.keys.map { |tag| Tags.tag_for(tag) }
end

#to_hObject Also known as: to_hash



28
# File 'lib/docks/containers/base_container.rb', line 28

def to_h; @details end

#update(tag) ⇒ Object



35
36
37
38
# File 'lib/docks/containers/base_container.rb', line 35

def update(tag)
  self[tag] = yield(self[tag])
  self
end