Class: Scrawl

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/scrawl.rb,
lib/scrawl/version.rb

Constant Summary collapse

KEY_VALUE_DELIMITER =
"="
PAIR_DELIMITER =
" "
NAMESPACE_DELIMITER =
"."
DEFAULT_SPLAT_CHARACTER =
"*"
VERSION =
"3.0.0"

Instance Method Summary collapse

Constructor Details

#initialize(*trees) ⇒ Scrawl

Returns a new instance of Scrawl.



9
10
11
# File 'lib/scrawl.rb', line 9

def initialize(*trees)
  @tree = trees.inject({}) { |global, tree| global.merge(tree) }
end

Instance Method Details

#each(&block) ⇒ Object



33
34
35
# File 'lib/scrawl.rb', line 33

def each(&block)
  tree.each(&block)
end

#inspect(namespace = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/scrawl.rb', line 17

def inspect(namespace = nil)
  tree.map do |key, value|
    case
      when value.nil? then nil
      when value.respond_to?(:none?) && value.none? then nil
      when value.respond_to?(:push) then itemize(namespace, key, value)
      when value.respond_to?(:merge) then Scrawl.new(value).inspect(key)
      else label(namespace, key) + KEY_VALUE_DELIMITER + element(value)
    end
  end.flatten.compact.join(PAIR_DELIMITER)
end

#merge(hash) ⇒ Object



13
14
15
# File 'lib/scrawl.rb', line 13

def merge(hash)
  @tree.merge!(hash.to_hash)
end

#to_hObject



41
42
43
# File 'lib/scrawl.rb', line 41

def to_h
  tree.to_h
end

#to_hashObject



37
38
39
# File 'lib/scrawl.rb', line 37

def to_hash
  tree.to_hash
end

#to_s(namespace = nil) ⇒ Object



29
30
31
# File 'lib/scrawl.rb', line 29

def to_s(namespace = nil)
  inspect(namespace)
end