Class: Featured::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/featured/feature.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#itemObject

Returns the value of attribute item.



6
7
8
# File 'lib/featured/feature.rb', line 6

def item
  @item
end

Class Method Details

.dbObject



8
9
10
# File 'lib/featured/feature.rb', line 8

def self.db
  @db ||= Fog::AWS::DynamoDB.new(aws_access_key_id: ENV['AWS_ACCESS_KEY'], aws_secret_access_key: ENV['AWS_SECRET_KEY'])
end

.get(entity, name) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/featured/feature.rb', line 36

def self.get(entity, name)
  response = db.get_item(:features, {HashKeyElement: {S: entity}, RangeKeyElement: {S: name}})
  if item = response.body['Item']
    set(item)
  end
rescue
  nil
end

.gets(entity) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/featured/feature.rb', line 45

def self.gets(entity)
  response = db.query(:features, {S: entity})
  if items = response.body['Items']
    items.map do |item|
      set(item)
    end
  end
rescue
  []
end

.init(entity, name) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/featured/feature.rb', line 27

def self.init(entity, name)
  entry = new
  entry.item = {}
  entry.entity = entity
  entry.name = name
  entry.created_at = Time.now.to_i.to_s
  entry
end

.put(entity, name) ⇒ Object



56
57
58
59
# File 'lib/featured/feature.rb', line 56

def self.put(entity, name)
  entry = init(entity, name)
  response = db.put_item(:features, entry.item, Expected: {entity: {Exists: false}, name: {Exists: false}})
end

.schema(attrs) ⇒ Object



12
13
14
15
16
17
# File 'lib/featured/feature.rb', line 12

def self.schema(attrs)
  attrs.each do |attr, type|
    class_eval "def #{attr}; @item['#{attr}']['#{type}'] if @item.key? '#{attr}' end", __FILE__, __LINE__
    class_eval "def #{attr}= value; @item['#{attr}'] = {'#{type}' => value} end", __FILE__, __LINE__
  end
end

.set(item) ⇒ Object



21
22
23
24
25
# File 'lib/featured/feature.rb', line 21

def self.set(item)
  entry = new
  entry.item = item
  entry
end