Class: JsonCache::MethodCache

Inherits:
Object
  • Object
show all
Includes:
FromHash
Defined in:
lib/json_cache/method_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#call_blk(&b) ⇒ Object

Returns the value of attribute call_blk.



4
5
6
# File 'lib/json_cache/method_cache.rb', line 4

def call_blk
  @call_blk
end

#class_blk(&b) ⇒ Object

Returns the value of attribute class_blk.



4
5
6
# File 'lib/json_cache/method_cache.rb', line 4

def class_blk
  @class_blk
end

#class_nameObject

Returns the value of attribute class_name.



4
5
6
# File 'lib/json_cache/method_cache.rb', line 4

def class_name
  @class_name
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/json_cache/method_cache.rb', line 4

def name
  @name
end

Instance Method Details

#allObject



70
71
72
# File 'lib/json_cache/method_cache.rb', line 70

def all
  JsonCache::CallResult.where('_query_name' => name)
end

#create_from_raw(raw, ops, from_array) ⇒ Object



51
52
53
54
# File 'lib/json_cache/method_cache.rb', line 51

def create_from_raw(raw,ops,from_array)
  cr_hash = raw.merge('_query_params' => ops, '_query_name' => name, '_from_array' => from_array)
  type_class.create!(cr_hash)
end

#ensure_class_existsObject



19
20
21
22
23
# File 'lib/json_cache/method_cache.rb', line 19

def ensure_class_exists
  eval class_name
rescue
  return make_class
end

#get(ops) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/json_cache/method_cache.rb', line 63

def get(ops)
  if need?(ops)
    get_fresh(ops)
  else
    get_existing(ops)
  end
end

#get_existing(ops) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/json_cache/method_cache.rb', line 37

def get_existing(ops)
  a = type_class.where('_query_params' => ops)
  f = a.first
  if !f
    nil
  elsif f['_from_array']
    a.to_a
  else
    f
  end
end

#get_fresh(ops) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/json_cache/method_cache.rb', line 55

def get_fresh(ops)
  res = call_blk[ops]
  if res.kind_of?(Array)
    res.map { |x| create_from_raw(x,ops,true) }
  else
    create_from_raw(res,ops,false)
  end
end

#make_classObject



24
25
26
27
28
29
# File 'lib/json_cache/method_cache.rb', line 24

def make_class
  b = class_blk || lambda { |x| }
  cls = Class.new(JsonCache::CallResult,&b)
  Object.const_set(class_name,cls)
  cls
end

#need?(ops) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/json_cache/method_cache.rb', line 48

def need?(ops)
  !get_existing(ops)
end