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



53
54
55
56
57
58
59
# File 'lib/origen/org_file.rb', line 53

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



50
51
52
# File 'lib/origen/org_file.rb', line 50

def filename
  @filename
end

#idObject (readonly)

Returns the value of attribute id



51
52
53
# File 'lib/origen/org_file.rb', line 51

def id
  @id
end

#operationObject (readonly)

Returns the value of attribute operation



51
52
53
# File 'lib/origen/org_file.rb', line 51

def operation
  @operation
end

#pathObject

Returns the value of attribute path



50
51
52
# File 'lib/origen/org_file.rb', line 50

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



29
30
31
32
33
# File 'lib/origen/org_file.rb', line 29

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



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

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

.new(*args, &block) ⇒ Object



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

def new(*args, &block)
  if @internal_new
    super
  else
    open(*args, &block)
  end
end

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



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

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



35
36
37
# File 'lib/origen/org_file.rb', line 35

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

Instance Method Details

#closeObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/origen/org_file.rb', line 78

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/origen/org_file.rb', line 109

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



61
62
63
# File 'lib/origen/org_file.rb', line 61

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

#exist?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/origen/org_file.rb', line 74

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

#fileObject



65
66
67
68
69
70
71
72
# File 'lib/origen/org_file.rb', line 65

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



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/origen/org_file.rb', line 89

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



101
102
103
104
105
106
107
# File 'lib/origen/org_file.rb', line 101

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