Class: OneTouch::RelationRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/one_touch/core/bridge.rb

Constant Summary collapse

@@recorders =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ RelationRecorder

Returns a new instance of RelationRecorder.



7
8
9
10
11
12
13
14
15
# File 'lib/one_touch/core/bridge.rb', line 7

def initialize(opts)
  # loop and set attr as bridge_klass = opts[:bridge_klass]
  opts.each do |k,v|
    if respond_to?("#{k}=")
      send "#{k}=", v
    end
  end
  @@recorders << self
end

Instance Attribute Details

#bridge_classObject

port_group is Hash as [klass_name, …]



6
7
8
# File 'lib/one_touch/core/bridge.rb', line 6

def bridge_class
  @bridge_class
end

#bridge_klassObject

port_group is Hash as [klass_name, …]



6
7
8
# File 'lib/one_touch/core/bridge.rb', line 6

def bridge_klass
  @bridge_klass
end

#port_groupObject

port_group is Hash as [klass_name, …]



6
7
8
# File 'lib/one_touch/core/bridge.rb', line 6

def port_group
  @port_group
end

Class Method Details

.find(bridge_klass) ⇒ Object



17
18
19
# File 'lib/one_touch/core/bridge.rb', line 17

def self.find(bridge_klass)
  @@recorders.select {|rr| rr.bridge_klass.to_sym == bridge_klass.to_sym}.first
end

.update_or_new(opts) ⇒ Object

update an existed instance or create a new instance



22
23
24
25
26
27
28
29
30
# File 'lib/one_touch/core/bridge.rb', line 22

def self.update_or_new(opts)
  if rr = find(opts[:bridge_klass])
    rr.add_new_port(opts[:port_group]) if opts.has_key? :port_group
    rr.bridge_class ||= opts[:bridge_class] if opts.has_key? :bridge_class
    rr
  else
    new(opts)
  end
end

Instance Method Details

#add_new_port(pg) ⇒ Object

add only one port, make sure pg is klassname



33
34
35
36
37
38
39
40
41
42
# File 'lib/one_touch/core/bridge.rb', line 33

def add_new_port(pg)
  portname = pg.keys.first
  if portname # pg maybe equal {} and portname is nil
    if port_group.has_key? portname
      self.port_group[portname].concat pg[portname]
    else
      self.port_group[portname] = pg[portname]
    end
  end
end

#portname_of(klass) ⇒ Object



44
45
46
# File 'lib/one_touch/core/bridge.rb', line 44

def portname_of(klass)
  port_group.select {|k,v| v.include? klass}.keys
end