Class: CrossLanguageSpotter::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/crosslanguagespotter/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



25
26
27
28
29
# File 'lib/crosslanguagespotter/context.rb', line 25

def initialize
    @map = Hash.new {|h,k| h[k]=[]}
    @sequence_of_values = []
    @register_sequence = []
end

Instance Attribute Details

#sequence_of_valuesObject (readonly)

Returns the value of attribute sequence_of_values.



23
24
25
# File 'lib/crosslanguagespotter/context.rb', line 23

def sequence_of_values
  @sequence_of_values
end

Instance Method Details

#cloneObject



65
66
67
68
69
70
71
# File 'lib/crosslanguagespotter/context.rb', line 65

def clone
    new_instance = Context.new
    @register_sequence.each do |r|
        new_instance.register(r[:value],r[:declarator])
    end
    new_instance
end

#countObject



35
36
37
# File 'lib/crosslanguagespotter/context.rb', line 35

def count
    values.count
end

#declarators_per_value(value) ⇒ Object



43
44
45
# File 'lib/crosslanguagespotter/context.rb', line 43

def declarators_per_value(value)
    @map[value]
end

#has_value?(v) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/crosslanguagespotter/context.rb', line 39

def has_value?(v)
    values.include?(v)
end

#intersection(values) ⇒ Object



73
74
75
76
77
# File 'lib/crosslanguagespotter/context.rb', line 73

def intersection(values)
    new_instance = self.clone
    new_instance.intersection!(values)
    new_instance
end

#intersection!(values) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/crosslanguagespotter/context.rb', line 79

def intersection!(values)
    @map.keys.each do |k|
        if values.is_a? Array
            @map[k] = [] unless values.include?(k)
        elsif values.is_a? Context
            if values.has_value?(k)
                @map[k].concat(values.declarators_per_value(k))
            else
                @map[k] = []
            end
        else
            raise "error"
        end             
    end
    self
end

#merge(other) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/crosslanguagespotter/context.rb', line 57

def merge(other)
    other.values.each do |v|
        other.declarators_per_value(v).each do |d|
            register(v,d)
        end
    end
end

#register(value, declarator) ⇒ Object



51
52
53
54
55
# File 'lib/crosslanguagespotter/context.rb', line 51

def register(value,declarator)
    @sequence_of_values << value
    @map[value] << declarator unless @map[value].include?(declarator)
    @register_sequence << {value:value, declarator:value}
end

#to_aObject



100
101
102
103
104
105
106
# File 'lib/crosslanguagespotter/context.rb', line 100

def to_a
    a = []
    values.sort.each do |v|
        a << {value:v,declarators:declarators_per_value(v)}
    end
    a
end

#to_sObject



108
109
110
# File 'lib/crosslanguagespotter/context.rb', line 108

def to_s
    to_a.to_s
end

#valuesObject



31
32
33
# File 'lib/crosslanguagespotter/context.rb', line 31

def values
    @map.keys.select {|k| @map[k].count>0}
end