naming 0.0.2 → 0.1.0

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.
@@ -0,0 +1,8 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
+ SimpleCov::Formatter::HTMLFormatter,
5
+ Coveralls::SimpleCov::Formatter
6
+ ]
7
+ SimpleCov.command_name 'bacon'
8
+ SimpleCov.start {add_filter 'test'}
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.2"
4
+ - "1.9.3"
5
+ - "2.0.0"
6
+ - jruby-19mode # JRuby in 1.9 mode
7
+ - rbx-19mode
8
+ script: rake test
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in naming.gemspec
3
+ # Specify your gem's dependencies in forwardablex.gemspec
4
4
  gemspec
5
+
6
+ gem 'simplecov', :require => false, :group => :test
7
+ gem 'coveralls', :require => false, :group => :test
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # naming
2
2
 
3
- naming is a ruby library for generating classes that consist from name and
4
- value. You can get naming class by sending class method +[]+ with the
5
- name.
3
+ naming is a Ruby library for generating classes that consist from name and
4
+ value. You can name objects and collect values by it.
5
+
6
+ [![Gem Version](https://badge.fury.io/rb/naming.png)](http://badge.fury.io/rb/naming) [![Build Status](https://travis-ci.org/keita/naming.png?branch=master)](https://travis-ci.org/keita/naming) [![Coverage Status](https://coveralls.io/repos/keita/naming/badge.png?branch=master)](https://coveralls.io/r/keita/naming) [![Code Climate](https://codeclimate.com/github/keita/naming.png)](https://codeclimate.com/github/keita/naming)
6
7
 
7
8
  ## Installation
8
9
 
@@ -65,9 +66,29 @@ message(Naming.B(2)) #=> "This is case B: 2"
65
66
  message(true) #=> "This is case others: true"
66
67
  ```
67
68
 
68
- ## Licence
69
+ ### Name Set
70
+
71
+ ```ruby
72
+ #
73
+ # collect objects by name set
74
+ #
75
+ list = [
76
+ Naming.A(1),
77
+ Naming.B(2),
78
+ "abc",
79
+ Naming.C(3),
80
+ 123,
81
+ nil
82
+ ]
83
+ # collect values of A and B
84
+ Naming[:A, :B].values(list) #=> [1, 2]
85
+ # collect others
86
+ Naming[:A, :B].others(list) #=> ["abc", Naming.C(3), 123, nil]
87
+ ```
88
+
89
+ ## License
69
90
 
70
- naming is free software distributed under MIT licence.
91
+ naming is free software distributed under MIT license.
71
92
 
72
93
  ## Contributing
73
94
 
data/Rakefile CHANGED
@@ -2,15 +2,17 @@ require "bundler/gem_tasks"
2
2
 
3
3
  desc 'Test specs'
4
4
  task 'test' do
5
- sh "bundle exec bacon -a"
5
+ sh "bundle exec bacon -r simplecov -a"
6
6
  end
7
7
 
8
8
  desc 'Generate API document'
9
9
  task 'html' do
10
- sh "yard doc -o html --hide-void-return --no-api"
10
+ sh "bundle exec yard doc -o html --hide-void-return --no-api"
11
11
  end
12
12
 
13
13
  desc 'Show undocumented function list'
14
14
  task 'html:undoc' do
15
- sh "yard stats --list-undoc --no-api"
15
+ sh "bundle exec yard stats --list-undoc --no-api --compact"
16
16
  end
17
+
18
+ task :default => :test
@@ -1,3 +1,12 @@
1
+ require 'set'
2
+
3
+ require 'forwardablex'
4
+ require 'structx'
5
+
6
+ require 'naming/version'
7
+ require 'naming/meta'
8
+ require 'naming/name-set'
9
+
1
10
  # Naming is a module for generating naming classes that consist by name and
2
11
  # value. Naming classes are generated dynamically when you refer it.
3
12
  #
@@ -45,105 +54,6 @@
45
54
  # message(Naming.B(2)) #=> "This is case B: 2"
46
55
  # message(true) #=> "This is case others: true"
47
56
  module Naming
48
- # Meta is a container of name and value. This is super class of all naming
49
- # classes.
50
- class Meta
51
- class << self
52
- # Extract values which have the same name from the array.
53
- #
54
- # @param array [Array]
55
- # target of value extraction
56
- #
57
- # @example
58
- # Naming::A.values([
59
- # Naming.A(1),
60
- # Naming.B(2),
61
- # "abc",
62
- # Naming.A(3),
63
- # 123,
64
- # nil
65
- # ]) #=> [1, 3]
66
- def values(array)
67
- array.select{|elt| elt.kind_of?(self)}.map{|elt| elt.value}
68
- end
69
-
70
- # Collect objects from the array excluding named objects which have the
71
- # same name.
72
- #
73
- # @param array [Array]
74
- # target of value extraction
75
- #
76
- # @example
77
- # Naming::A.values([
78
- # Naming.A(1),
79
- # Naming.B(2),
80
- # "abc",
81
- # Naming.A(3),
82
- # 123,
83
- # nil
84
- # ]) #=> [Naming.B(2), "abc", 123, nil]
85
- def others(array)
86
- array.select{|elt| not(elt.kind_of?(self))}
87
- end
88
-
89
- # Return the name as symbol. It is just name, doesn't include module path.
90
- #
91
- # @return [Symbol]
92
- # the name
93
- #
94
- # @example
95
- # Naming::A.name #=> :A
96
- def name
97
- self.to_s.split("::").last.to_sym
98
- end
99
-
100
- # Set the name.
101
- #
102
- # @param name [Symbol]
103
- # the name
104
- # @return [void]
105
- #
106
- # @api private
107
- def name=(name)
108
- @name = name unless @name
109
- end
110
- end
111
-
112
- # @param value [Object]
113
- # the value
114
- def initialize(value)
115
- @value = value
116
- end
117
-
118
- # Return the name.
119
- #
120
- # @return [Symbol]
121
- # the name
122
- def name
123
- self.class.name
124
- end
125
-
126
- # Return the value.
127
- #
128
- # @return [Object]
129
- # the value
130
- def value
131
- @value
132
- end
133
-
134
- # @api private
135
- def ==(other)
136
- return false unless other.kind_of?(self.class)
137
- @value == other.value
138
- end
139
- alias :eql? :"=="
140
-
141
- # @api private
142
- def hash
143
- @value.hash
144
- end
145
- end
146
-
147
57
  class << self
148
58
  # Generate a new naming class and call the constructor with value.
149
59
  #
@@ -183,5 +93,14 @@ module Naming
183
93
  def others(array)
184
94
  array.select{|elt| not(elt.kind_of?(Meta))}
185
95
  end
96
+
97
+ # Return the name set object.
98
+ #
99
+ # @param names [Array<Symbol>]
100
+ # names
101
+ # @return [NameSet]
102
+ def [](*names)
103
+ NameSet.new(*names)
104
+ end
186
105
  end
187
106
  end
@@ -0,0 +1,58 @@
1
+ module Naming
2
+ # Meta is a container of name and value. This is super class of all naming
3
+ # classes.
4
+ class Meta < StructX
5
+ class << self
6
+ # Extract values which have the same name from the array.
7
+ #
8
+ # @param array [Array]
9
+ # target of value extraction
10
+ #
11
+ # @example
12
+ # Naming::A.values([
13
+ # Naming.A(1),
14
+ # Naming.B(2),
15
+ # "abc",
16
+ # Naming.A(3),
17
+ # 123,
18
+ # nil
19
+ # ]) #=> [1, 3]
20
+ def values(array)
21
+ array.select{|elt| elt.kind_of?(self)}.map{|elt| elt.value}
22
+ end
23
+
24
+ # Collect objects from the array excluding named objects which have the
25
+ # same name.
26
+ #
27
+ # @param array [Array]
28
+ # target of value extraction
29
+ #
30
+ # @example
31
+ # Naming::A.values([
32
+ # Naming.A(1),
33
+ # Naming.B(2),
34
+ # "abc",
35
+ # Naming.A(3),
36
+ # 123,
37
+ # nil
38
+ # ]) #=> [Naming.B(2), "abc", 123, nil]
39
+ def others(array)
40
+ array.select{|elt| not(elt.kind_of?(self))}
41
+ end
42
+
43
+ # Return the name as symbol. It is just name, doesn't include module path.
44
+ #
45
+ # @return [Symbol]
46
+ # the name
47
+ #
48
+ # @example
49
+ # Naming::A.name #=> :A
50
+ def name
51
+ self.to_s.split("::").last.to_sym
52
+ end
53
+ end
54
+
55
+ forward :class, :name
56
+ member :value
57
+ end
58
+ end
@@ -0,0 +1,48 @@
1
+ module Naming
2
+ # NameSet is a set for names. This class is used for getting values of multipule names.
3
+ class NameSet < Set
4
+ # @param names [Array<Symbol,Naming::Meta>]
5
+ # element names of the set
6
+ def initialize(*names, &b)
7
+ elts = names.map {|name| name.kind_of?(Symbol) ? Naming.const_get(name) : name}
8
+ super(elts, &b)
9
+ end
10
+
11
+ # Extract values which have the same name from the array.
12
+ #
13
+ # @param array [Array]
14
+ # target of value extraction
15
+ #
16
+ # @example
17
+ # Naming::NameSet.new(:A, :B).values([
18
+ # Naming.A(1),
19
+ # Naming.B(2),
20
+ # "abc",
21
+ # Naming.A(3),
22
+ # 123,
23
+ # nil
24
+ # ]) #=> [1, 2, 3]
25
+ def values(array)
26
+ array.select{|elt| any?{|name| elt.kind_of?(name)}}.map{|elt| elt.value}
27
+ end
28
+
29
+ # Collect objects from the array excluding named objects which have the
30
+ # name in the set.
31
+ #
32
+ # @param array [Array]
33
+ # target of value extraction
34
+ #
35
+ # @example
36
+ # Naming::NameSet(:A, :B).values([
37
+ # Naming.A(1),
38
+ # Naming.B(2),
39
+ # "abc",
40
+ # Naming.A(3),
41
+ # 123,
42
+ # nil
43
+ # ]) #=> ["abc", 123, nil]
44
+ def others(array)
45
+ array.select{|elt| not(any?{|name| elt.kind_of?(name)})}
46
+ end
47
+ end
48
+ end
@@ -1,4 +1,4 @@
1
1
  module Naming
2
2
  # version of this library
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
4
4
  end
@@ -17,5 +17,11 @@ Gem::Specification.new do |gem|
17
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency "forwardablex", "~> 0.1.4"
22
+ gem.add_dependency "structx", "~> 0.1.0"
23
+
20
24
  gem.add_development_dependency "bacon"
25
+ gem.add_development_dependency "yard"
26
+ gem.add_development_dependency "redcarpet" unless RUBY_ENGINE == 'jruby'
21
27
  end
@@ -1,5 +1,14 @@
1
1
  require 'naming'
2
2
 
3
+ $list = [
4
+ Naming.A(1),
5
+ Naming.B(2),
6
+ "abc",
7
+ Naming.A(3),
8
+ 123,
9
+ nil
10
+ ]
11
+
3
12
  describe 'Naming' do
4
13
  it 'should generate a class dynamically' do
5
14
  Naming::A.should.be.kind_of Class
@@ -17,28 +26,67 @@ describe 'Naming' do
17
26
  end
18
27
 
19
28
  it 'should filter by name' do
20
- list = [
21
- Naming.A(1),
22
- Naming.B(2),
23
- "abc",
24
- Naming.A(3),
25
- 123,
26
- nil
27
- ]
28
- Naming::A.values(list).should == [1, 3]
29
- Naming::B.values(list).should == [2]
30
- Naming.others(list).should == ["abc", 123, nil]
29
+ Naming::A.values($list).should == [1, 3]
30
+ Naming::B.values($list).should == [2]
31
+ Naming.others($list).should == ["abc", 123, nil]
31
32
  end
32
33
 
33
34
  it 'should equal when the names and values are same' do
34
35
  Naming.A(1).should == Naming.A(1)
35
36
  end
36
37
 
37
- it 'should equal when names are different' do
38
+ it 'should not equal when names are different' do
38
39
  Naming.A(1).should.not == Naming.B(1)
39
40
  end
40
41
 
41
- it 'should equal when values are different' do
42
+ it 'should not equal when values are different' do
42
43
  Naming.A(1).should.not == Naming.A(2)
43
44
  end
45
+
46
+ it 'should get the name set' do
47
+ Naming[:A, :B].should.kind_of(Naming::NameSet)
48
+ Naming[:A, :B].should.include(Naming::A)
49
+ Naming[:A, :B].should.include(Naming::B)
50
+ end
51
+ end
52
+
53
+ describe "Naming::Meta" do
54
+ it "should get values" do
55
+ Naming::A.values($list).should == [1, 3]
56
+ end
57
+
58
+ it "should get others" do
59
+ Naming::A.others($list).should == [Naming.B(2), "abc", 123, nil]
60
+ end
61
+
62
+ it "should get the name" do
63
+ Naming::A.name.should == :A
64
+ end
65
+ end
66
+
67
+ describe "Naming::NameSet" do
68
+ before do
69
+ @set = Naming::NameSet.new(:A, :B)
70
+ end
71
+
72
+ it 'should be a set' do
73
+ @set.should.kind_of(Set)
74
+ end
75
+
76
+ it 'should include element names' do
77
+ @set.should.include Naming::A
78
+ @set.should.include Naming::B
79
+ end
80
+
81
+ it "should not include other names" do
82
+ @set.should.not.include Naming::C
83
+ end
84
+
85
+ it 'should get the values' do
86
+ @set.values($list).should == [1, 2, 3]
87
+ end
88
+
89
+ it 'should get other values' do
90
+ @set.others($list).should == ["abc", 123, nil]
91
+ end
44
92
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naming
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,40 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-20 00:00:00.000000000 Z
12
+ date: 2013-06-02 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: forwardablex
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.1.4
30
+ - !ruby/object:Gem::Dependency
31
+ name: structx
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.1.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.1.0
14
46
  - !ruby/object:Gem::Dependency
15
47
  name: bacon
16
48
  requirement: !ruby/object:Gem::Requirement
@@ -27,6 +59,38 @@ dependencies:
27
59
  - - ! '>='
28
60
  - !ruby/object:Gem::Version
29
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: yard
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: redcarpet
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
30
94
  description: naming provides name and value container with useful functions
31
95
  email:
32
96
  - keita.yamaguchi@gmail.com
@@ -35,11 +99,15 @@ extensions: []
35
99
  extra_rdoc_files: []
36
100
  files:
37
101
  - .gitignore
102
+ - .simplecov
103
+ - .travis.yml
38
104
  - Gemfile
39
105
  - LICENSE.txt
40
106
  - README.md
41
107
  - Rakefile
42
108
  - lib/naming.rb
109
+ - lib/naming/meta.rb
110
+ - lib/naming/name-set.rb
43
111
  - lib/naming/version.rb
44
112
  - naming.gemspec
45
113
  - test/spec_naming.rb
@@ -63,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
131
  version: '0'
64
132
  requirements: []
65
133
  rubyforge_project:
66
- rubygems_version: 1.8.24
134
+ rubygems_version: 1.8.25
67
135
  signing_key:
68
136
  specification_version: 3
69
137
  summary: naming provides name and value container with useful functions