class Giter8::Pairs

Pairs represents a set of property pairs

Public Class Methods

new(map = {}) click to toggle source

Creates a new Pairs instance, optionally with a given map.

# File lib/giter8/pairs.rb, line 7
def initialize(map = {})
  @pairs = map.map do |e|
    if e.is_a? Pair
      e
    else
      Pair.new(*e)
    end
  end
end

Public Instance Methods

each(&block) click to toggle source

When a block is provided, invokes the block once for each Pair included in this set. Otherwise, returns an Enumerator for this instance.

# File lib/giter8/pairs.rb, line 36
def each(&block)
  @pairs.each(&block)
end
each_pair() { |key, value| ... } click to toggle source

Invokes a given block once for each element in this set, providing the element's key and value as arguments, respectively.

# File lib/giter8/pairs.rb, line 42
def each_pair
  each do |p|
    yield p.key, p.value
  end
end
fetch(name, default = nil) click to toggle source

Returns the value associated with a given name, or an optional default argument. In case no default is provided, nil is returned.

# File lib/giter8/pairs.rb, line 30
def fetch(name, default = nil)
  find(name) || default
end
find(name) click to toggle source

Attempts to find a Pair instance with a given name among all propertines in the current set. Returns a Pair object, or nil, if the provided name does not match any Pair.

# File lib/giter8/pairs.rb, line 21
def find(name)
  v = find_pair(name.to_sym)
  return nil if v.nil?

  v.value
end
find_pair(name) click to toggle source

Returns a Pair value with a given name, or nil, in case no Pair matches the provided name.

# File lib/giter8/pairs.rb, line 50
def find_pair(name)
  @pairs.find { |e| e.key == name.to_sym }
end
key?(name) click to toggle source

Returns whether a Pair with the provided name exists in the set

# File lib/giter8/pairs.rb, line 60
def key?(name)
  !find(name).nil?
end
merge(pairs) click to toggle source

Merges a provided Hash or Pairs instance into the current set

# File lib/giter8/pairs.rb, line 65
def merge(pairs)
  pairs = Pairs.new(pairs) if pairs.is_a? Hash

  pairs.each_pair do |k, v|
    pair = find_pair(k)
    if pair.nil?
      @pairs << Pair.new(k, v)
    else
      idx = @pairs.index(pair)
      pair.value = v
      @pairs[idx] = pair
    end
  end
end
to_h() click to toggle source

Returns the current props represented as a Hash

# File lib/giter8/pairs.rb, line 55
def to_h
  @pairs.map { |e| [e.key, e.value] }.to_h
end