class-enum 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +88 -0
  3. data/lib/class/enum.rb +103 -0
  4. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e435a5446138814a1233b79b3ab4274983358e5e
4
+ data.tar.gz: e06a6fa83ce1c54f5f8cd42ae0ad9f8e679fc3b5
5
+ SHA512:
6
+ metadata.gz: 07890f28d6bcc883775e66d801ca54f8ceb7a5526e79fabee7a6a58f3405487e9b35def7f5e1a3422ddef8401d69b7b312e9b6f30dda0bc0c9ba05d2596a4365
7
+ data.tar.gz: d432e0f6924edf5dc9c6aec1fd209b59d9cf98dcf41236d07e83020fe91d0223ab2f63710ac03a2ead33da221ab84921d00c2210e2c34934042266d8dad55109
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Class::Enum
2
+
3
+ java-like class enum for Ruby
4
+
5
+ ```ruby
6
+ class Animal
7
+ include Class::Enum
8
+
9
+ attr_reader :sound
10
+
11
+ def initialize(sound)
12
+ @sound = sound
13
+ end
14
+
15
+ field :CAT, "meow"
16
+ field :DOG, "bark"
17
+ field :BIRD, "tweet"
18
+ end
19
+
20
+ puts Animal::CAT.sound # meow
21
+
22
+ case Animal::CAT
23
+ when Animal
24
+ puts "An animal"
25
+ else
26
+ puts "X"
27
+ end
28
+ # An animal
29
+
30
+ Animal::CAT.instance_of?(Animal) # => true
31
+ Animal::CAT.class # => Animal
32
+ ```
33
+
34
+ ## Requirements
35
+ - No (Ruby 1.9+?)
36
+
37
+ ## Usage
38
+ See [documentation](http://www.rubydoc.info/gems/class-enum)
39
+
40
+ ## Examples
41
+
42
+ ### Using block
43
+ ```ruby
44
+ class Animal
45
+ include Class::Enum
46
+
47
+ attr_reader :block
48
+
49
+ def initialize(&block)
50
+ @block = block
51
+ end
52
+
53
+ field :CAT do
54
+ puts "meow"
55
+ end
56
+ end
57
+
58
+ Animal::CAT.block.call # meow
59
+ ```
60
+
61
+ ### Using keyword arguments
62
+ You can keyword arguments only if ruby 2.0+.
63
+ If not ruby 2.0+, this gem doesn't use keyword arguments
64
+
65
+ ```ruby
66
+ class Animal
67
+ include Class::Enum
68
+
69
+ attr_reader :sound
70
+
71
+ def initialize(sound:)
72
+ @sound = sound
73
+ end
74
+
75
+ field :CAT, sound: "meow"
76
+ end
77
+
78
+ puts Animal::CAT.sound # meow
79
+ ```
80
+
81
+ ## Author
82
+ Sputnik Gugja (sputnikgugja@gmail.com)
83
+
84
+ ## License
85
+ This is free software released into the public domain (CC0 license).
86
+
87
+ See `LICENSE` file.
88
+
data/lib/class/enum.rb ADDED
@@ -0,0 +1,103 @@
1
+ class Class
2
+ # Utility for defining java-like enum type
3
+ module Enum
4
+ VERSION = '0.0.1'.freeze
5
+
6
+ def self.included(base)
7
+ base.send :extend, ClassMethods
8
+ base.private_class_method :new
9
+ end
10
+
11
+ module ClassMethods
12
+ # @!method field(name, *args, **kwargs, &block)
13
+ # @param [#to_sym] name
14
+ # @param args Arguments for constructor
15
+ # @param kwargs Keyword arguments for constructor
16
+ # **This argument exists only if ruby 2.0+**
17
+ # @param block A block for constructor
18
+ # @return A created object
19
+ if RUBY_VERSION >= '2.0'
20
+ eval <<-RUBY
21
+ # @param [#to_sym] name
22
+ # @param args Arguments for constructor
23
+ # @param kwargs Keyword arguments for constructor
24
+ # @param block A block for constructor
25
+ # @return A created object
26
+ def field(name, *args, **kwargs, &block)
27
+ if kwargs.empty?
28
+ obj = self.send(:new, *args, &block)
29
+ else
30
+ obj = self.send(:new, *args, **kwargs, &block)
31
+ end
32
+ name = name.to_sym
33
+ const_set(name, obj)
34
+ @enum_fields ||= {}
35
+ @enum_fields[name] = obj
36
+ obj
37
+ end
38
+ RUBY
39
+ else
40
+ eval <<-RUBY
41
+ # @param [#to_sym] name
42
+ # @param args Arguments for constructor
43
+ # @param block A block for constructor
44
+ # @return A created object
45
+ def field(name, *args, &block)
46
+ obj = self.send(:new, *args, &block)
47
+ name = name.to_sym
48
+ const_set(name, obj)
49
+ @enum_fields ||= {}
50
+ @enum_fields[name] = obj
51
+ obj
52
+ end
53
+ RUBY
54
+ end
55
+
56
+ # Checks a field of given name exists
57
+ # You can also do like `Animal::DOG.instance_of?(Animal)`
58
+ #
59
+ # @param [#to_sym] name
60
+ # @return [Boolean]
61
+ def key?(name)
62
+ name = name.to_sym
63
+ @enum_fields.key?(name)
64
+ end
65
+
66
+ # Checks given value is a field of this enum
67
+ #
68
+ # @param obj
69
+ # @return [Boolean]
70
+ def value?(obj)
71
+ @enum_fields.value?(obj)
72
+ end
73
+
74
+ # Gets a field by given name
75
+ # You can also do like `Animal::DOG`
76
+ #
77
+ # @param [#to_sym] name
78
+ # @return A enum field with given name. If not exists, nil
79
+ def value(name)
80
+ @enum_fields[name.to_sym]
81
+ end
82
+
83
+ # Gets an array of enum fields
84
+ # @return [Array] (copied) An array of enum fields
85
+ def values
86
+ @enum_fields.values
87
+ end
88
+ alias_method :to_a, :values
89
+
90
+ # Gets an array of enum field names
91
+ # @return [Array<Symbol>] (copied) An array of enum field names
92
+ def keys
93
+ @enum_fields.keys
94
+ end
95
+
96
+ # Gets an hash of enum fields
97
+ # @return [Hash{Symbol, Object}] (copied) An hash of enum fields
98
+ def to_h
99
+ @enum_fields.dup
100
+ end
101
+ end
102
+ end
103
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class-enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sputnik Gugja
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: java-like class enum for Ruby
14
+ email:
15
+ - sputnikgugja@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - lib/class/enum.rb
22
+ homepage: https://github.com/sputnikgugja/rb_event
23
+ licenses:
24
+ - CC0-1.0
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.6.3
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: java-like class enum for Ruby
46
+ test_files: []
47
+ has_rdoc: