Class: Origen::OrgFile

Inherits:
Object show all
Defined in:
lib/origen/org_file.rb,
lib/origen/org_file/interceptor.rb,
lib/origen/org_file/interceptable.rb

Defined Under Namespace

Modules: Interceptable Classes: Interceptor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) ⇒ OrgFile

Returns a new instance of OrgFile.



56
57
58
59
60
61
62
# File 'lib/origen/org_file.rb', line 56

def initialize(id, options = {})
  @id = id
  @path = options[:path] || default_filepath
  @filename = options[:filename] || "#{id}.org"
  FileUtils.mkdir_p(path)
  @path_to_file = File.join(path, filename)
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



53
54
55
# File 'lib/origen/org_file.rb', line 53

def filename
  @filename
end

#idObject (readonly)

Returns the value of attribute id.



54
55
56
# File 'lib/origen/org_file.rb', line 54

def id
  @id
end

#operationObject (readonly)

Returns the value of attribute operation.



54
55
56
# File 'lib/origen/org_file.rb', line 54

def operation
  @operation
end

#pathObject

Returns the value of attribute path.



53
54
55
# File 'lib/origen/org_file.rb', line 53

def path
  @path
end

Class Method Details

._block_given_?(&block) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper for the Interceptor where block_given? doesn't work internally

Returns:

  • (Boolean)


7
8
9
# File 'lib/origen/org_file/interceptor.rb', line 7

def self._block_given_?(&block)
  block_given?
end

.close(id, options = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/origen/org_file.rb', line 31

def close(id, options = {})
  fail "An org_file with this ID has not been opened id: #{id}" unless open_files[id]

  open_files[id].close unless options[:_internal_org_file_call_]
  open_files.delete(id)
end

.cycle(number = 1) ⇒ Object



42
43
44
# File 'lib/origen/org_file.rb', line 42

def cycle(number = 1)
  open_files.each { |id, f| f.cycle(number) }
end

.new(*args, &block) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/origen/org_file.rb', line 7

def new(*args, &block)
  if @internal_new
    super
  else
    # Kernel#open is a serious security risk
    open(*args, &block)   # rubocop:disable Security/Open
  end
end

.open(id, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/origen/org_file.rb', line 16

def open(id, options = {})
  fail "An org_file is already open with id: #{id}" if open_files[id]

  @internal_new = true
  f = OrgFile.new(id, options)
  @internal_new = nil
  open_files[id] = f
  if block_given?
    yield f
    close(id, options)
  else
    f
  end
end

.org_file(id = nil) ⇒ Object



38
39
40
# File 'lib/origen/org_file.rb', line 38

def org_file(id = nil)
  id ? open_files[id] : open_files.to_a.last[1]
end

Instance Method Details

#closeObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/origen/org_file.rb', line 81

def close
  # Corner case, if no call to read_line or record was made then no file was created and there
  # is no file to close
  if @file
    file.puts "#{@buffer}#{@buffer_cycles}" if @buffer
    file.close
  end
  self.class.close(id, _internal_org_file_call_: true)
  nil
end

#cycle(number) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/origen/org_file.rb', line 112

def cycle(number)
  if @buffer
    if @line == @buffer
      @buffer_cycles += number
    else
      file.puts "#{@buffer}#{@buffer_cycles}"
      @buffer = @line
      @buffer_cycles = number
    end
  else
    @buffer = @line
    @buffer_cycles = number
  end
  @line = nil
end

#default_filepathObject



64
65
66
# File 'lib/origen/org_file.rb', line 64

def default_filepath
  "#{Origen.root}/pattern/org/#{Origen.target.name}"
end

#exist?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/origen/org_file.rb', line 77

def exist?
  File.exist?(@path_to_file)
end

#fileObject



68
69
70
71
72
73
74
75
# File 'lib/origen/org_file.rb', line 68

def file
  @file ||= begin
    if operation == 'r'
      fail "No org file found at: #{@path_to_file}" unless exist?
    end
    File.open(@path_to_file, operation)
  end
end

#read_lineObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/origen/org_file.rb', line 92

def read_line
  @operation ||= 'r'
  operations = file.readline.strip.split(';')
  cycles = operations.pop.to_i
  operations = operations.map { |op| op.split(',') }.map { |op| op[0] = eval(op[0]); op }
  if block_given?
    yield operations, cycles
  else
    [operations, cycles]
  end
end

#record(path_to_object, method, *args) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/origen/org_file.rb', line 104

def record(path_to_object, method, *args)
  @operation ||= 'w'
  @line ||= ''
  @line += "#{path_to_object},#{method}"
  @line += ",#{args.join(',')}" unless args.empty?
  @line += ';'
end