jewel 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -42,13 +42,13 @@ class << Jewel::Gem
42
42
  # @return [String] the gem root as an absolute path
43
43
  # @since 0.0.2
44
44
  def root(relative_to = nil)
45
- # caller returns an array of strings that are like “file:line” or “file:line: in `method’”
46
- file = caller.first.split(/:/).first
47
45
  arguments = []
48
46
  unless relative_to.nil?
49
47
  relative_to = relative_to.to_s
50
- file = File.dirname file
51
- arguments << File.expand_path(relative_to, file)
48
+ # caller returns an array of strings that are like “file:line” or “file:line: in `method’”
49
+ file = caller.first.split(/:/).first
50
+ directory = File.dirname file
51
+ arguments << File.expand_path(relative_to, directory)
52
52
  end
53
53
  metadata.send :root, *arguments
54
54
  end
@@ -59,7 +59,7 @@ class << Jewel::Gem
59
59
  # added instead.
60
60
  #
61
61
  # @param [String, Symbol, #to_s] gem the name of the gem
62
- # @param [String, Symbol, #to_s] version the version of the gem
62
+ # @param [Array<String>] requirements the version requirements
63
63
  # @see development
64
64
  # @example
65
65
  # depend_on :jewel # runtime dependency
@@ -67,10 +67,9 @@ class << Jewel::Gem
67
67
  # development do
68
68
  # depend_on :rspec # development dependency
69
69
  # end
70
- def depend_on(gem, version = nil)
71
- metadata.send(if development?
72
- :development_dependencies
73
- else :dependencies end).merge! gem => version
70
+ def depend_on(gem, *requirements)
71
+ method = development? ? :add_development_dependency : :add_dependency
72
+ specification.send method, gem.to_s, *requirements
74
73
  end
75
74
 
76
75
  # Makes sure the correct versions of this gem's dependencies are loaded at
@@ -80,8 +79,8 @@ class << Jewel::Gem
80
79
  # dependencies should be activated
81
80
  # @since 0.0.2
82
81
  def activate_dependencies!(options = {})
83
- metadata.each_dependency options do |name, version|
84
- gem name, version unless version.nil?
82
+ metadata.each_dependency options do |name, *requirements|
83
+ gem name, *requirements unless requirements.empty?
85
84
  end
86
85
  end
87
86
 
@@ -101,16 +100,18 @@ class << Jewel::Gem
101
100
  @development = false
102
101
  end
103
102
 
104
- # Returns this gem's specification.
103
+ # Returns this gem's specification. If passed an existing instance, it will be
104
+ # stored as this gem's specification.
105
105
  #
106
+ # @param [::Gem::Specification] existing_specification the existing instance
106
107
  # @return [::Gem::Specification] the gem specification
107
- # @see Jewel::Gem::Metadata#to_spec
108
- def spec
109
- metadata.to_spec
108
+ # @see Jewel::Gem::Metadata#gem_specification
109
+ def specification(existing_specification = nil)
110
+ metadata.gem_specification existing_specification
110
111
  end
111
112
 
112
- alias gemspec spec
113
- alias to_spec spec
113
+ alias spec specification
114
+ alias to_spec specification
114
115
 
115
116
  private
116
117
 
@@ -128,7 +129,7 @@ class Jewel::Gem
128
129
 
129
130
  name! :jewel
130
131
  summary 'Easy access to gem metadata'
131
- version '0.0.3'
132
+ version '0.0.4'
132
133
  homepage 'https://github.com/matheusmoreira/jewel'
133
134
  license 'Mozilla Public License, version 2.0'
134
135
 
@@ -142,6 +143,7 @@ class Jewel::Gem
142
143
  depend_on :bundler
143
144
  depend_on :redcarpet # yard uses it for markdown formatting
144
145
  depend_on :rookie
146
+ depend_on :rspec
145
147
  depend_on :yard
146
148
  end
147
149
 
@@ -17,33 +17,52 @@ module Jewel
17
17
  instance_eval &block unless block.nil?
18
18
  end
19
19
 
20
- # Associates the name of the missing method with the given arguments.
20
+ # Assigns or returns attributes from the associated
21
+ # {#gem_specification gem specification}. If it doesn't respond to it, the
22
+ # attribute will be stored in this object.
21
23
  def method_missing(method_name, *arguments, &block)
22
24
  method = method_name.to_s.gsub(/[=?!\z]/ix, '').strip.intern
23
25
  count = arguments.count
24
- return stored_attributes[method] if count.zero?
25
- value = count == 1 ? arguments.first : arguments
26
- stored_attributes[method] = value
26
+ if count.zero?
27
+ if spec.respond_to? method
28
+ spec.send method
29
+ else
30
+ stored_attributes[method]
31
+ end
32
+ else
33
+ writer_method = '='.prepend(method.to_s).intern
34
+ if spec.respond_to? writer_method
35
+ spec.send writer_method, *arguments
36
+ else
37
+ value = count == 1 ? arguments.first : arguments
38
+ stored_attributes[method] = value
39
+ end
40
+ end
27
41
  end
28
42
 
29
43
  # This gem's runtime dependencies.
30
44
  #
31
- # @return [Hash] name => version pairs
45
+ # @return [Hash<String, Array<String>>] names of gems associated with
46
+ # their requirements
32
47
  def dependencies
33
- @dependencies ||= {}
48
+ convert_to_hash specification.runtime_dependencies
34
49
  end
35
50
 
51
+ alias runtime_dependencies dependencies
52
+
36
53
  # This gem's development dependencies.
37
54
  #
38
- # @return [Hash] name => version pairs
55
+ # @return [Hash<String, Array<String>>] names of gems associated with
56
+ # their requirements
39
57
  def development_dependencies
40
- @development_dependencies ||= {}
58
+ convert_to_hash specification.development_dependencies
41
59
  end
42
60
 
43
- # Runtime and development dependencies. The former takes precedence,
44
- # should version requirements conflict.
61
+ # Runtime and development dependencies. The former takes precedence in
62
+ # case of conflict.
45
63
  #
46
- # @return [Hash] name => version pairs
64
+ # @return [Hash<String, Array<String>>] names of gems associated with
65
+ # their requirements
47
66
  # @since 0.0.2
48
67
  def all_dependencies
49
68
  development_dependencies.merge dependencies
@@ -53,47 +72,39 @@ module Jewel
53
72
  #
54
73
  # @option options [true, false, :only] :development (false) whether
55
74
  # runtime and development dependencies should be included
75
+ # @yield [gem_name, *requirements]
56
76
  # @yieldparam [String] gem_name the name of the gem
57
- # @yieldparam [String, nil] version the required version
77
+ # @yieldparam [Array<String>] requirements the version requirements
58
78
  # @since 0.0.2
59
- def each_dependency(options = {})
79
+ def each_dependency(options = {}, &block)
60
80
  return enum_for :each_dependency, options unless block_given?
61
81
  development = options.fetch :development, false
62
82
  case development
63
83
  when :only then development_dependencies
64
84
  when true then all_dependencies
65
85
  else dependencies
66
- end.each do |gem_name, version|
67
- yield gem_name.to_s, version
68
- end
86
+ end.each &block
69
87
  end
70
88
 
71
- # Uses the stored information to generate a ::Gem::Specification.
89
+ # The Gem::Specification. If passed an existing instance, it will be
90
+ # used instead.
72
91
  #
92
+ # @param [::Gem::Specification] existing_specification the existing
93
+ # instance
73
94
  # @return [::Gem::Specification] the specification
74
- def to_spec
75
- ::Gem::Specification.new do |gem_specification|
76
-
77
- # Set all attributes
78
- stored_attributes.keys.each do |key|
79
- writer_method = '='.prepend(key.to_s).intern
80
- if gem_specification.respond_to? writer_method
81
- gem_specification.send writer_method, stored_attributes[key]
82
- end
83
- end
84
-
85
- # Add the dependencies
86
- {
87
- :add_runtime_dependency => false,
88
- :add_development_dependency => :only
89
- }.each do |method, option|
90
- each_dependency development: option do |*arguments|
91
- gem_specification.send method, *arguments.compact
92
- end
93
- end
95
+ # @since 0.0.4
96
+ def gem_specification(existing_specification = nil)
97
+ unless existing_specification.nil?
98
+ @gem_specification = existing_specification
94
99
  end
100
+ @gem_specification ||= ::Gem::Specification.new
95
101
  end
96
102
 
103
+ alias spec gem_specification
104
+ alias gemspec gem_specification
105
+ alias to_spec gem_specification
106
+ alias specification gem_specification
107
+
97
108
  private
98
109
 
99
110
  # The stored gem attributes.
@@ -103,6 +114,22 @@ module Jewel
103
114
  @stored_attributes ||= {}
104
115
  end
105
116
 
117
+ # Converts an array of Gem::Dependency objects into an array of hashes in
118
+ # the following form:
119
+ #
120
+ # { 'gem_name' => ['= 0.0.0'], ... }
121
+ #
122
+ # @return [Hash<String, Array<String>>] names of gems associated with
123
+ # their requirements
124
+ # @since 0.0.4
125
+ def convert_to_hash(dependencies)
126
+ Hash.new.tap do |hash|
127
+ dependencies.each do |dependency|
128
+ hash[dependency.name] = dependency.requirements_list
129
+ end
130
+ end
131
+ end
132
+
106
133
  end
107
134
 
108
135
  end
@@ -0,0 +1,65 @@
1
+ require 'jewel'
2
+
3
+ describe Jewel::Gem do
4
+
5
+ let(:example_name) { :example }
6
+ let(:example_summary) { 'Example description' }
7
+ let(:example_version) { '1.2.3' }
8
+ let(:example_homepage) { 'https://github.com/example/example' }
9
+ let(:example_license) { 'Mozilla Public License, version 2.0' }
10
+
11
+ let(:example_author) { 'Person' }
12
+ let(:example_email) { 'person@example.com' }
13
+
14
+ let(:example_files) { [] }
15
+
16
+ let(:example_runtime_dependency) { 'jewel' }
17
+ let(:example_development_dependency) { 'rookie' }
18
+
19
+ let :gem do
20
+ Class.new(Jewel::Gem).tap do |gem_class|
21
+ gem_class.name! example_name
22
+ gem_class.summary example_summary
23
+ gem_class.version example_version
24
+ gem_class.homepage example_homepage
25
+ gem_class.license example_license
26
+
27
+ gem_class.author example_author
28
+ gem_class.email example_email
29
+
30
+ gem_class.files example_files
31
+
32
+ gem_class.depend_on example_runtime_dependency
33
+
34
+ gem_class.development do
35
+ gem_class.depend_on example_development_dependency
36
+ end
37
+ end
38
+ end
39
+
40
+ subject { gem }
41
+
42
+ let :hand_written_gem_specification do
43
+ ::Gem::Specification.new 'jewel' do |gem|
44
+ gem.name = example_name.to_s
45
+ gem.summary = example_summary
46
+ gem.version = example_version
47
+ gem.homepage = example_homepage
48
+ gem.license = example_license
49
+
50
+ gem.author = example_author
51
+ gem.email = example_email
52
+
53
+ gem.files = example_files
54
+
55
+ gem.add_runtime_dependency example_runtime_dependency
56
+
57
+ gem.add_development_dependency example_development_dependency
58
+ end
59
+ end
60
+
61
+ it "'s gem specification should be equivalent to the hand-written one" do
62
+ subject.specification.should == hand_written_gem_specification
63
+ end
64
+
65
+ end
metadata CHANGED
@@ -1,15 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jewel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 4
5
9
  prerelease:
10
+ hash: -3852523161477996427
6
11
  platform: ruby
7
12
  authors:
8
13
  - Matheus Afonso Martins Moreira
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-06-06 00:00:00.000000000 Z
17
+ date: 2012-06-07 00:00:00.000000000 Z
13
18
  dependencies:
14
19
  - !ruby/object:Gem::Dependency
15
20
  name: bundler
@@ -19,6 +24,9 @@ dependencies:
19
24
  - - ! '>='
20
25
  - !ruby/object:Gem::Version
21
26
  version: '0'
27
+ segments:
28
+ - 0
29
+ hash: -4447801726355984837
22
30
  type: :development
23
31
  prerelease: false
24
32
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,6 +35,9 @@ dependencies:
27
35
  - - ! '>='
28
36
  - !ruby/object:Gem::Version
29
37
  version: '0'
38
+ segments:
39
+ - 0
40
+ hash: -4447801726355984837
30
41
  - !ruby/object:Gem::Dependency
31
42
  name: redcarpet
32
43
  requirement: !ruby/object:Gem::Requirement
@@ -35,6 +46,9 @@ dependencies:
35
46
  - - ! '>='
36
47
  - !ruby/object:Gem::Version
37
48
  version: '0'
49
+ segments:
50
+ - 0
51
+ hash: -4447801726355984837
38
52
  type: :development
39
53
  prerelease: false
40
54
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,6 +57,9 @@ dependencies:
43
57
  - - ! '>='
44
58
  - !ruby/object:Gem::Version
45
59
  version: '0'
60
+ segments:
61
+ - 0
62
+ hash: -4447801726355984837
46
63
  - !ruby/object:Gem::Dependency
47
64
  name: rookie
48
65
  requirement: !ruby/object:Gem::Requirement
@@ -51,6 +68,9 @@ dependencies:
51
68
  - - ! '>='
52
69
  - !ruby/object:Gem::Version
53
70
  version: '0'
71
+ segments:
72
+ - 0
73
+ hash: -4447801726355984837
54
74
  type: :development
55
75
  prerelease: false
56
76
  version_requirements: !ruby/object:Gem::Requirement
@@ -59,6 +79,31 @@ dependencies:
59
79
  - - ! '>='
60
80
  - !ruby/object:Gem::Version
61
81
  version: '0'
82
+ segments:
83
+ - 0
84
+ hash: -4447801726355984837
85
+ - !ruby/object:Gem::Dependency
86
+ name: rspec
87
+ requirement: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ segments:
94
+ - 0
95
+ hash: -4447801726355984837
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ segments:
105
+ - 0
106
+ hash: -4447801726355984837
62
107
  - !ruby/object:Gem::Dependency
63
108
  name: yard
64
109
  requirement: !ruby/object:Gem::Requirement
@@ -67,6 +112,9 @@ dependencies:
67
112
  - - ! '>='
68
113
  - !ruby/object:Gem::Version
69
114
  version: '0'
115
+ segments:
116
+ - 0
117
+ hash: -4447801726355984837
70
118
  type: :development
71
119
  prerelease: false
72
120
  version_requirements: !ruby/object:Gem::Requirement
@@ -75,6 +123,9 @@ dependencies:
75
123
  - - ! '>='
76
124
  - !ruby/object:Gem::Version
77
125
  version: '0'
126
+ segments:
127
+ - 0
128
+ hash: -4447801726355984837
78
129
  description:
79
130
  email: matheus.a.m.moreira@gmail.com
80
131
  executables: []
@@ -91,6 +142,7 @@ files:
91
142
  - lib/jewel.rb
92
143
  - lib/jewel/gem.rb
93
144
  - lib/jewel/gem/metadata.rb
145
+ - spec/jewel/gem_spec.rb
94
146
  homepage: https://github.com/matheusmoreira/jewel
95
147
  licenses:
96
148
  - Mozilla Public License, version 2.0
@@ -106,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
158
  version: '0'
107
159
  segments:
108
160
  - 0
109
- hash: 4515205193521416105
161
+ hash: -4447801726355984837
110
162
  required_rubygems_version: !ruby/object:Gem::Requirement
111
163
  none: false
112
164
  requirements:
@@ -115,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
167
  version: '0'
116
168
  segments:
117
169
  - 0
118
- hash: 4515205193521416105
170
+ hash: -4447801726355984837
119
171
  requirements: []
120
172
  rubyforge_project:
121
173
  rubygems_version: 1.8.24