attribute_enum 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5075ad8cbbf43b276833d5e1b369a415a046bdac
4
+ data.tar.gz: 17cc221ce2dac88bcac9beb371fd6c03eaff1077
5
+ SHA512:
6
+ metadata.gz: 6eb30fbeb983574b458553d3c8107e6f77de76575a53de38038bb0e8a03d2f7b14d494e276592e60b011a38d16699b935de4ca578ff0d4a8d13f27495366a91d
7
+ data.tar.gz: bf5260f15d6fad7a7eadd0c545342be9120c42eb9c0a6cd333bffdb467ab4b2da1265fb6681eff6db835acf651e3f7905482dd5dc620548a7efcfa04829ab594
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,23 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="FacetManager">
4
+ <facet type="gem" name="New Gem">
5
+ <configuration>
6
+ <option name="GEM_APP_ROOT_PATH" value="$MODULE_DIR$" />
7
+ <option name="GEM_APP_TEST_PATH" value="$MODULE_DIR$/test" />
8
+ <option name="GEM_APP_LIB_PATH" value="$MODULE_DIR$/lib" />
9
+ </configuration>
10
+ </facet>
11
+ </component>
12
+ <component name="NewModuleRootManager">
13
+ <content url="file://$MODULE_DIR$">
14
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
15
+ <excludeFolder url="file://$MODULE_DIR$/.bundle" />
16
+ <excludeFolder url="file://$MODULE_DIR$/vendor/bundle" />
17
+ </content>
18
+ <orderEntry type="inheritedJdk" />
19
+ <orderEntry type="sourceFolder" forTests="false" />
20
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v1.11.2, rbenv: 2.3.0) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.4.2, rbenv: 2.3.0) [gem]" level="application" />
22
+ </component>
23
+ </module>
data/.idea/misc.xml ADDED
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectLevelVcsManager" settingsEditedManually="false">
4
+ <OptionsSetting value="true" id="Add" />
5
+ <OptionsSetting value="true" id="Remove" />
6
+ <OptionsSetting value="true" id="Checkout" />
7
+ <OptionsSetting value="true" id="Update" />
8
+ <OptionsSetting value="true" id="Status" />
9
+ <OptionsSetting value="true" id="Edit" />
10
+ <ConfirmationsSetting value="0" id="Add" />
11
+ <ConfirmationsSetting value="0" id="Remove" />
12
+ </component>
13
+ <component name="ProjectRootManager" version="2" project-jdk-name="rbenv: 2.3.0" project-jdk-type="RUBY_SDK" />
14
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/attribute_enum.iml" filepath="$PROJECT_DIR$/.idea/attribute_enum.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,214 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ChangeListManager">
4
+ <list default="true" id="9a54a5f6-efef-449e-80de-a116ffb81be8" name="Default" comment="" />
5
+ <ignored path="attribute_enum.iws" />
6
+ <ignored path=".idea/workspace.xml" />
7
+ <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
8
+ <option name="TRACKING_ENABLED" value="true" />
9
+ <option name="SHOW_DIALOG" value="false" />
10
+ <option name="HIGHLIGHT_CONFLICTS" value="true" />
11
+ <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
12
+ <option name="LAST_RESOLUTION" value="IGNORE" />
13
+ </component>
14
+ <component name="ChangesViewManager" flattened_view="true" show_ignored="false" />
15
+ <component name="CreatePatchCommitExecutor">
16
+ <option name="PATCH_PATH" value="" />
17
+ </component>
18
+ <component name="ExecutionTargetManager" SELECTED_TARGET="default_target" />
19
+ <component name="FavoritesManager">
20
+ <favorites_list name="attribute_enum" />
21
+ </component>
22
+ <component name="FileEditorManager">
23
+ <leaf>
24
+ <file leaf-file-name="attribute_enum.rb" pinned="false" current-in-tab="false">
25
+ <entry file="file://$PROJECT_DIR$/lib/attribute_enum.rb">
26
+ <provider selected="true" editor-type-id="text-editor">
27
+ <state relative-caret-position="18">
28
+ <caret line="1" column="9" selection-start-line="1" selection-start-column="9" selection-end-line="1" selection-end-column="23" />
29
+ <folding />
30
+ </state>
31
+ </provider>
32
+ </entry>
33
+ </file>
34
+ <file leaf-file-name="Rakefile" pinned="false" current-in-tab="false">
35
+ <entry file="file://$PROJECT_DIR$/Rakefile">
36
+ <provider selected="true" editor-type-id="text-editor">
37
+ <state relative-caret-position="0">
38
+ <caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
39
+ <folding />
40
+ </state>
41
+ </provider>
42
+ </entry>
43
+ </file>
44
+ <file leaf-file-name="attribute_enum.gemspec" pinned="false" current-in-tab="true">
45
+ <entry file="file://$PROJECT_DIR$/attribute_enum.gemspec">
46
+ <provider selected="true" editor-type-id="text-editor">
47
+ <state relative-caret-position="205">
48
+ <caret line="29" column="37" selection-start-line="29" selection-start-column="37" selection-end-line="29" selection-end-column="37" />
49
+ <folding />
50
+ </state>
51
+ </provider>
52
+ </entry>
53
+ </file>
54
+ </leaf>
55
+ </component>
56
+ <component name="IdeDocumentHistory">
57
+ <option name="CHANGED_PATHS">
58
+ <list>
59
+ <option value="$PROJECT_DIR$/lib/attribute_enum.rb" />
60
+ <option value="$PROJECT_DIR$/attribute_enum.gemspec" />
61
+ </list>
62
+ </option>
63
+ </component>
64
+ <component name="JsBuildToolGruntFileManager" detection-done="true" />
65
+ <component name="JsBuildToolPackageJson" detection-done="true" />
66
+ <component name="JsGulpfileManager">
67
+ <detection-done>true</detection-done>
68
+ </component>
69
+ <component name="ProjectFrameBounds">
70
+ <option name="x" value="65" />
71
+ <option name="y" value="24" />
72
+ <option name="width" value="1301" />
73
+ <option name="height" value="744" />
74
+ </component>
75
+ <component name="ProjectLevelVcsManager" settingsEditedManually="false">
76
+ <OptionsSetting value="true" id="Add" />
77
+ <OptionsSetting value="true" id="Remove" />
78
+ <OptionsSetting value="true" id="Checkout" />
79
+ <OptionsSetting value="true" id="Update" />
80
+ <OptionsSetting value="true" id="Status" />
81
+ <OptionsSetting value="true" id="Edit" />
82
+ <ConfirmationsSetting value="0" id="Add" />
83
+ <ConfirmationsSetting value="0" id="Remove" />
84
+ </component>
85
+ <component name="ProjectView">
86
+ <navigator currentView="ProjectPane" proportions="" version="1">
87
+ <flattenPackages />
88
+ <showMembers />
89
+ <showModules />
90
+ <showLibraryContents />
91
+ <hideEmptyPackages />
92
+ <abbreviatePackageNames />
93
+ <autoscrollToSource />
94
+ <autoscrollFromSource />
95
+ <sortByType />
96
+ <manualOrder />
97
+ <foldersAlwaysOnTop value="true" />
98
+ </navigator>
99
+ <panes>
100
+ <pane id="Scope" />
101
+ <pane id="ProjectPane">
102
+ <subPane>
103
+ <PATH>
104
+ <PATH_ELEMENT>
105
+ <option name="myItemId" value="attribute_enum" />
106
+ <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
107
+ </PATH_ELEMENT>
108
+ </PATH>
109
+ <PATH>
110
+ <PATH_ELEMENT>
111
+ <option name="myItemId" value="attribute_enum" />
112
+ <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
113
+ </PATH_ELEMENT>
114
+ <PATH_ELEMENT>
115
+ <option name="myItemId" value="attribute_enum" />
116
+ <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
117
+ </PATH_ELEMENT>
118
+ </PATH>
119
+ <PATH>
120
+ <PATH_ELEMENT>
121
+ <option name="myItemId" value="attribute_enum" />
122
+ <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
123
+ </PATH_ELEMENT>
124
+ <PATH_ELEMENT>
125
+ <option name="myItemId" value="attribute_enum" />
126
+ <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
127
+ </PATH_ELEMENT>
128
+ <PATH_ELEMENT>
129
+ <option name="myItemId" value="lib" />
130
+ <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
131
+ </PATH_ELEMENT>
132
+ </PATH>
133
+ </subPane>
134
+ </pane>
135
+ <pane id="Scratches" />
136
+ </panes>
137
+ </component>
138
+ <component name="PropertiesComponent">
139
+ <property name="WebServerToolWindowFactoryState" value="false" />
140
+ </component>
141
+ <component name="ShelveChangesManager" show_recycled="false">
142
+ <option name="remove_strategy" value="false" />
143
+ </component>
144
+ <component name="TaskManager">
145
+ <task active="true" id="Default" summary="Default task">
146
+ <changelist id="9a54a5f6-efef-449e-80de-a116ffb81be8" name="Default" comment="" />
147
+ <created>1461329391280</created>
148
+ <option name="number" value="Default" />
149
+ <option name="presentableId" value="Default" />
150
+ <updated>1461329391280</updated>
151
+ <workItem from="1461329392694" duration="90000" />
152
+ </task>
153
+ <servers />
154
+ </component>
155
+ <component name="TimeTrackingManager">
156
+ <option name="totallyTimeSpent" value="90000" />
157
+ </component>
158
+ <component name="ToolWindowManager">
159
+ <frame x="65" y="24" width="1301" height="744" extended-state="6" />
160
+ <editor active="true" />
161
+ <layout>
162
+ <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.25418326" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
163
+ <window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
164
+ <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
165
+ <window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
166
+ <window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
167
+ <window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.32868218" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
168
+ <window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
169
+ <window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
170
+ <window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
171
+ <window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
172
+ <window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
173
+ <window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
174
+ <window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
175
+ <window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
176
+ <window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
177
+ <window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
178
+ <window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
179
+ </layout>
180
+ </component>
181
+ <component name="VcsContentAnnotationSettings">
182
+ <option name="myLimit" value="2678400000" />
183
+ </component>
184
+ <component name="XDebuggerManager">
185
+ <breakpoint-manager />
186
+ <watches-manager />
187
+ </component>
188
+ <component name="editorHistoryManager">
189
+ <entry file="file://$PROJECT_DIR$/Rakefile">
190
+ <provider selected="true" editor-type-id="text-editor">
191
+ <state relative-caret-position="0">
192
+ <caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
193
+ <folding />
194
+ </state>
195
+ </provider>
196
+ </entry>
197
+ <entry file="file://$PROJECT_DIR$/lib/attribute_enum.rb">
198
+ <provider selected="true" editor-type-id="text-editor">
199
+ <state relative-caret-position="18">
200
+ <caret line="1" column="9" selection-start-line="1" selection-start-column="9" selection-end-line="1" selection-end-column="23" />
201
+ <folding />
202
+ </state>
203
+ </provider>
204
+ </entry>
205
+ <entry file="file://$PROJECT_DIR$/attribute_enum.gemspec">
206
+ <provider selected="true" editor-type-id="text-editor">
207
+ <state relative-caret-position="205">
208
+ <caret line="29" column="37" selection-start-line="29" selection-start-column="37" selection-end-line="29" selection-end-column="37" />
209
+ <folding />
210
+ </state>
211
+ </provider>
212
+ </entry>
213
+ </component>
214
+ </project>
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at vladislav.faust@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in attribute_enum.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Vlad Faust
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # AttributeEnum
2
+
3
+ Turns a dot-accessible class attribute to an enum (a.k.a bytefield).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'attribute_enum'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install attribute_enum
20
+
21
+ ## Usage
22
+
23
+ Class must have read'n'write attribute named `[what's being enumerated]`.
24
+
25
+ ```ruby
26
+ class Payment
27
+ include Enum
28
+ attr_accessor :status
29
+
30
+ def initialize(status = 0)
31
+ @status = status
32
+ end
33
+
34
+ enum :status, [ :active, :inactive ]
35
+ end
36
+
37
+ payment = Payment.new
38
+
39
+ payment.get_status # => :active
40
+ payment.inactive? # => false
41
+ payment.set_status(:inactive) # => true
42
+ payment.set_status(:invalid) # => ArgumentError('unknown value invalid')
43
+ payment.active? # => false
44
+ payment.active! # => true
45
+ payment.statuses # => [:active, :inactive]
46
+ payment.get_statuses # => [:active, :inactive]
47
+
48
+ Payment.statuses # => [:active, :inactive]
49
+ Payment.get_statuses # => [:active, :inactive]
50
+ Payment.get_status(1) # => :inactive
51
+ Payment.get_status(:active) # => 0
52
+ Payment.get_status('invalid') # => ArgumentError('valid argument is either Fixnum or Symbol')
53
+
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vladfaust/attribute_enum. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
70
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'attribute_enum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "attribute_enum"
8
+ spec.version = AttributeEnum::VERSION
9
+ spec.authors = ["Vlad Faust"]
10
+ spec.email = ["vladislav.faust@gmail.com"]
11
+
12
+ spec.summary = 'Turns a dot-accessible class attribute to an enum (a.k.a bytefield).'
13
+ spec.homepage = "https://github.com/vladfaust/attribute_enum"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activesupport'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "attribute_enum"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module AttributeEnum
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,148 @@
1
+ require 'attribute_enum/version'
2
+ require 'active_support/inflector'
3
+
4
+ module AttributeEnum
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ base.instance_eval do
8
+ @enums = {}
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ attr_reader :enums
14
+
15
+ # Turns a dot-accessible class attribute to an enum (a.k.a bytefield).
16
+ # Class must have read'n'write attribute named [what's being enumerated].
17
+ #
18
+ # @example
19
+ #
20
+ # class Payment
21
+ # include AttributeEnum
22
+ # attr_accessor :status
23
+ #
24
+ # def initialize(status = 0)
25
+ # @status = status
26
+ # end
27
+ #
28
+ # enum :status, [ :active, :inactive ]
29
+ # end
30
+ #
31
+ # payment = Payment.new
32
+ #
33
+ # payment.get_status # => :active
34
+ # payment.inactive? # => false
35
+ # payment.set_status(:inactive) # => true
36
+ # payment.set_status(:invalid) # => ArgumentError('unknown value invalid')
37
+ # payment.active? # => false
38
+ # payment.active! # => true
39
+ # payment.statuses # => [:active, :inactive]
40
+ # payment.get_statuses # => [:active, :inactive]
41
+ #
42
+ # Payment.statuses # => [:active, :inactive]
43
+ # Payment.get_statuses # => [:active, :inactive]
44
+ # Payment.get_status(1) # => :inactive
45
+ # Payment.get_status(:active) # => 0
46
+ # Payment.get_status('invalid') # => ArgumentError('valid argument is either Fixnum or Symbol')
47
+ #
48
+ def enum(enumerated, values)
49
+ if values.is_a? Hash
50
+ values.each do |key,val|
51
+ raise ArgumentError, "index should be numeric, #{key} provided wich it's a #{key.class}" unless key.is_a? Fixnum
52
+ raise ArgumentError "value should be a symbol, #{val} provided wich it's a #{val.class}" unless val.is_a? Symbol
53
+ end
54
+ elsif values.is_a? Array
55
+ values = Hash[values.map.with_index { |v, i| [i,v] }]
56
+ else
57
+ raise ArgumentError, "#enum expects the second argument to be an array of symbols or a hash like { index => :value }"
58
+ end
59
+
60
+ # Symbolize values
61
+ #
62
+ values.each do |key, val|
63
+ s_key = key.to_s
64
+ values[key] = values[key].to_sym if values[key]
65
+ values[s_key] = values[s_key].to_sym if values[s_key]
66
+ end
67
+
68
+ pluralized = enumerated.to_s.pluralize
69
+
70
+ # ###
71
+ # Instance methods
72
+ # ###
73
+
74
+ # obj.set_status :active
75
+ #
76
+ define_method "set_#{enumerated}" do |value|
77
+ index = self.class.enums[enumerated].rassoc(value)
78
+ raise ArgumentError.new("unknown value #{ value }") unless (index && (index = index.first))
79
+ self.send("#{enumerated}=", index)
80
+ true
81
+ end
82
+
83
+ # obj.get_status # => :active
84
+ #
85
+ define_method "get_#{enumerated}" do
86
+ self.class.enums[enumerated].fetch(self.send(enumerated), nil)
87
+ end
88
+
89
+ values.each do |key, value|
90
+ # obj.inactive? # => false
91
+ #
92
+ define_method "#{value}?" do
93
+ self.send("get_#{enumerated}") == value
94
+ end
95
+
96
+ # obj.inactive! # => true
97
+ #
98
+ define_method "#{value}!" do
99
+ self.send("set_#{enumerated}", value)
100
+ end
101
+ end
102
+
103
+ # obj.statuses # => [:active, :inactive]
104
+ #
105
+ define_method pluralized do
106
+ self.class.enums[enumerated].map{ |k, v| v }
107
+ end
108
+
109
+ # obj.get_statuses # => [:active, :inactive]
110
+ #
111
+ alias_method "get_#{pluralized}", pluralized
112
+
113
+ # ###
114
+ # Class methods
115
+ # ###
116
+
117
+ # Obj.statuses # => [:active, :inactive]
118
+ #
119
+ define_singleton_method pluralized do
120
+ self.enums[enumerated].map{ |k, v| v }
121
+ end
122
+
123
+ # Obj.get_statuses # => [:active, :inactive]
124
+ #
125
+ singleton_class.class_eval do
126
+ alias_method "get_#{pluralized}", pluralized
127
+ end
128
+
129
+ # Obj.get_status(0) # => :active
130
+ # Obj.get_status(:active) # => 0
131
+ #
132
+ define_singleton_method "get_#{enumerated}" do |arg|
133
+ if arg.is_a? Fixnum
134
+ self.enums[enumerated].fetch(arg, nil)
135
+ elsif arg.is_a? Symbol
136
+ key = self.enums[enumerated].rassoc(arg)
137
+ key ? key[0] : nil
138
+ else
139
+ raise ArgumentError.new('valid argument is either Fixnum or Symbol')
140
+ end
141
+ end
142
+
143
+ # Fin
144
+ enums[enumerated] = values
145
+ end
146
+ end
147
+ end
148
+
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribute_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vlad Faust
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - vladislav.faust@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".idea/attribute_enum.iml"
64
+ - ".idea/misc.xml"
65
+ - ".idea/modules.xml"
66
+ - ".idea/workspace.xml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - attribute_enum.gemspec
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/attribute_enum.rb
76
+ - lib/attribute_enum/version.rb
77
+ homepage: https://github.com/vladfaust/attribute_enum
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.5.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Turns a dot-accessible class attribute to an enum (a.k.a bytefield).
101
+ test_files: []