Module: Ccrypto::InMemoryRecord

Includes:
TR::CondUtils
Included in:
SupportedCipherList
Defined in:
lib/ccrypto/in_memory_record.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

ClassMethods



42
43
44
45
# File 'lib/ccrypto/in_memory_record.rb', line 42

def self.included(klass)
  klass.extend(ClassMethods)
  @klass = klass
end

Instance Method Details

#collect(&block) ⇒ Object



149
150
151
# File 'lib/ccrypto/in_memory_record.rb', line 149

def collect(&block)
  records.collect(&block)
end

#each(&block) ⇒ Object

find() operation



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ccrypto/in_memory_record.rb', line 137

def each(&block)
  raise InMemoryRecordError, "each function requires a block" if not block
  records.each(&block)
  #records.map {|r| 
  #  if r.respond_to?(:ddup)
  #    r.ddup
  #  else
  #    r.clone
  #  end
  #}.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/ccrypto/in_memory_record.rb', line 164

def empty?
  records.empty?
end

#find(field) ⇒ Object

finder only accept hash



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ccrypto/in_memory_record.rb', line 70

def find(field)
  res = []
  case field
  when Hash
    loopRes = []
    field.each do |k,v|
      case v
      when Array
        # extra configration at value is array
        # 1st parameter always the keyword to find
        ss = v.first
        fuzzy = v[1] || false
      else
        ss = v
        fuzzy = false
      end

      if not searchRes[k.to_sym].nil?
        if fuzzy
          found = searchRes[k.to_sym].select { |sk,sv|
            sk.to_s.downcase =~ /#{ss.downcase}/
          }
          loopRes = found.values
        else
          ss = ss.to_s.downcase
          if searchRes[k.to_sym].keys.include?(ss)
            loopRes = searchRes[k.to_sym][ss]
          end
        end
      end

      if is_empty?(res)
        res = loopRes
      else
        res = res & loopRes
      end
    end
  else
    raise InMemoryRecordError, "Hash is expected for finder function"
  end

  # apparantly dup and clone has some different side effects
  # dup will not copy the frozen flag but shall also ignore the dynamic method
  # created using instance_eval
  # clone shall retain the frozen flag but shall also carry over the instance_eval
  # result.
  # Hence let the application to decide to use dup or clone
  res
  #if field[:do_not_dup] == true
  #  res
  #else
  #  # always return a copy instead of actual one
  #  # dup call here might not help if there are embedded
  #  # object in the class as the dup only does swallow
  #  # copy, not deep copy
  #  # Library user need to handle deep copy
  #  res.map { |r| 
  #    if r.respond_to?(:ddup)
  #      r.ddup
  #    else
  #      r.clone
  #    end
  #  }
  #end

end

#register(obj, opts = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/ccrypto/in_memory_record.rb', line 59

def register(obj, opts = {})
  logger.debug "Giving #{obj} to register"
  if not obj.nil?
    extrapolate(obj, opts)
    logger.debug "Extrapolated object #{obj}"
    records << obj
    logger.debug "Object #{obj} added to records"
  end
end

#save_to_storage(record) ⇒ Object Also known as: save

Instance methods



50
51
52
53
54
55
56
# File 'lib/ccrypto/in_memory_record.rb', line 50

def save_to_storage(record)
  target = File.join(self.class.record_storage_root,"#{record}.imr")  
  File.open(target,"w") do |f|
    f.write YAML.dump(self)
  end
  self
end

#select(&block) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/ccrypto/in_memory_record.rb', line 153

def select(&block)
  records.select(&block)
  #records.map { |r| 
  #  if r.respond_to?(:ddup)
  #    r.ddup
  #  else
  #    r.clone
  #  end
  #}.select(&block)
end

#to_aObject



168
169
170
171
172
173
174
175
176
177
# File 'lib/ccrypto/in_memory_record.rb', line 168

def to_a
  records #.freeze
  #records.map { |c| 
  #  if c.respond_to?(:ddup)
  #    c.ddup
  #  else
  #    c.clone
  #  end
  #}.freeze
end