-
Notifications
You must be signed in to change notification settings - Fork 67
Update how generics work for Avram::Attribute #586
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,77 +1,69 @@ | ||
| class Avram::Attribute(T) | ||
| alias ErrorMessage = String | Proc(String, String, String) | Avram::CallableErrorMessage | ||
| getter :name | ||
| setter :value | ||
| getter :param_key | ||
| getter name : Symbol | ||
| setter value : T? | ||
| getter param_key : String | ||
| @errors = [] of String | ||
| @param : Avram::Uploadable | String? | ||
|
|
||
| def initialize( | ||
| @name : Symbol, | ||
| @param : Avram::Uploadable | String?, | ||
| @value : T, | ||
| @param_key : String | ||
| ) | ||
| def initialize(@name, @param, @value : T?, @param_key) | ||
| @original_value = @value | ||
| end | ||
|
|
||
| @_permitted : Avram::PermittedAttribute(T)? | ||
|
|
||
| def permitted | ||
| @_permitted ||= begin | ||
| Avram::PermittedAttribute.new(name: @name, param: @param, value: @value, param_key: @param_key).tap do |attribute| | ||
| Avram::PermittedAttribute(T).new(name: @name, param: @param, value: @value, param_key: @param_key).tap do |attribute| | ||
| errors.each do |error| | ||
| attribute.add_error error | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def param | ||
| def param : Avram::Uploadable | String | ||
| @param || value.to_s | ||
| end | ||
|
|
||
| def add_error(message : ErrorMessage = "is invalid") | ||
| perform_add_error(message) | ||
| end | ||
|
|
||
| private def perform_add_error(message : String = "is invalid") | ||
| def add_error(message : String = "is invalid") | ||
| @errors << message | ||
| end | ||
|
|
||
| private def perform_add_error(message : (Proc | Avram::CallableErrorMessage)) | ||
|
Member
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. Moving this overload into the |
||
| def add_error(message : (Proc | Avram::CallableErrorMessage)) | ||
| message_string = message.call(@name.to_s, @value.to_s) | ||
| perform_add_error(message_string) | ||
| add_error(message_string) | ||
| end | ||
|
|
||
| def reset_errors | ||
| @errors = [] of String | ||
| end | ||
|
|
||
| def errors | ||
| def errors : Array(String) | ||
| @errors.uniq | ||
| end | ||
|
|
||
| def value | ||
| def value : T? | ||
| ensure_no_blank(@value) | ||
| end | ||
|
|
||
| def original_value | ||
| def original_value : T? | ||
| ensure_no_blank(@original_value) | ||
| end | ||
|
|
||
| private def ensure_no_blank(value : T) | ||
| private def ensure_no_blank(value : T?) : T? | ||
| if value.is_a?(Avram::Uploadable | String) && value.blank? | ||
| nil | ||
| else | ||
| value | ||
|
Member
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. I wonder if we should be adding a
Member
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. The value can always be nil, this code is just the logic that also treats blank strings as nil. |
||
| end | ||
| end | ||
|
|
||
| def valid? | ||
| def valid? : Bool | ||
| errors.empty? | ||
| end | ||
|
|
||
| def changed?(from : T | Nothing = Nothing.new, to : T | Nothing = Nothing.new) | ||
| def changed?(from : T? | Nothing = Nothing.new, to : T? | Nothing = Nothing.new) : Bool | ||
| from = from.is_a?(Nothing) ? true : from == original_value | ||
| to = to.is_a?(Nothing) ? true : to == value | ||
| value != original_value && from && to | ||
|
|
||
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.
I change symbol to type declaration out of preference, hopefully that's ok 😄