Class: IOSParser::IOS::Command

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Queryable
Defined in:
lib/ios_parser/ios/command.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(tokens: [], commands: [], parent: nil, document: nil, indent: nil) ⇒ Command

Returns a new instance of Command.



10
11
12
13
14
15
16
17
# File 'lib/ios_parser/ios/command.rb', line 10

def initialize(tokens: [], commands: [],
               parent: nil, document: nil, indent: nil)
  @tokens = tokens
  @commands = commands
  @parent = parent
  @document = document
  @indent = indent || 0
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



9
10
11
# File 'lib/ios_parser/ios/command.rb', line 9

def commands
  @commands
end

#documentObject

Returns the value of attribute document.



9
10
11
# File 'lib/ios_parser/ios/command.rb', line 9

def document
  @document
end

#indentObject

Returns the value of attribute indent.



9
10
11
# File 'lib/ios_parser/ios/command.rb', line 9

def indent
  @indent
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/ios_parser/ios/command.rb', line 9

def parent
  @parent
end

#tokensObject

Returns the value of attribute tokens.



9
10
11
# File 'lib/ios_parser/ios/command.rb', line 9

def tokens
  @tokens
end

Class Method Details

.from_hash(hash, parent = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ios_parser/ios/command.rb', line 84

def from_hash(hash, parent = nil)
  hash[:parent] = parent
  [:args, :commands, :pos].each do |key|
    val = hash.delete(key.to_s)
    hash[key] ||= val
  end

  hash[:commands] ||= []
  hash[:commands].each_index do |i|
    hash[:commands][i] = from_hash(hash[:commands][i])
  end
  new(**hash)
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  args == other.args && commands == other.commands
end

#argsObject



19
20
21
# File 'lib/ios_parser/ios/command.rb', line 19

def args
  tokens.map(&:value)
end

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



51
52
53
54
# File 'lib/ios_parser/ios/command.rb', line 51

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  self == other && self.class == other.class
end

#indentation(base: 0) ⇒ Object



47
48
49
# File 'lib/ios_parser/ios/command.rb', line 47

def indentation(base: 0)
  ' ' * (path.length - base)
end

#inspectObject



56
57
58
59
60
61
62
63
# File 'lib/ios_parser/ios/command.rb', line 56

def inspect
  "<IOSParser::IOS::Command:0x#{object_id.to_s(16)} "\
  "@tokens=#{tokens.inspect}, "\
  "@commands=#{commands.inspect}, "\
  "@pos=#{pos.inspect}, "\
  "@indent=#{indent}, "\
  "@document=<IOSParser::IOS::Document:0x#{document.object_id.to_s(16)}>>"
end

#lineObject



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

def line
  args.join(' ')
end

#nameObject



23
24
25
# File 'lib/ios_parser/ios/command.rb', line 23

def name
  (tokens.first) && tokens.first.value
end

#pathObject



39
40
41
# File 'lib/ios_parser/ios/command.rb', line 39

def path
  parent ? parent.path + [parent.line] : []
end

#posObject



43
44
45
# File 'lib/ios_parser/ios/command.rb', line 43

def pos
  tokens.first && tokens.first.pos
end

#to_hashObject



70
71
72
73
74
75
76
77
# File 'lib/ios_parser/ios/command.rb', line 70

def to_hash
  {
    args: args,
    commands: commands.map(&:to_hash),
    pos: pos,
    indent: indent
  }
end

#to_jsonObject



79
80
81
# File 'lib/ios_parser/ios/command.rb', line 79

def to_json
  JSON.dump(to_hash)
end

#to_s(dedent: false) ⇒ Object



65
66
67
68
# File 'lib/ios_parser/ios/command.rb', line 65

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