sugar-high 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -36,6 +36,9 @@ h2. Sugar packs
36
36
  * properties
37
37
  * reg_exp
38
38
 
39
+ * rails
40
+ * concerns
41
+
39
42
  h3. Alias
40
43
 
41
44
  * multi_alias name, options_hash : creates multiple aliases using patterns
@@ -242,6 +245,25 @@ h3. RSpec 2 Matchers
242
245
  have_aliases :original, :alias_1, :alias2
243
246
  </pre>
244
247
 
248
+ h3. Rails
249
+
250
+ Currently the following Rails packs are included:
251
+
252
+ * concerns
253
+
254
+ More to follow in the future...
255
+
256
+ h4. Concerns
257
+
258
+ @require 'rails/concerns'@
259
+
260
+ Useful concern pattern in order to include concern modules into Rails models (or even controllers etc.)
261
+
262
+ * concerned_with *concern_modules
263
+ * include_concerns *concern_modules
264
+ * shared_concerns *concern_modules
265
+ * include_shared_concerns *concern_modules
266
+
245
267
  h3. String
246
268
 
247
269
  Class methods
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.6.1
@@ -1,6 +1,15 @@
1
1
  require 'sugar-high/arguments'
2
2
 
3
3
  class Hash
4
+
5
+ # http://www.dweebd.com/ruby/hash-key-rewrite/
6
+ def rewrite mapping
7
+ inject({}) do |rewritten_hash, (original_key, value)|
8
+ rewritten_hash[mapping.fetch(original_key, original_key)] = value
9
+ rewritten_hash
10
+ end
11
+ end
12
+
4
13
  def hash_revert
5
14
  r = Hash.new {|h,k| h[k] = []}
6
15
  each {|k,v| r[v] << k}
@@ -1,29 +1,78 @@
1
1
  class Module
2
2
  def concerned_with(*concerns)
3
- concerns.each do |concern|
4
- require_method "#{name.underscore}/#{concern}"
3
+ options = concerns.extract_options!
4
+ concerns.flatten.each do |concern|
5
+ next if concern.blank?
6
+ require_concern name, concern
5
7
  end
8
+ shared_concerns([options[:shared]].flatten.compact)
9
+ end
10
+
11
+ def include_concerns(*concerns)
12
+ options = concerns.extract_options!
13
+ concerns.flatten.each do |concern|
14
+ next if concern.blank?
15
+ require_concern name, concern
16
+ concern_ns = [name, concern.to_s.camelize].join('::')
17
+ self.send :include, concern_ns.constantize
18
+ begin
19
+ self.extend [concern_ns, 'ClassMethods'].join('::').constantize
20
+ rescue
21
+ end
22
+ end
23
+ include_shared_concerns([options[:shared]].flatten.compact)
6
24
  end
7
25
 
8
26
  def shared_concerns(*concerns)
9
- concerns.each do |concern|
10
- require_method "shared/#{concern}"
27
+ concerns.flatten.each do |concern|
28
+ next if concern.blank?
29
+ require_shared concern
11
30
  end
12
31
  end
13
32
 
14
33
  def include_shared_concerns(*concerns)
15
- concerns.each do |concern|
16
- require_method "shared/#{concern}"
17
- self.send :include, concern.to_s.camelize.constantize
34
+ concerns.flatten.each do |concern|
35
+ next if concern.blank?
36
+ require_shared concern
37
+ concern_ns = concern.to_s.camelize
38
+
39
+ self.send :include, shared_const(concern_ns)
40
+
41
+ begin
42
+ self.extend [concern_ns, 'ClassMethods'].join('::').constantize
43
+ rescue
44
+ end
18
45
  end
19
46
  end
20
47
 
21
48
  alias_method :shared_concern, :shared_concerns
49
+ alias_method :include_concern, :include_concerns
22
50
  alias_method :include_shared_concern, :include_shared_concerns
23
51
 
24
52
  protected
25
53
 
54
+ def require_concern nane, concern
55
+ require_method "#{name.underscore}/#{concern.to_s.underscore}"
56
+ end
57
+
58
+ def require_shared concern
59
+ require_method "shared/#{concern.to_s.underscore}"
60
+ end
61
+
62
+ def shared_const concern_ns
63
+ concern_ns.constantize
64
+ rescue NameError
65
+ shared_ns_const concern_ns
66
+ end
67
+
68
+ def shared_ns_const concern_ns
69
+ const_name = "Shared::#{concern_ns}"
70
+ const_name.constantize
71
+ rescue NameError
72
+ raise "No module could be found for: #{concern_ns} or #{const_name}"
73
+ end
74
+
26
75
  def require_method path
27
76
  defined?(require_dependency) ? require_dependency(path) : require(path)
28
77
  end
29
- end
78
+ end
@@ -3,6 +3,13 @@ require 'sugar-high/hash'
3
3
 
4
4
  describe "SugarHigh" do
5
5
  describe "Hash packet" do
6
+ describe '#rewrite' do
7
+ it 'should rewrite keys of the hash' do
8
+ mapping = {:a => :new_a, :b => :new_b}
9
+ {:a => 'hello', :b => 'hi', :c => 'hi'}.rewrite(mapping).should == {:new_a => 'hello', :new_b => 'hi', :c => 'hi'}
10
+ end
11
+ end
12
+
6
13
  describe '#hash_revert' do
7
14
  it "should revert hash" do
8
15
  {:a => 'hello', :b => 'hi', :c => 'hi'}.hash_revert.should == {'hello' => [:a], 'hi' => [:b, :c]}
data/sugar-high.gemspec CHANGED
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{sugar-high}
8
- s.version = "0.6.0"
7
+ s.name = "sugar-high"
8
+ s.version = "0.6.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{Kristian Mandrup}]
12
- s.date = %q{2011-08-21}
13
- s.description = %q{More Ruby sugar - inspired by the 'zuker' project}
14
- s.email = %q{kmandrup@gmail.com}
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = "2011-12-06"
13
+ s.description = "More Ruby sugar - inspired by the 'zuker' project"
14
+ s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
17
  "README.textile"
@@ -78,10 +78,10 @@ Gem::Specification.new do |s|
78
78
  "spec/sugar-high/string_spec.rb",
79
79
  "sugar-high.gemspec"
80
80
  ]
81
- s.homepage = %q{http://github.com/kristianmandrup/sugar-high}
82
- s.require_paths = [%q{lib}]
83
- s.rubygems_version = %q{1.8.8}
84
- s.summary = %q{Ruby convenience sugar packs!}
81
+ s.homepage = "http://github.com/kristianmandrup/sugar-high"
82
+ s.require_paths = ["lib"]
83
+ s.rubygems_version = "1.8.10"
84
+ s.summary = "Ruby convenience sugar packs!"
85
85
 
86
86
  if s.respond_to? :specification_version then
87
87
  s.specification_version = 3
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugar-high
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-21 00:00:00.000000000Z
12
+ date: 2011-12-06 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &2155469300 !ruby/object:Gem::Requirement
16
+ requirement: &899493810 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2155469300
24
+ version_requirements: *899493810
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2155468820 !ruby/object:Gem::Requirement
27
+ requirement: &899450620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.4.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2155468820
35
+ version_requirements: *899450620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rails
38
- requirement: &2155468340 !ruby/object:Gem::Requirement
38
+ requirement: &899449950 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 3.0.1
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2155468340
46
+ version_requirements: *899449950
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &2155467860 !ruby/object:Gem::Requirement
49
+ requirement: &899449180 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.6.4
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2155467860
57
+ version_requirements: *899449180
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bundler
60
- requirement: &2155467380 !ruby/object:Gem::Requirement
60
+ requirement: &899448420 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.0.1
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2155467380
68
+ version_requirements: *899448420
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &2155466900 !ruby/object:Gem::Requirement
71
+ requirement: &899447800 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2155466900
79
+ version_requirements: *899447800
80
80
  description: More Ruby sugar - inspired by the 'zuker' project
81
81
  email: kmandrup@gmail.com
82
82
  executables: []
@@ -159,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
159
  version: '0'
160
160
  segments:
161
161
  - 0
162
- hash: -2156933571915273099
162
+ hash: 595246579
163
163
  required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  none: false
165
165
  requirements:
@@ -168,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
168
  version: '0'
169
169
  requirements: []
170
170
  rubyforge_project:
171
- rubygems_version: 1.8.8
171
+ rubygems_version: 1.8.10
172
172
  signing_key:
173
173
  specification_version: 3
174
174
  summary: Ruby convenience sugar packs!