Class: Alfred::Setting
- Inherits:
-
Object
show all
- Defined in:
- lib/alfred/setting.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(alfred, &block) ⇒ Setting
Returns a new instance of Setting.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# 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
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Send missing method to @table to mimic a hash
121
122
123
|
# File 'lib/alfred/setting.rb', line 121
def method_missing (name, *args, &block) @table.send(name, *args, &block)
end
|
Instance Attribute Details
#backend_file ⇒ Object
Returns the value of attribute backend_file.
6
7
8
|
# File 'lib/alfred/setting.rb', line 6
def backend_file
@backend_file
end
|
Returns the value of attribute format.
7
8
9
|
# File 'lib/alfred/setting.rb', line 7
def format
@format
end
|
Instance Method Details
#==(other) ⇒ Object
104
105
106
107
|
# File 'lib/alfred/setting.rb', line 104
def ==(other)
return false unless other.kind_of?(Alfred::Setting)
@table == other.table
end
|
#[](name) ⇒ Object
84
85
86
|
# File 'lib/alfred/setting.rb', line 84
def [](name)
@table[name]
end
|
#[]=(name, value) ⇒ Object
Sets the value of a member.
person = Alfred::Setting.new('name' => 'John Smith', 'age' => 70)
person[:age] = 42
94
95
96
|
# File 'lib/alfred/setting.rb', line 94
def []=(name, value)
@table[name] = value
end
|
#dump(opts = {}) ⇒ Object
Also known as:
close
39
40
41
|
# File 'lib/alfred/setting.rb', line 39
def dump(opts = {})
send("dump_to_#{format}".to_sym, opts)
end
|
#each_pair ⇒ Object
78
79
80
81
|
# 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) ⇒ Object
53
54
55
|
# File 'lib/alfred/setting.rb', line 53
def encode_with(coder)
coder['table'] = @table
end
|
#eql?(other) ⇒ Boolean
109
110
111
112
|
# 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) ⇒ Boolean
Also known as:
key?
98
99
100
|
# File 'lib/alfred/setting.rb', line 98
def has_key?(key)
@table.has_key?(key)
end
|
#load ⇒ Object
34
35
36
|
# File 'lib/alfred/setting.rb', line 34
def load
send("load_from_#{format}".to_sym)
end
|
#marshal_dump ⇒ Object
Provides marshalling support for use by the Marshal library.
61
62
63
|
# File 'lib/alfred/setting.rb', line 61
def marshal_dump
@table
end
|
#marshal_load(x) ⇒ Object
Provides marshalling support for use by the Marshal library.
68
69
70
|
# File 'lib/alfred/setting.rb', line 68
def marshal_load(x)
@table.merge! x
end
|
#to_h ⇒ Object
74
75
76
|
# File 'lib/alfred/setting.rb', line 74
def to_h
@table.dup
end
|
#to_yaml_properties ⇒ Object
49
50
51
|
# File 'lib/alfred/setting.rb', line 49
def to_yaml_properties
[ '@table' ]
end
|
29
30
31
|
# File 'lib/alfred/setting.rb', line 29
def validate_format
['yaml'].include?(format)
end
|