Class: Bcome::Node::Resources::Base
- Inherits:
-
Object
- Object
- Bcome::Node::Resources::Base
- Includes:
- Enumerable
- Defined in:
- lib/objects/node/resources/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
- #<<(node) ⇒ Object
- #active ⇒ Object
- #clear! ⇒ Object
- #disable(identifier) ⇒ Object
- #disable! ⇒ Object
- #do_disable(identifier) ⇒ Object
- #do_enable(identifier) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #enable(identifier) ⇒ Object
- #enable! ⇒ Object
- #first ⇒ Object
- #for_identifier(identifier) ⇒ Object
- #has_active_nodes? ⇒ Boolean
-
#initialize(*_params) ⇒ Base
constructor
A new instance of Base.
- #is_active_resource?(resource) ⇒ Boolean
- #size ⇒ Object
Constructor Details
#initialize(*_params) ⇒ Base
Returns a new instance of Base.
7 8 9 10 |
# File 'lib/objects/node/resources/base.rb', line 7 def initialize(*_params) @nodes = [] @disabled_resources = [] end |
Instance Attribute Details
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
5 6 7 |
# File 'lib/objects/node/resources/base.rb', line 5 def nodes @nodes end |
Instance Method Details
#<<(node) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/objects/node/resources/base.rb', line 16 def <<(node) existing_node = for_identifier(node.identifier) if existing_node = "#{node.identifier} is not unique within namespace #{node.parent.namespace}" raise Bcome::Exception::NodeIdentifiersMustBeUnique, end @nodes << node end |
#active ⇒ Object
69 70 71 |
# File 'lib/objects/node/resources/base.rb', line 69 def active @nodes - @disabled_resources end |
#clear! ⇒ Object
25 26 27 |
# File 'lib/objects/node/resources/base.rb', line 25 def clear! @disabled_resources = [] end |
#disable(identifier) ⇒ Object
52 53 54 55 56 |
# File 'lib/objects/node/resources/base.rb', line 52 def disable(identifier) resource = for_identifier(identifier) raise Bcome::Exception::NoNodeNamedByIdentifier, identifier unless resource @disabled_resources << resource unless @disabled_resources.include?(resource) end |
#disable! ⇒ Object
48 49 50 |
# File 'lib/objects/node/resources/base.rb', line 48 def disable! @disabled_resources = @nodes end |
#do_disable(identifier) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/objects/node/resources/base.rb', line 30 def do_disable(identifier) if identifier.is_a?(Array) identifier.each { |id| disable(id) } else disable(identifier) end nil end |
#do_enable(identifier) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/objects/node/resources/base.rb', line 39 def do_enable(identifier) if identifier.is_a?(Array) identifier.each { |id| enable(id) } else enable(identifier) end nil end |
#each ⇒ Object
12 13 14 |
# File 'lib/objects/node/resources/base.rb', line 12 def each @nodes.each { |node| yield(node) } end |
#empty? ⇒ Boolean
82 83 84 |
# File 'lib/objects/node/resources/base.rb', line 82 def empty? @nodes.empty? end |
#enable(identifier) ⇒ Object
58 59 60 61 62 |
# File 'lib/objects/node/resources/base.rb', line 58 def enable(identifier) resource = for_identifier(identifier) raise Bcome::Exception::NoNodeNamedByIdentifier, identifier unless resource @disabled_resources -= [resource] end |
#enable! ⇒ Object
28 29 30 |
# File 'lib/objects/node/resources/base.rb', line 28 def clear! @disabled_resources = [] end |
#first ⇒ Object
94 95 96 |
# File 'lib/objects/node/resources/base.rb', line 94 def first @nodes.first end |
#for_identifier(identifier) ⇒ Object
77 78 79 80 |
# File 'lib/objects/node/resources/base.rb', line 77 def for_identifier(identifier) resource = @nodes.select { |node| node.identifier == identifier }.first resource end |
#has_active_nodes? ⇒ Boolean
86 87 88 |
# File 'lib/objects/node/resources/base.rb', line 86 def has_active_nodes? active.any? end |
#is_active_resource?(resource) ⇒ Boolean
73 74 75 |
# File 'lib/objects/node/resources/base.rb', line 73 def is_active_resource?(resource) active.include?(resource) end |
#size ⇒ Object
90 91 92 |
# File 'lib/objects/node/resources/base.rb', line 90 def size @nodes.size end |