Class: Scrawl

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

Constant Summary collapse

KEY_VALUE_DELIMITER =
"="
PAIR_DELIMITER =
" "
NAMESPACE_DELIMITER =
"."
VERSION =
"2.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*trees) ⇒ Scrawl

Returns a new instance of Scrawl.



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

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

Instance Attribute Details

#treeObject (readonly)

Returns the value of attribute tree.



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

def tree
  @tree
end

Instance Method Details

#inspect(namespace = nil) ⇒ Object



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

def inspect(namespace = nil)
  @tree.map do |key, value|
    unless value.respond_to?(:to_hash)
      label(namespace, key) + KEY_VALUE_DELIMITER + element(value)
    else
      Scrawl.new(value).inspect(key)
    end
  end.join(PAIR_DELIMITER)
end

#merge(hash) ⇒ Object



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

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

#to_hObject



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

def to_h
  tree.to_h
end

#to_hashObject



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

def to_hash
  tree.to_hash
end

#to_s(namespace = nil) ⇒ Object



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

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