Class: Snap7::DataBlockRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-snap7/data_structures/data_block_registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDataBlockRegistry

Returns a new instance of DataBlockRegistry.



6
7
8
# File 'lib/ffi-snap7/data_structures/data_block_registry.rb', line 6

def initialize
  @dbs = {}
end

Instance Attribute Details

#dbsObject (readonly)

Returns the value of attribute dbs.



4
5
6
# File 'lib/ffi-snap7/data_structures/data_block_registry.rb', line 4

def dbs
  @dbs
end

Instance Method Details

#add_variable(ident, address, data_type) ⇒ Object



11
12
13
14
15
# File 'lib/ffi-snap7/data_structures/data_block_registry.rb', line 11

def add_variable(ident, address, data_type)
  db_addr, _, var_addr = address.partition('.')
  var = Snap7::Variable.new ident, var_addr, data_type
  db(db_addr).add_variable var
end

#db(db_addr) ⇒ Object



28
29
30
# File 'lib/ffi-snap7/data_structures/data_block_registry.rb', line 28

def db(db_addr)
  @dbs[db_addr] ||= Snap7::DataBlock.new(db_addr)
end

#each(&block) ⇒ Object



33
34
35
# File 'lib/ffi-snap7/data_structures/data_block_registry.rb', line 33

def each(&block)
  sorted_dbs.each &block
end

#find_variable(ident) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/ffi-snap7/data_structures/data_block_registry.rb', line 18

def find_variable(ident)
  each do |db|
    found_variable = db.variables.find { |var| var.ident == ident }
    return found_variable if found_variable
  end

  return nil
end

#sorted_dbsObject



38
39
40
# File 'lib/ffi-snap7/data_structures/data_block_registry.rb', line 38

def sorted_dbs
  @dbs.values.sort { |a,b| a.number <=> b.number }
end