Class: Nightfury::Identity::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nightfury/identity.rb

Constant Summary collapse

METRIC_MAPPINGS =
{
  :value => Nightfury::Metric::Value,
  :time_series => Nightfury::Metric::TimeSeries,
  :avg_time_series => Nightfury::Metric::AvgTimeSeries,
  :count_time_series => Nightfury::Metric::CountTimeSeries
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) ⇒ Base

Returns a new instance of Base.



42
43
44
45
46
# File 'lib/nightfury/identity.rb', line 42

def initialize(id, options={})
  @redis = Nightfury.redis
  @id = id
  @tags = options[:tags]
end

Class Attribute Details

.metricsObject (readonly)

Returns the value of attribute metrics.



14
15
16
# File 'lib/nightfury/identity.rb', line 14

def metrics
  @metrics
end

.store_asObject

Returns the value of attribute store_as.



15
16
17
# File 'lib/nightfury/identity.rb', line 15

def store_as
  @store_as
end

.tagsObject (readonly)

Returns the value of attribute tags.



14
15
16
# File 'lib/nightfury/identity.rb', line 14

def tags
  @tags
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



39
40
41
# File 'lib/nightfury/identity.rb', line 39

def id
  @id
end

#redisObject (readonly)

Returns the value of attribute redis.



40
41
42
# File 'lib/nightfury/identity.rb', line 40

def redis
  @redis
end

#tagsObject

Returns the value of attribute tags.



39
40
41
# File 'lib/nightfury/identity.rb', line 39

def tags
  @tags
end

Class Method Details

.metric(name, type = :value, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nightfury/identity.rb', line 21

def metric(name, type = :value, options={})
  @metrics ||= {}
  @metrics[name] = {type: type}
  store_as = options[:store_as] ? ":#{options[:store_as]}" : 'nil' 
  step = options[:step] ? ":#{options[:step]}" : ":minute"
  class_eval <<-ENDOFMETHOD
    def #{name}
      @_#{name} ||= METRIC_MAPPINGS[:#{type}].new(:#{name}, redis_key_prefix: key_prefix, store_as: #{store_as}, step: #{step})
    end
  ENDOFMETHOD
end

.nameObject



17
18
19
# File 'lib/nightfury/identity.rb', line 17

def name
  self.to_s.demodulize.underscore
end

.tag(name, options = {}) ⇒ Object



33
34
35
36
# File 'lib/nightfury/identity.rb', line 33

def tag(name, options={})
  @tags ||= {}
  @tags[name] = options[:store_as] ? options[:store_as] : name
end

Instance Method Details

#key_prefixObject



48
49
50
51
52
53
# File 'lib/nightfury/identity.rb', line 48

def key_prefix
  store_name = self.class.store_as ? self.class.store_as : self.class.name
  tag_ids = generate_tag_ids
  tag_ids = tag_ids.nil? ? '' : ":#{tag_ids}"
  "#{store_name}.#{id}#{tag_ids}"
end

#new_record?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/nightfury/identity.rb', line 55

def new_record?
  redis.keys("#{key_prefix}*").empty?
end