Class: IOSParser::IOS::Document

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Queryable
Defined in:
lib/ios_parser/ios/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Queryable

#_find, #_find_all, #find, #find_all, #match_expr

Constructor Details

#initialize(source) ⇒ Document

Returns a new instance of Document.



12
13
14
15
16
# File 'lib/ios_parser/ios/document.rb', line 12

def initialize(source)
  @commands = []
  @parent = nil
  @source = source
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



10
11
12
# File 'lib/ios_parser/ios/document.rb', line 10

def commands
  @commands
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/ios_parser/ios/document.rb', line 10

def parent
  @parent
end

#sourceObject

Returns the value of attribute source.



10
11
12
# File 'lib/ios_parser/ios/document.rb', line 10

def source
  @source
end

Class Method Details

.from_hash(hash) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ios_parser/ios/document.rb', line 40

def from_hash(hash)
  hash[:parent] = parent
  [:commands, :source].each do |key|
    val = hash.delete(key.to_s)
    hash[key] = val unless hash.key?(key)
  end

  new(source).tap do |doc|
    doc.push(*(hash[:commands].map { |c| Command.from_hash(c) }))
  end
end

Instance Method Details

#eachObject



22
23
24
# File 'lib/ios_parser/ios/document.rb', line 22

def each
  commands.each { |command| command.each { |cmd| yield cmd } }
end

#to_hashObject



31
32
33
# File 'lib/ios_parser/ios/document.rb', line 31

def to_hash
  { commands: commands.map(&:to_hash) }
end

#to_jsonObject



35
36
37
# File 'lib/ios_parser/ios/document.rb', line 35

def to_json
  JSON.dump(to_hash)
end

#to_s(dedent: false) ⇒ Object



26
27
28
29
# File 'lib/ios_parser/ios/document.rb', line 26

def to_s(dedent: false)
  base = dedent ? indentation : 0
  map { |cmd| "#{cmd.indentation(base: base)}#{cmd.line}\n" }.join
end