Sha256: 06c9e26eb408944bfd37b4d52f5dd0fb28f8be3fcc4763428ce8cd6bcbf66bdc

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 KB

Contents

module Marty::Util
  def self.set_posting_id(sid)
    snap = Marty::Posting.find_by_id(sid)
    sid = nil if snap && (snap.created_dt == Float::INFINITY)
    Netzke::Base.session[:posting] = sid
  end

  def self.get_posting
    sid = Netzke::Base.session && Netzke::Base.session[:posting]
    return unless sid.is_a? Fixnum
    sid && Marty::Posting.find_by_id(sid)
  end

  def self.get_posting_time
    snap = self.get_posting
    snap ? snap.created_dt : Float::INFINITY
  end

  def self.warped?
    self.get_posting_time != Float::INFINITY
  end

  def self.logger
    @@s_logger ||= Rails.logger || Logger.new($stderr)
  end

  # route path to where Marty is mounted
  def self.marty_path
    Rails.application.routes.named_routes[:marty].path.spec
  end

  def self.pg_range_to_human(r)
    return r if r == "empty" || r.nil?

    m = /\A(?<open>\[|\()(?<start>.*?),(?<end>.*?)(?<close>\]|\))\z/.match(r)

    raise "bad PG range #{r}" unless m

    if m[:start] == ""
      res = ""
    else
      op = m[:open] == "(" ? ">" : ">="
      res = "#{op}#{m[:start]}"
    end

    if m[:end] != ""
      op = m[:close] == ")" ? "<" : "<="
      res += "#{op}#{m[:end]}"
    end

    res
  end

  def self.human_to_pg_range(r)
    return r if r == "empty"

    m = /\A
    ((?<op0>\>|\>=)(?<start>[^\<\>\=]*?))?
    ((?<op1>\<|\<=)(?<end>[^\<\>\=]*?))?
    \z/x.match(r)

    raise "bad range #{r}" unless m

    if m[:op0]
      open = m[:op0] == ">" ? "(" : "["
      start = "#{open}#{m[:start]}"
    else
      start = "["
    end

    if m[:op1]
      close = m[:op1] == "<" ? ")" : "]"
      ends = "#{m[:end]}#{close}"
    else
      ends = "]"
    end

    "#{start},#{ends}"
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
marty-0.5.15 lib/marty/util.rb~
marty-0.5.14 lib/marty/util.rb~
marty-0.5.13 lib/marty/util.rb~
marty-0.5.12 lib/marty/util.rb~