Module: UssdEngine::Controller::Storable::StorableClassMethods

Defined in:
lib/ussd_engine/controller/storable.rb

Instance Method Summary collapse

Instance Method Details

#stores(field, accessor = nil, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ussd_engine/controller/storable.rb', line 13

def stores(field, accessor = nil, &block)
  # Given
  # field = :user_id
  instance_prop = "@#{field}" # @user_id
  key_method_name = "#{field}_key" # user_id_key
  getter_method_name = field  # user_id
  setter_method_name = "#{field}="  # user_id=

  # def user_id_key
  #   File.join ussd_request_id, "user_id"
  # end
  define_method key_method_name do
    File.join ussd_request_id, field.to_s
  end

  # def user_id
  #   session[user_id_key]
  # end
  define_method getter_method_name do
    session[send(key_method_name)]
  end

  # def user_id=(value)
  #   session[user_id_key] = value
  # end
  define_method setter_method_name do |value|
    session[send(key_method_name)] = value
  end

  if accessor.present?
    raise "A block is required if you pass an accessor" unless block_given?

    # Given
    # accessor = :user
    cache_method_name = "#{accessor}_cache" # user_cache
    cache_instance_prop = "@#{cache_method_name}" # @user_cache

    # def user_cache
    #   @user_cache ||= {}
    # end
    define_method cache_method_name do
      unless instance_variable_defined? cache_instance_prop
        instance_variable_set cache_instance_prop, {}
      end
      instance_variable_get cache_instance_prop
    end

    # Given
    # block = do |user_id|
    #   User.find user_id
    # end
    #
    # def user
    #   return if user_id.blank?
    #
    #   user_cache[user_id] ||= block(user_id)
    # end
    define_method accessor do
      field_value = send(getter_method_name)
      return if field_value.blank?

      send(cache_method_name)[field_value] ||= instance_exec(field_value, &block)
    end
  end
end