Module: Nomo

Extended by:
Nomo
Included in:
Nomo
Defined in:
lib/nomo.rb,
lib/nomo/page.rb,
lib/nomo/model.rb,
lib/nomo/helpers.rb,
lib/nomo/version.rb,
lib/nomo/rails/engine.rb,
lib/nomo/conditional_get.rb,
lib/nomo/rack/middleware/unit_of_work.rb

Defined Under Namespace

Modules: ConditionalGet, Helpers, Model, Rack, Rails Classes: Page

Constant Summary collapse

VERSION =
"0.0.50"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_optionsObject



67
68
69
# File 'lib/nomo.rb', line 67

def default_options
  @default_options || {}
end

#loggerObject



73
74
75
# File 'lib/nomo.rb', line 73

def logger
  @logger || ::Rails.logger
end

#serverObject

Nomo WebSocket server

Accepts:

1. A 'hostname:port' String


19
20
21
# File 'lib/nomo.rb', line 19

def server
  @server
end

Instance Method Details

#execObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/nomo.rb', line 89

def exec
  if @in_multi
    for key in @updates.uniq
      Nomo.logger.debug("(nomo) update published -- #{key}")
      Nomo.redis.publish("updates", key)
    end
    @updates = []
    @in_multi = false

    Nomo.logger.debug("(nomo) Nomo.exec")
    Nomo.redis.exec
  end
end

#multiObject



80
81
82
83
84
85
86
87
# File 'lib/nomo.rb', line 80

def multi
  unless @in_multi
    @in_multi = true

    Nomo.logger.debug("(nomo) Nomo.multi")
    Nomo.redis.multi
  end
end

#publish(channel, msg) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/nomo.rb', line 103

def publish(channel, msg)
  if @in_multi
    Nomo.logger.debug("(nomo) update queued -- #{msg}")
    @updates << msg
  else
    Nomo.logger.debug("(nomo) update published -- #{msg}")
    Nomo.redis.publish(channel, msg)
  end
end

#redisObject

Returns the Redis connection. If none exists, try creating one.



53
54
55
# File 'lib/nomo.rb', line 53

def redis
  @redis ||= self.redis = Redis.respond_to?(:connect) ? Redis.connect : 'localhost:6379'
end

#redis=(server) ⇒ Object

Accepts:

1. A 'hostname:port' String
2. A 'hostname:port:db' String (to select the Redis db)
3. A 'hostname:port/namespace' String (to set the Redis namespace)
4. A Redis URL String 'redis://host:port'
5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`,
   or `Redis::Namespace`.


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nomo.rb', line 30

def redis=(server)
  case server
  when String
    if server =~ /redis\:\/\//
      redis = Redis.connect(:url => server, :thread_safe => true)
    else
      server, namespace = server.split('/', 2)
      host, port, db = server.split(':')
      redis = Redis.new(:host => host, :port => port,
        :thread_safe => true, :db => db)
    end
    namespace ||= :nomo

    @redis = Redis::Namespace.new(namespace, :redis => redis)
  when Redis::Namespace
    @redis = server
  else
    @redis = Redis::Namespace.new(:nomo, :redis => server)
  end
end

#unit_of_workObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nomo.rb', line 113

def unit_of_work
  begin
    yield if block_given?
  ensure
    if @in_multi
      Nomo.redis.discard
      @in_multi = false
      @updates = []
    end
  end
end