class Alfred::Setting

Attributes

backend_file[RW]
format[R]

Public Class Methods

new(alfred, &block) click to toggle source
# File lib/alfred/setting.rb, line 9
def initialize(alfred, &block)
  @core = alfred
  @table = {}

  instance_eval(&block) if block_given?

  @format ||= "yaml"
  @backend_file ||= File.join(@core.storage_path, "setting.#{@format}")

  raise InvalidFormat, "#{format} is not suported." unless validate_format

  unless File.exist?(@backend_file)
    @table.merge!({:id => @core.bundle_id})
    dump(:flush => true)
  else
    load
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/alfred/setting.rb, line 104
def ==(other)
  return false unless other.kind_of?(Alfred::Setting)
  @table == other.table
end
[](name) click to toggle source
# File lib/alfred/setting.rb, line 84
def [](name)
  @table[name]
end
[]=(name, value) click to toggle source

Sets the value of a member.

person = Alfred::Setting.new('name' => 'John Smith', 'age' => 70)
person[:age] = 42
# File lib/alfred/setting.rb, line 94
def []=(name, value)
  @table[name] = value
end
close(opts = {})
Alias for: dump
dump(opts = {}) click to toggle source
# File lib/alfred/setting.rb, line 39
def dump(opts = {})
  send("dump_to_#{format}".to_sym, opts)
end
Also aliased as: close
each_pair() { |p| ... } click to toggle source
# File lib/alfred/setting.rb, line 78
def each_pair
  return to_enum __method__ unless block_given?
  @table.each_pair{|p| yield p}
end
encode_with(coder) click to toggle source
# File lib/alfred/setting.rb, line 53
def encode_with(coder)
  coder['table'] = @table
end
eql?(other) click to toggle source
# File lib/alfred/setting.rb, line 109
def eql?(other)
  return false unless other.kind_of?(Alfred::Setting)
  @table.eql?(other.table)
end
has_key?(key) click to toggle source
# File lib/alfred/setting.rb, line 98
def has_key?(key)
  @table.has_key?(key)
end
Also aliased as: key?
key?(key)
Alias for: has_key?
load() click to toggle source
# File lib/alfred/setting.rb, line 34
def load
  send("load_from_#{format}".to_sym)
end
marshal_dump() click to toggle source

Provides marshalling support for use by the Marshal library.

# File lib/alfred/setting.rb, line 61
def marshal_dump
  @table
end
marshal_load(x) click to toggle source

Provides marshalling support for use by the Marshal library.

# File lib/alfred/setting.rb, line 68
def marshal_load(x)
  @table.merge! x
end
to_h() click to toggle source

Converts to hash

# File lib/alfred/setting.rb, line 74
def to_h
  @table.dup
end
to_yaml_properties() click to toggle source
# File lib/alfred/setting.rb, line 49
def to_yaml_properties
  [ '@table' ]
end
validate_format() click to toggle source
# File lib/alfred/setting.rb, line 29
def validate_format
  ['yaml'].include?(format)
end

Protected Instance Methods

dump_to_yaml(opts = {}) click to toggle source
# File lib/alfred/setting.rb, line 131
def dump_to_yaml(opts = {})
  File.open(@backend_file, File::WRONLY|File::TRUNC|File::CREAT) { |f|
    YAML::dump(@table, f)
    f.flush if opts[:flush]
  }
end
load_from_yaml() click to toggle source
# File lib/alfred/setting.rb, line 127
def load_from_yaml
  @table.merge!(YAML::load_file(@backend_file))
end