-
Notifications
You must be signed in to change notification settings - Fork 4
Proposed addition of value class Enum::Value #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ginjo
wants to merge
11
commits into
mezuka:master
Choose a base branch
from
ginjo:valueclass
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b7f529b
Add Value class to create immutable instances of enum values.
ginjo 184b930
Fix delegation bug in init_child_class.
ginjo b83bd99
Add value_test.rb and several specs.
ginjo 310c762
Add tests for Value instance methods.
ginjo f4cb96d
Change .default to .default_value.
ginjo 566832f
Update test labels.
ginjo c19dae2
Change new_val to raw_val in Enum::Value#initialize.
ginjo ccec43c
Changed :ERROR and :ANY to :$error and :$any
ginjo 41d96c0
Fixed code that was producing method redefinition warnings.
ginjo 81bb949
Updated test files to make use of require_relative,
ginjo 5d23f9b
Removed require 'enum' to value.rb... it was circular.
ginjo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| require 'enum/token_not_found_error' | ||
| require 'enum/base' | ||
| require 'enum/predicates' | ||
| require 'enum/value' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| module Enum | ||
| class Value | ||
|
|
||
| # TODO: Perhaps stored_value should be renamed unsafe_value? | ||
|
|
||
| include Comparable | ||
|
|
||
| attr_reader :stored_value, :error | ||
|
|
||
| class << self | ||
| #attr_accessor :default_value, :suppress_read_errors, :klass | ||
| attr_writer :default_value, :suppress_read_errors | ||
| attr_accessor :klass | ||
| end | ||
|
|
||
| def self.inherited(subclass) | ||
| # Value subclass settings and options | ||
| # @default_value => <:$error|:$any|:something|nil> | ||
| # @suppress_read_errors => <true|false> | ||
| subclass.suppress_read_errors = false | ||
| subclass.default_value = :$error | ||
| super | ||
| end | ||
|
|
||
| # Combined getter/setter for 'default_value'. | ||
| def self.default_value(*args) | ||
| # Can't use .any? because [nil].any? is false (ruby 2.4), | ||
| # and nil is a valid argument here. | ||
| # [nil].empty? is also false, which is what we want. | ||
| if args.empty? | ||
| @default_value | ||
| else | ||
| @default_value = args[0] | ||
| end | ||
| end | ||
|
|
||
| # Combined getter/setter for 'suppress_read_errors'. | ||
| def self.suppress_read_errors(*args) | ||
| if !args.empty? | ||
| @suppress_read_errors = args[0] | ||
| else | ||
| @suppress_read_errors | ||
| end | ||
| end | ||
|
|
||
| # Load a primitive (symbol, string, integer) into new enum Value instance, | ||
| # taking into consideration enum constraints, default_value setting. | ||
| # Returns frozen Value instance. | ||
| # TODO: Add param: opts = {} for run-time temp settings changes (default_value, suppress_read_errors). | ||
| # TODO: Maybe convert all opts to @options => {default_value:<class or instance opts>, suppress_read_errors:<class or instance opts>} | ||
| # This would be easier to maintain but would require some special getter/setter methods. | ||
| def initialize(raw_val, opts={}) | ||
| begin | ||
| @stored_value = klass[raw_val].to_sym.freeze | ||
| rescue Enum::TokenNotFoundError => _error | ||
| @error = _error.freeze | ||
| case | ||
| when self.class.default_value == :$error || opts[:default_value] == :$error | ||
| raise _error | ||
| when self.class.default_value == :$any || opts[:default_value] == :$any | ||
| @stored_value = raw_val.freeze | ||
| else | ||
| @stored_value = self.class.default_value.freeze | ||
| end | ||
| end | ||
| self.freeze | ||
| self | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to return
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right... I probably thought I was working on |
||
| end | ||
|
|
||
| # Returns the Enum::Base subclass attached through Enum::Value subclass. | ||
| def klass | ||
| self.class.klass | ||
| end | ||
|
|
||
| # Convenience method to pass arguments to klass[]. | ||
| # Returns enum value given symbol, string, or integer. | ||
| def enum_value(_value=stored_value) | ||
| begin | ||
| klass[_value] | ||
| rescue Enum::TokenNotFoundError => _error | ||
| if self.class.suppress_read_errors | ||
| _value | ||
| else | ||
| raise _error | ||
| end | ||
| end | ||
| end | ||
| private :enum_value | ||
|
|
||
| # Returns result of Enum::Base subclass.enum(self) as string. | ||
| def to_s | ||
| enum_value.to_s | ||
| end | ||
| alias_method :to_str, :to_s | ||
|
|
||
| # Returns result of Enum::Base subclass.enum(self) as symbol. | ||
| def to_sym | ||
| val = enum_value | ||
| val.to_sym if val.respond_to?(:to_sym) | ||
| end | ||
|
|
||
| # Returns result of Enum::Base subclass.enum(self). | ||
| def value | ||
| enum_value | ||
| end | ||
|
|
||
| # Is stored value nil? | ||
| def nil? | ||
| @stored_value.nil? | ||
| end | ||
|
|
||
| # Is stored value valid for this enum class? | ||
| def valid? | ||
| klass.enum(@stored_value) | ||
| true | ||
| rescue Enum::TokenNotFoundError | ||
| false | ||
| end | ||
|
|
||
| # This (or other) value's enum index. | ||
| def index(_value=stored_value) | ||
| begin | ||
| klass.index(_value) | ||
| rescue Enum::TokenNotFoundError => _error | ||
| raise _error unless self.class.suppress_read_errors | ||
| end | ||
| end | ||
| alias_method :to_i, :index | ||
|
|
||
| # Enable comparisons between Value instances, strings, symbols, and integers. | ||
| def <=>(other) | ||
| case | ||
| when other.is_a?(Symbol) | ||
| index <=> index(other) | ||
| when other.is_a?(String) | ||
| index <=> index(other) | ||
| when other.is_a?(Integer) | ||
| index <=> other | ||
| when other.is_a?(self.class) && other.klass == klass | ||
| index <=> other.index | ||
| else | ||
| # Nil will be uncomparable with strings, symbols, or integers, | ||
| # and will raise exception, as it should. | ||
| nil | ||
| end | ||
| end | ||
|
|
||
| end # Value | ||
| end # Enum | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| require 'test_helper' | ||
| require_relative 'test_helper' | ||
|
|
||
| describe Enum::Base do | ||
| describe Side do | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| require 'test_helper' | ||
| require_relative 'test_helper' | ||
|
|
||
| describe Enum::Predicates do | ||
| describe Table do | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call it
atom, wdyt? The idea borrow from this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I like atom.