Class: IPScriptables::Chain
- Inherits:
-
Object
- Object
- IPScriptables::Chain
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/ipscriptables/chain.rb,
lib/ipscriptables/pretty_print.rb
Instance Attribute Summary collapse
-
#counters ⇒ Object
readonly
Returns the value of attribute counters.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#policy ⇒ Object
Returns the value of attribute policy.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #alter(policy = nil, counters = nil, &block) ⇒ Object
-
#initialize(name, table, policy = '-', counters = [0, 0], &block) ⇒ Chain
constructor
A new instance of Chain.
- #inspect ⇒ Object
- #original ⇒ Object
- #pretty_print(q) ⇒ Object
- #render_header ⇒ Object
- #render_rules ⇒ Object
- #rule(term, *rest, &block) ⇒ Object
Constructor Details
#initialize(name, table, policy = '-', counters = [0, 0], &block) ⇒ Chain
Returns a new instance of Chain.
12 13 14 15 16 17 18 19 20 |
# File 'lib/ipscriptables/chain.rb', line 12 def initialize(name, table, policy = '-', counters = [0, 0], &block) @name = name @table = table @policy = policy @counters = counters @rules = [] @rule_stack = [] Docile.dsl_eval(self, &block) if block_given? end |
Instance Attribute Details
#counters ⇒ Object (readonly)
Returns the value of attribute counters.
7 8 9 |
# File 'lib/ipscriptables/chain.rb', line 7 def counters @counters end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/ipscriptables/chain.rb', line 7 def name @name end |
#policy ⇒ Object
Returns the value of attribute policy.
8 9 10 |
# File 'lib/ipscriptables/chain.rb', line 8 def policy @policy end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
7 8 9 |
# File 'lib/ipscriptables/chain.rb', line 7 def rules @rules end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
7 8 9 |
# File 'lib/ipscriptables/chain.rb', line 7 def table @table end |
Instance Method Details
#alter(policy = nil, counters = nil, &block) ⇒ Object
26 27 28 29 30 |
# File 'lib/ipscriptables/chain.rb', line 26 def alter(policy = nil, counters = nil, &block) @policy = policy unless policy.nil? @counters = counters unless counters.nil? Docile.dsl_eval(self, &block) if block_given? end |
#inspect ⇒ Object
35 36 37 |
# File 'lib/ipscriptables/pretty_print.rb', line 35 def inspect "#<#{self.class} #{name} [#{map(&:inspect).join(', ')}]>" end |
#original ⇒ Object
22 23 24 |
# File 'lib/ipscriptables/chain.rb', line 22 def original table.original[name] if table.original end |
#pretty_print(q) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/ipscriptables/pretty_print.rb', line 39 def pretty_print(q) q.group(2, "#{render_header} {", '}') do unless rules.empty? q.breakable q.seplist(rules, -> { q.breakable ' ; ' }) { |v| q.pp(v) } end end end |
#render_header ⇒ Object
75 76 77 |
# File 'lib/ipscriptables/chain.rb', line 75 def render_header ":#{name} #{policy} [#{counters.join(':')}]" end |
#render_rules ⇒ Object
79 80 81 |
# File 'lib/ipscriptables/chain.rb', line 79 def render_rules rules.map(&:render).join("\n") unless rules.empty? end |
#rule(term, *rest, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ipscriptables/chain.rb', line 32 def rule(term, *rest, &block) # rubocop:disable CyclomaticComplexity, MethodLength, LineLength # FIXME: ^^ case term when Rule @rules << term # we trust here that term.chain is self when Hash # Explode hash into [switch, value, switch, value, ...] sequence exploded = [] term.each do |key, val| if key.is_a? Symbol key = key.to_s if key.length == 1 exploded << "-#{key}" else exploded << "--#{key.gsub('_', '-')}" end else exploded << key.to_s end exploded << val end exploded.concat(rest) rule(*exploded, &block) when Enumerable term.each do |term1| rule(term1, *rest, &block) end else begin @rule_stack << term if !rest.empty? rule(*rest, &block) elsif block_given? yield else @rules << Rule.new(self, @rule_stack.map(&:to_s).join(' ')) end ensure @rule_stack.pop end end end |