Class: Bluecap::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/bluecap/handlers/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Attributes

Initialize an Attributes handler.

data - A Hash containing attributes to set for a user:

:attributes - The hash key/value pairs to be set.
:id - The id of the user to set the attributes for.

Examples

Bluecap::Attributes.new(
  id: 3,
  attributes: {
    gender: 'Female',
    country: 'Australia'
  }
)


21
22
23
24
# File 'lib/bluecap/handlers/attributes.rb', line 21

def initialize(data)
  @id = data.fetch(:id)
  @attributes = data.fetch(:attributes)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/bluecap/handlers/attributes.rb', line 4

def attributes
  @attributes
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/bluecap/handlers/attributes.rb', line 4

def id
  @id
end

Instance Method Details

#handleObject

Store attributes for a user. Each attribute/value has its own bitset so this is best used with data that has a limited number of values (e.g.: gender, country).

Returns nil.



59
60
61
62
63
64
65
# File 'lib/bluecap/handlers/attributes.rb', line 59

def handle
  Bluecap.redis.multi do
    keys.each { |k| Bluecap.redis.setbit(k, @id, 1) }
  end

  nil
end

#key(attribute, value) ⇒ Object

Returns a cleaned key for an attribute and value.

Examples

key('gender', 'female')
# => "attributes:gender:female"


50
51
52
# File 'lib/bluecap/handlers/attributes.rb', line 50

def key(attribute, value)
  "attributes:#{Bluecap::Keys.clean(attribute)}:#{Bluecap::Keys.clean(value)}"
end

#keysObject

Returns keys for each of the attributes.

Examples

attributes = Bluecap::Attributes.new(
  id:3,
  attributes: {
    gender: 'Female',
    country: 'Australia'
  }
attributes.keys
# => ["attributes:gender:female", "attributes:country:australia"]

Returns the Array of keys.



40
41
42
# File 'lib/bluecap/handlers/attributes.rb', line 40

def keys
  @attributes.map { |k, v| key(k.to_s, v) }
end