named 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,53 @@
1
1
  # Named
2
2
 
3
- TODO: Write a gem description
3
+ Give anonymous modules and classes names when they are inspected.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/adamhunter/named.png?branch=master)](http://travis-ci.org/adamhunter/named)
6
+ [![Code Climate](https://codeclimate.com/github/adamhunter/named.png)](https://codeclimate.com/github/adamhunter/named)
7
+
8
+ ## Usage
9
+
10
+ ### Named modules
11
+
12
+ ```ruby
13
+ fancy_module = Named::Module.new('FancyModule') do
14
+ def module_method
15
+ puts 'Hello from the module'
16
+ end
17
+ end
18
+
19
+ fancy_module.inspect # => "Named::Module:FancyModule:0x7fa9ea9e6178"
20
+ ```
21
+
22
+ ### Named classes
23
+
24
+ ```ruby
25
+ # subclasses Array
26
+ fancy_class = Named::Class.new('FancyClass', Array) do
27
+ def class_method
28
+ puts 'Hello from the class'
29
+ end
30
+ end
31
+
32
+ # Inspection string shows the superclass
33
+ fancy_class.inspect => "Named::Class:Array:FancyClass:0x7fa9ea8aaea8"
34
+ ```
35
+
36
+ ### Using them together
37
+ ```ruby
38
+ fancy_class.send(:include, fancy_module)
39
+
40
+ fancy_class.ancestors # => [Named::Class:Array:FancyClass:0x7fa9ea8aaea8,
41
+ # Named::Module:FancyModule:0x7fa9ea9e6178,
42
+ # Named::Class, Named::Naming,
43
+ # Array, Kernel, BasicObject]
44
+
45
+ instance = fancy_class.new
46
+
47
+ instance.module_method # => 'Hello from the module'
48
+ instance.class_method # => 'Hello from the class'
49
+
50
+ ```
4
51
 
5
52
  ## Installation
6
53
 
@@ -16,10 +63,6 @@ Or install it yourself as:
16
63
 
17
64
  $ gem install named
18
65
 
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
22
-
23
66
  ## Contributing
24
67
 
25
68
  1. Fork it
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -2,6 +2,7 @@ module Named
2
2
  end
3
3
 
4
4
  require "named/version"
5
+ require 'named/inspection'
5
6
  require 'named/naming'
6
7
  require 'named/class'
7
8
  require 'named/module'
@@ -1,26 +1,27 @@
1
1
  module Named
2
2
  module Class
3
- include Naming
4
-
5
3
  def self.new(name, superclass = Object, &block)
6
4
  ::Class.new(superclass, &block).tap do |klass|
5
+ klass.extend(self) # adds support for is_a?
7
6
  klass.extend(ClassMethods)
8
- klass.send(:include, self)
7
+ klass.send(:include, InstanceMethods)
9
8
  klass.name = name
10
9
  end
11
10
  end
12
11
 
13
12
  module ClassMethods
14
- include Naming
13
+ include Inspection, Naming
15
14
 
16
15
  def to_s
17
16
  [Named::Class, superclass, name, inspect_object_id].join(':')
18
17
  end
19
18
  end
20
19
 
21
- # implementation from: http://stackoverflow.com/a/2818916/4376
22
- def to_s
23
- "#<#{self.class}:#{inspect_object_id}>".sub(":#{self.class.inspect_object_id}", '')
20
+ module InstanceMethods
21
+ include Inspection
22
+ def to_s
23
+ "#<#{self.class}:#{inspect_object_id}>".sub(":#{self.class.inspect_object_id}", '')
24
+ end
24
25
  end
25
26
  end
26
27
  end
@@ -0,0 +1,8 @@
1
+ module Named
2
+ module Inspection
3
+ # implementation from: http://stackoverflow.com/a/2818916/4376
4
+ def inspect_object_id
5
+ "0x%x" % (object_id << 1)
6
+ end
7
+ end
8
+ end
@@ -1,6 +1,6 @@
1
1
  module Named
2
2
  class Module < ::Module
3
- include Naming
3
+ include Inspection, Naming
4
4
 
5
5
  def initialize(name, &block)
6
6
  super(&block)
@@ -1,9 +1,5 @@
1
1
  module Named
2
2
  module Naming
3
3
  attr_accessor :name
4
-
5
- def inspect_object_id
6
- "0x00%x" % (object_id << 1)
7
- end
8
4
  end
9
5
  end
@@ -1,3 +1,3 @@
1
1
  module Named
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rspec", "~> 2.13"
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rspec", "~> 2.13"
23
+ spec.add_development_dependency "simplecov", "~> 0.7"
23
24
  spec.add_development_dependency "rake"
24
25
  end
@@ -1,7 +1,73 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Named::Class do
4
+ let(:subject) { Named::Class.new("Spec Class") { attr_accessor :foo } }
5
+
4
6
  it "is a module" do
5
7
  expect(Named::Class).to be_a(Module)
6
8
  end
9
+
10
+ it "is a Named::Class" do
11
+ expect(subject).to be_a(Named::Class)
12
+ end
13
+
14
+ describe "class creation" do
15
+ it "has the provided code block as its body" do
16
+ expect(subject.new).to respond_to(:foo)
17
+ end
18
+ end
19
+
20
+ describe "classes" do
21
+ describe "to_s" do
22
+ it "starts with Named::Class" do
23
+ expect(subject.to_s).to start_with 'Named::Class'
24
+ end
25
+
26
+ it "includes the given module name" do
27
+ expect(subject.to_s).to include("Spec Class")
28
+ end
29
+
30
+ it "ends with the properly formatted object id" do
31
+ expect(subject.to_s).to end_with(subject.inspect_object_id)
32
+ end
33
+ end
34
+
35
+ describe "inspect" do
36
+ it "is the same as to_s" do
37
+ expect(subject.to_s).to eq subject.inspect
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "instances" do
43
+ let(:instance) { subject.new }
44
+
45
+ it "is not a Named::Class" do
46
+ expect(instance).not_to be_a(Named::Class)
47
+ end
48
+
49
+ it "does not have a name accessor" do
50
+ expect(instance).not_to respond_to(:name)
51
+ end
52
+
53
+ describe "to_s" do
54
+ it "starts with Named::Class" do
55
+ expect(instance.to_s).to start_with '#<Named::Class'
56
+ end
57
+
58
+ it "includes the given module name" do
59
+ expect(instance.to_s).to include("Spec Class")
60
+ end
61
+
62
+ it "ends with the properly formatted object id" do
63
+ expect(instance.to_s).to end_with("#{instance.inspect_object_id}>")
64
+ end
65
+ end
66
+
67
+ describe "inspect" do
68
+ it "is the same as to_s" do
69
+ expect(instance.to_s).to eq instance.inspect
70
+ end
71
+ end
72
+ end
7
73
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Named::Inspection do
4
+ let(:subject) { Class.new.extend(described_class) }
5
+
6
+ it "properly formats object ids" do
7
+ _, hex_object_id = subject.inspect_object_id.split('x')
8
+ expect(subject.inspect).to include hex_object_id
9
+ end
10
+ end
@@ -1,7 +1,38 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Named::Module do
4
+
5
+ let(:subject) { Named::Module.new("Spec Module") { attr_accessor :foo } }
6
+
4
7
  it "is a class" do
5
8
  expect(Named::Module).to be_a(Class)
6
9
  end
10
+
11
+ describe "module creation" do
12
+ before(:each) { subject.extend(subject) }
13
+
14
+ it "has the provided code block as its body" do
15
+ expect(subject).to respond_to(:foo)
16
+ end
17
+ end
18
+
19
+ describe "to_s" do
20
+ it "starts with Named::Module" do
21
+ expect(subject.to_s).to start_with 'Named::Module'
22
+ end
23
+
24
+ it "includes the given module name" do
25
+ expect(subject.to_s).to include("Spec Module")
26
+ end
27
+
28
+ it "ends with the properly formatted object id" do
29
+ expect(subject.to_s).to end_with(subject.inspect_object_id)
30
+ end
31
+ end
32
+
33
+ describe "inspect" do
34
+ it "is the same as to_s" do
35
+ expect(subject.to_s).to eq subject.inspect
36
+ end
37
+ end
7
38
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Named::Naming do
4
+ let(:subject) { Class.new.extend(described_class) }
5
+
6
+ it "adds a name getter" do
7
+ expect(subject).to respond_to :name
8
+ end
9
+
10
+ it "adds a name setter" do
11
+ expect(subject).to respond_to :name=
12
+ end
13
+ end
@@ -1,4 +1,6 @@
1
1
  require 'named'
2
+ require 'simplecov'
3
+ SimpleCov.start
2
4
 
3
5
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
4
6
  RSpec.configure do |config|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: named
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -44,6 +44,22 @@ dependencies:
44
44
  - - ~>
45
45
  - !ruby/object:Gem::Version
46
46
  version: '2.13'
47
+ - !ruby/object:Gem::Dependency
48
+ name: simplecov
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '0.7'
47
63
  - !ruby/object:Gem::Dependency
48
64
  name: rake
49
65
  requirement: !ruby/object:Gem::Requirement
@@ -76,12 +92,15 @@ files:
76
92
  - Rakefile
77
93
  - lib/named.rb
78
94
  - lib/named/class.rb
95
+ - lib/named/inspection.rb
79
96
  - lib/named/module.rb
80
97
  - lib/named/naming.rb
81
98
  - lib/named/version.rb
82
99
  - named.gemspec
83
100
  - spec/named/class_spec.rb
101
+ - spec/named/inspection_spec.rb
84
102
  - spec/named/module_spec.rb
103
+ - spec/named/naming_spec.rb
85
104
  - spec/spec_helper.rb
86
105
  homepage: https://github.com/adamhunter/named
87
106
  licenses:
@@ -98,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
117
  version: '0'
99
118
  segments:
100
119
  - 0
101
- hash: -2815502534427764522
120
+ hash: 2605474607335762517
102
121
  required_rubygems_version: !ruby/object:Gem::Requirement
103
122
  none: false
104
123
  requirements:
@@ -107,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
126
  version: '0'
108
127
  segments:
109
128
  - 0
110
- hash: -2815502534427764522
129
+ hash: 2605474607335762517
111
130
  requirements: []
112
131
  rubyforge_project:
113
132
  rubygems_version: 1.8.23
@@ -117,5 +136,7 @@ summary: Allows providing a name when creating an anonymous module or class usin
117
136
  Named::Module.new or Named::Class.new
118
137
  test_files:
119
138
  - spec/named/class_spec.rb
139
+ - spec/named/inspection_spec.rb
120
140
  - spec/named/module_spec.rb
141
+ - spec/named/naming_spec.rb
121
142
  - spec/spec_helper.rb