Sha256: 0f482288775ebe04f4da02614e61bc0d7fcc4dad67d94219df3f2331a82f8a9b

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

class Settingson::Store

  attr_accessor :name, :value

  def self.model_name
    self
  end

  def initialize(klass, string, *args)
    @klass  = klass
    @value  = parse(string, args.first)
  end

  def to_s
    @value
  end

  def to_ary
    [ @value ]
  end

  def nil?
    true
  end

  def [](*args)
    return nil if args.nil?
    if args.count == 1
      result = @klass.find_by(name: "#{@name}.#{args.first}").try(:value)
      result.nil? ? nil : YAML.load(result)
    else
      args.map do |subname|
        result = @klass.find_by(name: "#{@name}.#{subname}").try(:value)
        result.nil? ? nil : YAML.load(result)
      end
    end
  end

  def []=(*args)
    return nil if args.nil?
    if args.count == 2
      if record = @klass.find_by(name: "#{@name}.#{args.first}")
        record.update(value: args.last.to_yaml)
      else
        @klass.create(name: "#{@name}.#{args.first}", value: args.last.to_yaml)
      end
      args.last
    elsif args.count > 2
      values = args.pop
      args.map do |subname|
        value = values.shift
        if record = @klass.find_by(name: "#{@name}.#{subname}")
          record.update(value: value.to_yaml)
        else
          @klass.create(name: "#{@name}.#{subname}", value: value.to_yaml)
        end
        value
      end
    end
  end

  def method_missing(string, *args)
    @value   = parse(string, args.first)
  end

  protected
  def parse(string, value)
    case string.to_s
    when /(.+)=/
      @name = @name.nil? ? $1 : @name + ".#{$1}"
      if record = @klass.find_by(name: @name)
        if value.nil?
          record.destroy
        else
          record.update(value: value.to_yaml)
        end
        value
      else
        @klass.create(name: @name, value: value.to_yaml) unless value.nil?
        value
      end
    else
      @name = @name.nil? ? string.to_s : @name + ".#{string.to_s}"
      if result = @klass.find_by(name: @name)
        YAML.load result.value
      else
        self
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
settingson-1.1.15 lib/settingson/store.rb