Sha256: 019180bdf26a742c7cf85784101576079c027cc23c669f12fa859e602cf2354e

Contents?: true

Size: 879 Bytes

Versions: 43

Compression:

Stored size: 879 Bytes

Contents

class Marty::Config < Marty::Base
  class ConfigValidator < ActiveModel::Validator
    def validate(entry)
      v = entry.get_value
      entry.errors[:base] << "bad JSON value" if v.nil?
      v
    end
  end

  validates_presence_of :key, :value
  validates_uniqueness_of :key
  validates_with ConfigValidator

  delorean_fn :lookup, sig: 1 do
    |key|
    self[key]
  end

  def get_value
    self.value[0]
  end

  def set_value(v)
    self.value = [v]
  end

  def self.[]=(key, value)
    entry = find_by_key(key)
    if !entry
      entry = self.new
      entry.key = key
    end
    entry.set_value(value)
    entry.save!

    value
  end

  def self.[](key)
    entry = find_by_key(key)
    entry and entry.get_value
  end

  def self.del(key)
    entry = find_by_key(key)
    if entry
      result = entry.get_value
      entry.destroy
      result
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
marty-2.5.2 app/models/marty/config.rb
marty-2.5.1 app/models/marty/config.rb
marty-2.5.0 app/models/marty/config.rb
marty-2.4.9 app/models/marty/config.rb
marty-2.4.8 app/models/marty/config.rb
marty-2.4.7 app/models/marty/config.rb
marty-2.4.6 app/models/marty/config.rb
marty-2.4.5 app/models/marty/config.rb
marty-2.4.4 app/models/marty/config.rb
marty-2.4.3 app/models/marty/config.rb
marty-2.4.2 app/models/marty/config.rb
marty-2.4.1 app/models/marty/config.rb
marty-2.4.0 app/models/marty/config.rb
marty-2.3.15 app/models/marty/config.rb
marty-2.3.14 app/models/marty/config.rb
marty-2.3.13 app/models/marty/config.rb
marty-2.3.12 app/models/marty/config.rb
marty-2.3.11 app/models/marty/config.rb
marty-2.3.10 app/models/marty/config.rb
marty-2.3.9 app/models/marty/config.rb