Class: SimpleFeed::Providers::Key

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/simplefeed/providers/key.rb

Overview

Here is a meta key for a given user ID:

   user   'm' for meta
     ↓        ↓
 "ff|u.f23098.m"
  ↑         ↑
namespace user_id(base62)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id, key_template) ⇒ Key


24
25
26
27
28
29
# File 'lib/simplefeed/providers/key.rb', line 24

def initialize(user_id, key_template)
  self.user_id  = user_id
  self.key_template = key_template

  define_key_methods
end

Instance Attribute Details

#key_templateObject

Returns the value of attribute key_template


19
20
21
# File 'lib/simplefeed/providers/key.rb', line 19

def key_template
  @key_template
end

#user_idObject

Returns the value of attribute user_id


19
20
21
# File 'lib/simplefeed/providers/key.rb', line 19

def user_id
  @user_id
end

Instance Method Details

#base62_user_idObject


44
45
46
# File 'lib/simplefeed/providers/key.rb', line 44

def base62_user_id
  @base62_user_id ||= ::Base62.encode(user_id)
end

#define_key_methodsObject


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/simplefeed/providers/key.rb', line 31

def define_key_methods
  key_template.key_types.each do |type|
    key_name = type.name
    unless self.respond_to?(key_name)
      self.class.send(:define_method, key_name) do
        instance_variable_get("@#{key_name}") ||
          instance_variable_set("@#{key_name}", type.render(render_options))
      end
    end

  end
end

#inspectObject


63
64
65
# File 'lib/simplefeed/providers/key.rb', line 63

def inspect
  render_options.inspect
end

#keysObject


48
49
50
# File 'lib/simplefeed/providers/key.rb', line 48

def keys
  key_names.map { |name| self.send(name) }
end

#render_optionsObject


52
53
54
55
56
57
# File 'lib/simplefeed/providers/key.rb', line 52

def render_options
  key_template.render_options.merge!({
                                       'user_id'        => user_id,
                                       'base62_user_id' => base62_user_id
                                     })
end

#to_sObject


59
60
61
# File 'lib/simplefeed/providers/key.rb', line 59

def to_s
  super + { user_id: user_id, base62_user_id: base62_user_id, keys: keys }.to_s
end