Class: DiffResource::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/diff_resource/parser/parser.rb

Direct Known Subclasses

JsonParser, XmlParser, YamlParser

Constant Summary collapse

@@resource =
Struct.new "Resource", :key, :value

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
10
# File 'lib/diff_resource/parser/parser.rb', line 6

def initialize options = {}
	@root = options["root"] || ""
	@key = options["key"] || ""
	@value = options["value"] || ""
end

Instance Method Details

#nested_hash_access(hash, path) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/diff_resource/parser/parser.rb', line 12

def nested_hash_access hash, path
	ret = hash
	path.split(".").each do |symbol|
		ret = ret[symbol]
	end

	return ret
end

#parse(str) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/diff_resource/parser/parser.rb', line 21

def parse str
	ret = []
	begin
		obj = parse_string str
		root = nested_hash_access obj, @root

		ret = if @key == "*"
			parse_hash root
		else
			parse_array root
		end
	rescue => e
		puts e.message
	end

	return ret
end

#parse_array(root) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/diff_resource/parser/parser.rb', line 39

def parse_array root
	ret = []
	root.each do |item|
		next unless parsre_target? item
		key = nested_hash_access item, @key
		value = nested_hash_access item, @value
		ret << @@resource.new(key, value)
	end
	return ret
end

#parse_hash(root) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/diff_resource/parser/parser.rb', line 50

def parse_hash root
	ret = []
	root.each do |key, value|
		next unless parsre_target? key
		ret << @@resource.new(key, value)
	end
	return ret
end

#parse_string(str) ⇒ Object



59
60
# File 'lib/diff_resource/parser/parser.rb', line 59

def parse_string str
end

#parsre_target?(item) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/diff_resource/parser/parser.rb', line 62

def parsre_target? item
	return true
end