Class: Pushr::Configuration
- Inherits:
-
Object
- Object
- Pushr::Configuration
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/pushr/configuration.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#connections ⇒ Object
Returns the value of attribute connections.
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#id ⇒ Object
Returns the value of attribute id.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
- .all ⇒ Object
- .create(attributes = {}) ⇒ Object
- .create!(attributes = {}) ⇒ Object
- .find(key) ⇒ Object
- .instantiate(hsh) ⇒ Object
- .instantiate_json(config, id) ⇒ Object
- .read_from_redis ⇒ Object
- .read_from_yaml_file ⇒ Object
Instance Method Summary collapse
- #delete ⇒ Object
-
#initialize(attributes = {}) ⇒ Configuration
constructor
A new instance of Configuration.
- #key ⇒ Object
- #queue_depth ⇒ Object
- #save ⇒ Object
- #to_json ⇒ Object
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
#app ⇒ Object
Returns the value of attribute app.
5 6 7 |
# File 'lib/pushr/configuration.rb', line 5 def app @app end |
#connections ⇒ Object
Returns the value of attribute connections.
5 6 7 |
# File 'lib/pushr/configuration.rb', line 5 def connections @connections end |
#enabled ⇒ Object
Returns the value of attribute enabled.
5 6 7 |
# File 'lib/pushr/configuration.rb', line 5 def enabled @enabled end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/pushr/configuration.rb', line 5 def id @id end |
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/pushr/configuration.rb', line 5 def type @type end |
Class Method Details
.all ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/pushr/configuration.rb', line 56 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
64 65 66 67 |
# File 'lib/pushr/configuration.rb', line 64 def self.find(key) config = Pushr::Core.redis { |conn| conn.hget('pushr:configurations', key) } instantiate_json(config, key) end |
.instantiate(hsh) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/pushr/configuration.rb', line 85 def self.instantiate(hsh) klass = hsh['type'].split('::').reduce(Object) do |a, e| if Object.const_defined?(hsh['type']) a.const_get e end end return nil if klass == nil klass.new(hsh) end |
.instantiate_json(config, id) ⇒ Object
81 82 83 |
# File 'lib/pushr/configuration.rb', line 81 def self.instantiate_json(config, id) instantiate(::MultiJson.load(config).merge!(id: id)) end |
.read_from_redis ⇒ Object
75 76 77 78 79 |
# File 'lib/pushr/configuration.rb', line 75 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.compact end |
.read_from_yaml_file ⇒ Object
69 70 71 72 73 |
# File 'lib/pushr/configuration.rb', line 69 def self.read_from_yaml_file filename = Pushr::Core.configuration_file configs = File.open(filename) { |fd| YAML.load(fd) } configs.map { |hsh| instantiate(hsh) }.compact end |
Instance Method Details
#delete ⇒ Object
48 49 50 |
# File 'lib/pushr/configuration.rb', line 48 def delete Pushr::Core.redis { |conn| conn.hdel('pushr:configurations', key) } end |
#key ⇒ Object
17 18 19 |
# File 'lib/pushr/configuration.rb', line 17 def key "#{app}:#{name}" end |
#queue_depth ⇒ Object
44 45 46 |
# File 'lib/pushr/configuration.rb', line 44 def queue_depth Pushr::Core.redis { |conn| conn.llen("pushr:#{key}") } end |
#save ⇒ Object
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_json ⇒ Object
52 53 54 |
# File 'lib/pushr/configuration.rb', line 52 def to_json MultiJson.dump(to_hash) end |