Class: Pushr::Configuration

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/pushr/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Configuration

Returns a new instance of Configuration.



11
12
13
14
15
# File 'lib/pushr/configuration.rb', line 11

def initialize(attributes = {})
  attributes.each do |name, value|
    send("#{name}=", value) if respond_to?("#{name}=")
  end
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



5
6
7
# File 'lib/pushr/configuration.rb', line 5

def app
  @app
end

#connectionsObject

Returns the value of attribute connections.



5
6
7
# File 'lib/pushr/configuration.rb', line 5

def connections
  @connections
end

#enabledObject

Returns the value of attribute enabled.



5
6
7
# File 'lib/pushr/configuration.rb', line 5

def enabled
  @enabled
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/pushr/configuration.rb', line 5

def id
  @id
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/pushr/configuration.rb', line 5

def type
  @type
end

Class Method Details

.allObject



52
53
54
55
56
57
58
# File 'lib/pushr/configuration.rb', line 52

def self.all
  if Pushr::Core.configuration_file # only set if file exists
    read_from_yaml_file
  else
    read_from_redis
  end
end

.create(attributes = {}) ⇒ Object



30
31
32
33
34
# File 'lib/pushr/configuration.rb', line 30

def self.create(attributes = {})
  m = new(attributes)
  m.save
  m
end

.create!(attributes = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/pushr/configuration.rb', line 36

def self.create!(attributes = {})
  m = new(attributes)
  unless m.save
    raise Pushr::Error::RecordInvalid
  end
  m
end

.find(key) ⇒ Object



60
61
62
63
# File 'lib/pushr/configuration.rb', line 60

def self.find(key)
  config = Pushr::Core.redis { |conn| conn.hget('pushr:configurations', key) }
  instantiate_json(config, key)
end

.instantiate(hsh) ⇒ Object



81
82
83
84
# File 'lib/pushr/configuration.rb', line 81

def self.instantiate(hsh)
  klass = hsh['type'].split('::').reduce(Object) { |a, e| a.const_get e }
  klass.new(hsh)
end

.instantiate_json(config, id) ⇒ Object



77
78
79
# File 'lib/pushr/configuration.rb', line 77

def self.instantiate_json(config, id)
  instantiate(::MultiJson.load(config).merge!(id: id))
end

.read_from_redisObject



71
72
73
74
75
# File 'lib/pushr/configuration.rb', line 71

def self.read_from_redis
  configurations = Pushr::Core.redis { |conn| conn.hgetall('pushr:configurations') }
  configurations.each { |key, config| configurations[key] = instantiate_json(config, key) }
  configurations.values
end

.read_from_yaml_fileObject



65
66
67
68
69
# File 'lib/pushr/configuration.rb', line 65

def self.read_from_yaml_file
  filename = Pushr::Core.configuration_file
  configs = File.open(filename) { |fd| YAML.load(fd) }
  configs.map { |hsh| instantiate(hsh) }
end

Instance Method Details

#deleteObject



44
45
46
# File 'lib/pushr/configuration.rb', line 44

def delete
  Pushr::Core.redis { |conn| conn.hdel('pushr:configurations', key) }
end

#keyObject



17
18
19
# File 'lib/pushr/configuration.rb', line 17

def key
  "#{app}:#{name}"
end

#saveObject



21
22
23
24
25
26
27
28
# File 'lib/pushr/configuration.rb', line 21

def save
  if valid?
    Pushr::Core.redis { |conn| conn.hset('pushr:configurations', key, to_json) }
    return true
  else
    return false
  end
end

#to_jsonObject



48
49
50
# File 'lib/pushr/configuration.rb', line 48

def to_json
  MultiJson.dump(to_hash)
end