Class: Promiscuous::Dependency
- Inherits:
-
Object
- Object
- Promiscuous::Dependency
- Defined in:
- lib/promiscuous/dependency.rb
Instance Attribute Summary collapse
-
#internal_key ⇒ Object
Returns the value of attribute internal_key.
-
#type ⇒ Object
Returns the value of attribute type.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#eql?(other) ⇒ Boolean
(also: #==)
We need the eql? method to function properly (we use ==, uniq, …) in operation XXX The version is not taken in account.
- #hash ⇒ Object
-
#initialize(*args) ⇒ Dependency
constructor
A new instance of Dependency.
- #key(role) ⇒ Object
- #read? ⇒ Boolean
- #redis_node(distributed_redis = nil) ⇒ Object
- #to_s ⇒ Object
- #write? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Dependency
Returns a new instance of Dependency.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/promiscuous/dependency.rb', line 6 def initialize(*args) = args. @type = [:type] @owner = [:owner] @dont_hash = [:dont_hash] @internal_key = args.join('/') if @internal_key =~ /^[0-9]+$/ @internal_key = @internal_key.to_i @hash = @internal_key else @hash = FNV.new.fnv1a_32(@internal_key) if Promiscuous::Config.hash_size.to_i > 0 # We hash dependencies to have a O(1) memory footprint in Redis. # The hashing needs to be deterministic across instances in order to # function properly. @hash = @hash % Promiscuous::Config.hash_size.to_i @internal_key = @hash unless @dont_hash end end if @owner @internal_key = "#{@owner}:#{@internal_key}" end end |
Instance Attribute Details
#internal_key ⇒ Object
Returns the value of attribute internal_key.
4 5 6 |
# File 'lib/promiscuous/dependency.rb', line 4 def internal_key @internal_key end |
#type ⇒ Object
Returns the value of attribute type.
4 5 6 |
# File 'lib/promiscuous/dependency.rb', line 4 def type @type end |
#version ⇒ Object
Returns the value of attribute version.
4 5 6 |
# File 'lib/promiscuous/dependency.rb', line 4 def version @version end |
Class Method Details
.parse(payload, options = {}) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/promiscuous/dependency.rb', line 57 def self.parse(payload, ={}) case payload when /^(.+):([0-9]+)$/ then new($1, ).tap { |d| d.version = $2.to_i } when /^(.+)$/ then new($1, ) end end |
Instance Method Details
#as_json(options = {}) ⇒ Object
53 54 55 |
# File 'lib/promiscuous/dependency.rb', line 53 def as_json(={}) @version ? [@internal_key, @version].join(':') : @internal_key end |
#eql?(other) ⇒ Boolean Also known as: ==
We need the eql? method to function properly (we use ==, uniq, …) in operation XXX The version is not taken in account.
70 71 72 |
# File 'lib/promiscuous/dependency.rb', line 70 def eql?(other) self.internal_key == other.internal_key end |
#hash ⇒ Object
75 76 77 |
# File 'lib/promiscuous/dependency.rb', line 75 def hash self.internal_key.hash end |
#key(role) ⇒ Object
44 45 46 |
# File 'lib/promiscuous/dependency.rb', line 44 def key(role) Promiscuous::Key.new(role).join(@internal_key) end |
#read? ⇒ Boolean
34 35 36 37 |
# File 'lib/promiscuous/dependency.rb', line 34 def read? raise "Type not set" unless @type @type == :read end |
#redis_node(distributed_redis = nil) ⇒ Object
48 49 50 51 |
# File 'lib/promiscuous/dependency.rb', line 48 def redis_node(distributed_redis=nil) distributed_redis ||= Promiscuous::Redis.master distributed_redis.nodes[@hash % distributed_redis.nodes.size] end |
#to_s ⇒ Object
64 65 66 |
# File 'lib/promiscuous/dependency.rb', line 64 def to_s as_json.to_s end |
#write? ⇒ Boolean
39 40 41 42 |
# File 'lib/promiscuous/dependency.rb', line 39 def write? raise "Type not set" unless @type @type == :write end |