Sha256: 656fba76e85720bc5ddf41af7b90f839f434d3b5c81cfbf5fcf4d4a070427051

Contents?: true

Size: 878 Bytes

Versions: 33

Compression:

Stored size: 878 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

33 entries across 33 versions & 1 rubygems

Version Path
marty-1.2.9 app/models/marty/config.rb
marty-1.2.8 app/models/marty/config.rb
marty-1.2.7 app/models/marty/config.rb
marty-1.2.6 app/models/marty/config.rb
marty-1.2.5 app/models/marty/config.rb
marty-1.2.4 app/models/marty/config.rb
marty-1.2.3 app/models/marty/config.rb
marty-1.2.2 app/models/marty/config.rb
marty-1.2.1 app/models/marty/config.rb
marty-1.2.0 app/models/marty/config.rb
marty-1.1.9 app/models/marty/config.rb
marty-1.1.8 app/models/marty/config.rb
marty-1.1.7 app/models/marty/config.rb
marty-1.1.6 app/models/marty/config.rb
marty-1.1.5 app/models/marty/config.rb
marty-1.1.4 app/models/marty/config.rb
marty-1.1.3 app/models/marty/config.rb
marty-1.1.2 app/models/marty/config.rb
marty-1.1.1 app/models/marty/config.rb
marty-1.0.54 app/models/marty/config.rb