squire 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,12 +1,9 @@
1
1
  # Squire
2
2
 
3
- ![alt text](https://raw.github.com/smolnar/squire/master/squire.png "Source: http://www.printactivities.com/ColoringPages/Knights/Squire.html")
4
-
5
3
  Squire is your configuration squire thats bears your configuration for object, so you don't need to worry about bunch of settings.
6
4
 
7
5
  It's designed to support multiple formats (for now, Hash and YML) and replace [Settingslogic](https://github.com/binarylogic/settingslogic) that lacks some crutial features like default/base namespace or extensibility for another source formats.
8
6
 
9
- Logo is courtesy of [PrintActivities](http://www.printactivities.com/ColoringPages/Knights/Squire.html).
10
7
 
11
8
  ## Installation
12
9
 
@@ -41,6 +38,7 @@ class Elephant
41
38
  namespace 'namespace'
42
39
  ....
43
40
  end
41
+ end
44
42
  ```
45
43
 
46
44
  After that, you can access `Elephanth.config`. For example:
@@ -107,7 +105,8 @@ Default namespace set by `base` option in `squire.namespace` is used for merging
107
105
 
108
106
  ## Statistics
109
107
 
110
- Run `bundle exec ruby benchmark/settings_access.rb`.
108
+ Run `bundle exec ruby benchmark/settings_access.rb`.
109
+ Example uses Hash with 10 000 keys.
111
110
 
112
111
  Single access:
113
112
  ```
@@ -121,6 +120,9 @@ Squire 0.090000 0.000000 0.090000 ( 0.093573)
121
120
  Settingslogic 0.060000 0.000000 0.060000 ( 0.066079)
122
121
  ```
123
122
 
123
+ First access of key in `Settingslogic` is painfully slow, as you can see. But after defining accessor, the second access
124
+ is just a matter of miliseconds.
125
+
124
126
  ## Contributing
125
127
 
126
128
  1. Fork it
@@ -49,7 +49,7 @@ module Squire
49
49
  def settings(&block)
50
50
  @settings ||= setup
51
51
 
52
- settings = @namespace ? @settings.send(@namespace) : @settings
52
+ settings = @namespace ? @settings.get_value(@namespace) : @settings
53
53
 
54
54
  if block_given?
55
55
  block.arity == 0 ? settings.instance_eval(&block) : block.call(settings)
@@ -1,6 +1,6 @@
1
1
  module Squire
2
2
  class Settings
3
- RESERVED = [:get_value, :to_hash]
3
+ RESERVED = [:get_value, :set_value, :define_key_accessor, :to_hash]
4
4
 
5
5
  ##
6
6
  # Creates new settings with +path+ and +parent+.
@@ -51,11 +51,9 @@ module Squire
51
51
  @children << settings
52
52
  end
53
53
 
54
- block.arity == 0 ? settings.instance_eval(&block) : block.call(settings)
54
+ get_value(key, &block)
55
55
  elsif args.count == 1
56
56
  set_value(key, args.pop)
57
-
58
- define_key_accessor(key)
59
57
  elsif type == '?'
60
58
  !!get_value(key)
61
59
  else
@@ -65,29 +63,41 @@ module Squire
65
63
  raise MissingSettingError.new("Missing setting in '#{key}' in '#{@path}'.")
66
64
  end
67
65
 
68
- define_key_accessor(key) unless repond_to?(key)
69
-
70
66
  value
71
67
  end
72
68
  end
73
69
 
74
70
  ##
75
71
  # Returns a value for +key+ from settings table.
72
+ # Yields +value+ of +key+ if +block+ provided.
73
+ #
74
+ # == Examples:
75
+ #
76
+ # .key do |key|
77
+ # ...
78
+ # end
76
79
  #
77
- # Goes up in ancestor chain by +parent+ until reaches a top. So if you define settings like
78
- # settings.a = 1
79
- # and ask for
80
- # settings.nested.a
81
- # it returns +settings.a+.
82
- def get_value(key)
83
- return @table[key] unless @table[key].nil?
84
- return @parent.get_value(key) if @parent
80
+ # # or
81
+ #
82
+ # .key do
83
+ # ...
84
+ # end
85
+ def get_value(key, &block)
86
+ value = @table[key]
87
+
88
+ if block_given?
89
+ block.arity == 0 ? value.instance_eval(&block) : block.call(value)
90
+ end
91
+
92
+ value
85
93
  end
86
94
 
87
95
  ##
88
96
  # Sets a +value+ for +key+
89
97
  def set_value(key, value)
90
98
  @table[key] = value
99
+
100
+ define_key_accessor(key)
91
101
  end
92
102
 
93
103
  ##
@@ -127,7 +137,9 @@ module Squire
127
137
  ##
128
138
  # Defines key accessor for +key+ for faster accessing of keys.
129
139
  def define_key_accessor(key)
130
- define_singleton_method(key) { get_value(key) } if key =~ /\A\w+\z/
140
+ define_singleton_method(key) do |&block|
141
+ get_value(key, &block)
142
+ end
131
143
  end
132
144
  end
133
145
  end
@@ -1,3 +1,3 @@
1
1
  module Squire
2
- VERSION = '1.2.1'
2
+ VERSION = '1.2.2'
3
3
  end
@@ -7,6 +7,10 @@ end
7
7
  describe Squire::Base do
8
8
  subject { Configuration }
9
9
 
10
+ before :each do
11
+ subject.squire.reload!
12
+ end
13
+
10
14
  it 'should properly setup base configuration' do
11
15
  subject.squire.source development: { a: 1, b: 2 }
12
16
  subject.squire.namespace :development
@@ -25,4 +29,11 @@ describe Squire::Base do
25
29
 
26
30
  expect { subject.d }.to raise_error(Squire::MissingSettingError)
27
31
  end
32
+
33
+ it 'should properly delegate keys' do
34
+ subject.squire.source test: { a: 1, b: 2 }
35
+ subject.squire.namespace :test
36
+
37
+ subject.a.should eql(1)
38
+ end
28
39
  end
@@ -28,16 +28,6 @@ describe Squire::Settings do
28
28
  config.nested.b.should eql(2)
29
29
  end
30
30
 
31
- it 'should fallback to parent configuration if not defined' do
32
- config = subject.new
33
-
34
- config.global = 1
35
-
36
- config.nested { |nested| nested.a = 1 }
37
-
38
- config.nested.global.should eql(1)
39
- end
40
-
41
31
  it 'should check if the value is set' do
42
32
  config = subject.new
43
33
 
@@ -48,7 +38,7 @@ describe Squire::Settings do
48
38
 
49
39
  config.a?.should be_true
50
40
  config.nested.b?.should be_true
51
- config.nested.a?.should be_true
41
+ config.nested.a?.should be_false
52
42
  config.global?.should be_false
53
43
  end
54
44
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squire
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-30 00:00:00.000000000 Z
12
+ date: 2013-08-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -181,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
181
  version: '0'
182
182
  requirements: []
183
183
  rubyforge_project:
184
- rubygems_version: 1.8.24
184
+ rubygems_version: 1.8.25
185
185
  signing_key:
186
186
  specification_version: 3
187
187
  summary: Squire handles your configuration per class/file by common config DSL and