naming 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ html/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in naming.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Keita Yamaguchi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # naming
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.
6
+
7
+ ## Installation
8
+
9
+ $ gem install naming
10
+
11
+ ## Usage
12
+
13
+ ### Basic
14
+
15
+ ```ruby
16
+ #
17
+ # naming object of A with the value 123
18
+ #
19
+ a = Naming.A(123)
20
+ a.value #=> 123
21
+ a.name #=> :A
22
+ a.class #=> Naming::A
23
+ ````
24
+
25
+ ### Collect values by name from array
26
+
27
+ ```ruby
28
+ #
29
+ # collect objects by name
30
+ #
31
+ list = [
32
+ Naming.A(1),
33
+ Naming.B(2),
34
+ "abc",
35
+ Naming.A(3),
36
+ 123,
37
+ nil
38
+ ]
39
+ # collect values of A objects
40
+ Naming::A.values(list) #=> [1, 3]
41
+ # collect values of B objects
42
+ Naming::B.values(list) #=> [2]
43
+ # collect objects excluding naming objects
44
+ Naming.others(list) #=> ["abc", 123, nil]
45
+ ```
46
+
47
+ ### Case Selecting
48
+
49
+ ```ruby
50
+ #
51
+ # case control flow with name
52
+ #
53
+ def message(obj)
54
+ case obj
55
+ when Naming::A
56
+ "This is case A: %s" % obj.value
57
+ when Naming::B
58
+ "This is case B: %s" % obj.value
59
+ else
60
+ "This is case others: %s" % obj
61
+ end
62
+ end
63
+ message(Naming.A(1)) #=> "This is case A: 1"
64
+ message(Naming.B(2)) #=> "This is case B: 2"
65
+ message(true) #=> "This is case others: true"
66
+ ```
67
+
68
+ ## Licence
69
+
70
+ naming is free software distributed under MIT licence.
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Test specs'
4
+ task 'test' do
5
+ sh "bundle exec bacon -a"
6
+ end
7
+
8
+ desc 'Generate API document'
9
+ task 'html' do
10
+ sh "yard doc -o html --hide-void-return --no-api"
11
+ end
12
+
13
+ desc 'Show undocumented function list'
14
+ task 'html:undoc' do
15
+ sh "yard stats --list-undoc --no-api"
16
+ end
data/lib/naming.rb ADDED
@@ -0,0 +1,175 @@
1
+ # Naming is a module for generating naming classes that consist by name and
2
+ # value. Naming classes are generated dynamically when you refer it.
3
+ #
4
+ # @example
5
+ # #
6
+ # # naming object of A with the value 123
7
+ # #
8
+ # a = Naming.A(123)
9
+ # a.value #=> 123
10
+ # a.name #=> :A
11
+ # a.class #=> Naming::A
12
+ # @example
13
+ # #
14
+ # # collect objects by name
15
+ # #
16
+ # list = [
17
+ # Naming.A(1),
18
+ # Naming.B(2),
19
+ # "abc",
20
+ # Naming.A(3),
21
+ # 123,
22
+ # nil
23
+ # ]
24
+ # # collect values of A objects
25
+ # Naming::A.values(list) #=> [1, 3]
26
+ # # collect values of B objects
27
+ # Naming::B.values(list) #=> [2]
28
+ # # collect objects excluding naming objects
29
+ # Naming.others(list) #=> ["abc", 123, nil]
30
+ # @example
31
+ # #
32
+ # # case control flow with name
33
+ # #
34
+ # def message(obj)
35
+ # case obj
36
+ # when Naming::A
37
+ # "This is case A: %s" % obj.value
38
+ # when Naming::B
39
+ # "This is case B: %s" % obj.value
40
+ # else
41
+ # "This is case others: %s" % obj
42
+ # end
43
+ # end
44
+ # message(Naming.A(1)) #=> "This is case A: 1"
45
+ # message(Naming.B(2)) #=> "This is case B: 2"
46
+ # message(true) #=> "This is case others: true"
47
+ 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
+ end
134
+
135
+ class << self
136
+ # Generate a new naming class and call the constructor with value.
137
+ #
138
+ # @api private
139
+ def method_missing(name, *args)
140
+ const_get(name)
141
+ send(name, *args)
142
+ end
143
+
144
+ # Generate a new naming class.
145
+ #
146
+ # @api private
147
+ def const_missing(name)
148
+ cls = Class.new(Meta)
149
+ self.singleton_class.class_exec do
150
+ define_method(name) do |value|
151
+ cls.new(value)
152
+ end
153
+ end
154
+ const_set(name, cls)
155
+ end
156
+
157
+ # Collect objects from the array excluding naming objects.
158
+ #
159
+ # @param array [Array]
160
+ # collection target array
161
+ #
162
+ # @example
163
+ # Naming.others([
164
+ # Naming.A(1),
165
+ # Naming.B(2),
166
+ # "abc",
167
+ # Naming.A(3),
168
+ # 123,
169
+ # nil
170
+ # ]) #=> ["abc", 123, nil]
171
+ def others(array)
172
+ array.select{|elt| not(elt.kind_of?(Meta))}
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,4 @@
1
+ module Naming
2
+ # version of this library
3
+ VERSION = "0.0.1"
4
+ end
data/naming.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- ruby -*-
2
+ # -*- encoding: utf-8 -*-
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'naming/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "naming"
9
+ gem.version = Naming::VERSION
10
+ gem.authors = ["Keita Yamaguchi"]
11
+ gem.email = ["keita.yamaguchi@gmail.com"]
12
+ gem.description = "naming provides name and value container with useful functions"
13
+ gem.summary = "naming provides name and value container with useful functions"
14
+ gem.homepage = "https://github.com/keita/naming"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+ gem.add_development_dependency "bacon"
21
+ end
@@ -0,0 +1,32 @@
1
+ require 'naming'
2
+
3
+ describe 'Naming' do
4
+ it 'should generate a class dynamically' do
5
+ Naming::A.should.be.kind_of Class
6
+ end
7
+
8
+ it 'should have name' do
9
+ Naming::A.name.should == :A
10
+ Naming.A(1).name.should == :A
11
+ end
12
+
13
+ it 'should have value' do
14
+ Naming.A(1).value.should == 1
15
+ Naming.A(true).value.should == true
16
+ Naming.A("str").value.should == "str"
17
+ end
18
+
19
+ 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]
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: naming
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keita Yamaguchi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bacon
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: naming provides name and value container with useful functions
31
+ email:
32
+ - keita.yamaguchi@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/naming.rb
43
+ - lib/naming/version.rb
44
+ - naming.gemspec
45
+ - test/spec_naming.rb
46
+ homepage: https://github.com/keita/naming
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.24
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: naming provides name and value container with useful functions
70
+ test_files:
71
+ - test/spec_naming.rb
72
+ has_rdoc: