stephencelis-kvc 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,15 @@
1
+ === 0.0.3 / 2009-06-27
2
+
3
+ * 1 minor enhancement
4
+
5
+ * Optimize by relying less on method_missing.
6
+
7
+
8
+ * 1 bugfix
9
+
10
+ * Don't confuse strings and symbols.
11
+
12
+
1
13
  === 0.0.2 / 2009-04-04
2
14
 
3
15
  * 1 minor enhancement
data/lib/kvc.rb CHANGED
@@ -35,30 +35,32 @@
35
35
  #
36
36
  # Failed validations will raise <tt>ActiveRecord::RecordInvalid</tt>.
37
37
  module KVC
38
- VERSION = "0.0.2"
38
+ VERSION = "0.0.3"
39
39
 
40
40
  class << self
41
+ def [](key)
42
+ if setting = KVC::Settings.find_by_key(key.to_s)
43
+ KVC::SettingsProxy.new setting
44
+ elsif KVC::Settings.strict_keys?
45
+ raise NoMethodError,
46
+ "undefined method `#{key}' for #{self}:#{self.class}"
47
+ end
48
+ end
49
+
50
+ def []=(key, value)
51
+ setting = KVC::Settings.find_or_initialize_by_key key.to_s
52
+ setting.update_attributes! :value => value
53
+ end
54
+
41
55
  private
42
56
 
43
57
  # Handles the key-value magic.
44
58
  def method_missing(method, *args, &block)
45
59
  key = method.to_s
46
- key.sub!(/^\[\](=?)$/) { "#{args.shift}#{$1}" }
47
-
48
60
  if key.sub!(/=$/) {} # Is it a writer method?
49
- returning args.shift do |value|
50
- setting = KVC::Settings.find_or_initialize_by_key(key)
51
- setting.update_attributes!(:value => value)
52
- end
53
- elsif setting = KVC::Settings.find_by_key(key) # Is it a reader?
54
- if args.present?
55
- error_message = "wrong number of arguments (#{args.length} for 0)"
56
- raise ArgumentError, error_message
57
- end
58
-
59
- KVC::SettingsProxy.new(setting)
60
- elsif args.present? || KVC::Settings.strict_keys?
61
- raise NoMethodError
61
+ self[key] = *args
62
+ else
63
+ self[key, *args]
62
64
  end
63
65
  end
64
66
  end
data/lib/kvc/settings.rb CHANGED
@@ -43,14 +43,12 @@ class KVC::Settings < ActiveRecord::Base
43
43
 
44
44
  # Deserializes value from database.
45
45
  def value
46
- @value ||= YAML.load(read_attribute(:value))
46
+ @value ||= YAML.load read_attribute(:value)
47
47
  end
48
48
 
49
49
  # Serializes value for database.
50
50
  def value=(input)
51
- returning @value = input do
52
- write_attribute :value, input.to_yaml
53
- end
51
+ write_attribute :value, (@value = input).to_yaml
54
52
  end
55
53
 
56
54
  private
data/test/kvc_test.rb CHANGED
@@ -26,7 +26,7 @@ class KVCTest < ActiveSupport::TestCase
26
26
  end
27
27
 
28
28
  test "method_missing should re-raise if there are arguments" do
29
- assert_raise NoMethodError do
29
+ assert_raise ArgumentError do
30
30
  KVC.this_should("not_work")
31
31
  end
32
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stephencelis-kvc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Celis
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-04 00:00:00 -07:00
12
+ date: 2009-06-27 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.11.0
23
+ version: 2.0.0
24
24
  version:
25
25
  description: KVC (Key-Value Configuration) provides a powerful, transparent way to maintain mutable app settings in the database.
26
26
  email:
@@ -44,7 +44,7 @@ files:
44
44
  - tasks/kvc_tasks.rake
45
45
  - test/kvc_test.rb
46
46
  - uninstall.rb
47
- has_rdoc: true
47
+ has_rdoc: false
48
48
  homepage: http://github.com/stephencelis/kvc
49
49
  post_install_message:
50
50
  rdoc_options:
@@ -69,7 +69,7 @@ requirements: []
69
69
  rubyforge_project: kvc
70
70
  rubygems_version: 1.2.0
71
71
  signing_key:
72
- specification_version: 2
72
+ specification_version: 3
73
73
  summary: KVC (Key-Value Configuration) provides a powerful, transparent way to maintain mutable app settings in the database.
74
74
  test_files: []
75
75