Class: NRSER::Types::Type
- Inherits:
-
Object
- Object
- NRSER::Types::Type
- Defined in:
- lib/nrser/types/type.rb
Class Method Summary collapse
Instance Method Summary collapse
- #check(value, &make_fail_message) ⇒ Object
- #default_name ⇒ Object
- #from_data(data) ⇒ Object
-
#from_s(s) ⇒ Object
Load a value of this type from a string representation by passing ‘s` to the @from_s Proc.
- #has_from_data? ⇒ Boolean
-
#has_from_s? ⇒ Boolean
Test if the type knows how to load values from strings.
-
#has_to_data? ⇒ Boolean
Test if the type has custom information about how to convert it’s values into “data” - structures and values suitable for transportation and storage (JSON, etc.).
-
#initialize(name: nil, from_s: nil, to_data: nil, from_data: nil) ⇒ Type
constructor
Instantiate a new ‘NRSER::Types::Type`.
- #name ⇒ Object
-
#respond_to?(name, include_all = false) ⇒ Boolean
Overridden to customize behavior for the #from_s and #to_data methods - those methods are always defined, but we have #respond_to? return ‘false` if they lack the underlying instance variables needed to execute.
- #test(value) ⇒ Object
-
#to_data(value) ⇒ Object
Dumps a value of this type to “data” - structures and values suitable for transport and storage, such as dumping to JSON or YAML, etc.
-
#to_s ⇒ String
(also: #inspect)
A brief string description of the type - just it’s #name surrounded by some back-ticks to make it easy to see where it starts and stops.
Constructor Details
#initialize(name: nil, from_s: nil, to_data: nil, from_data: nil) ⇒ Type
Instantiate a new ‘NRSER::Types::Type`.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/nrser/types/type.rb', line 38 def initialize name: nil, from_s: nil, to_data: nil, from_data: nil @name = name @from_s = from_s @to_data = if to_data.nil? nil elsif to_data.respond_to?( :call ) to_data elsif to_data.respond_to?( :to_proc ) to_data.to_proc else raise TypeError.new binding.erb <<-ERB `to_data:` keyword arg must be `nil`, respond to `#call` or respond to `#to_proc`. Found value: <%= to_data.pretty_inspect %> (type <%= to_data.class %>) ERB end @from_data = if from_data.nil? nil elsif from_data.respond_to?( :call ) from_data elsif from_data.respond_to?( :to_proc ) from_data.to_proc else raise TypeError.new binding.erb <<-ERB `to_data:` keyword arg must be `nil`, respond to `#call` or respond to `#to_proc`. Found value: <%= from_data.pretty_inspect %> (type <%= from_data.class %>) ERB end end |
Class Method Details
.short_name ⇒ Object
13 14 15 |
# File 'lib/nrser/types/type.rb', line 13 def self.short_name name.split('::').last end |
Instance Method Details
#check(value, &make_fail_message) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/nrser/types/type.rb', line 97 def check value, & # success case return value if test value msg = if .call type: self, value: value else NRSER.squish <<-END value #{ value.inspect } failed check #{ self.to_s } END end raise TypeError.new msg end |
#default_name ⇒ Object
88 89 90 |
# File 'lib/nrser/types/type.rb', line 88 def default_name self.class.short_name end |
#from_data(data) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/nrser/types/type.rb', line 182 def from_data data if @from_data.nil? raise NoMethodError, "#from_data not defined" end check @from_data.call( data ) end |
#from_s(s) ⇒ Object
Load a value of this type from a string representation by passing ‘s` to the @from_s Proc.
Checks the value @from_s returns with #check before returning it, so you know it satisfies this type.
173 174 175 176 177 178 179 |
# File 'lib/nrser/types/type.rb', line 173 def from_s s if @from_s.nil? raise NoMethodError, "#from_s not defined" end check @from_s.call( s ) end |
#has_from_data? ⇒ Boolean
215 216 217 |
# File 'lib/nrser/types/type.rb', line 215 def has_from_data? ! @from_data.nil? end |
#has_from_s? ⇒ Boolean
Test if the type knows how to load values from strings.
If this method returns ‘true`, then we expect #from_s to succeed.
197 198 199 |
# File 'lib/nrser/types/type.rb', line 197 def has_from_s? ! @from_s.nil? end |
#has_to_data? ⇒ Boolean
Test if the type has custom information about how to convert it’s values into “data” - structures and values suitable for transportation and storage (JSON, etc.).
If this method returns ‘true` then #to_data should succeed.
210 211 212 |
# File 'lib/nrser/types/type.rb', line 210 def has_to_data? ! @to_data.nil? end |
#name ⇒ Object
84 85 86 |
# File 'lib/nrser/types/type.rb', line 84 def name @name || default_name end |
#respond_to?(name, include_all = false) ⇒ Boolean
Overridden to customize behavior for the #from_s and #to_data methods - those methods are always defined, but we have #respond_to? return ‘false` if they lack the underlying instance variables needed to execute.
135 136 137 138 139 140 141 142 143 |
# File 'lib/nrser/types/type.rb', line 135 def respond_to? name, include_all = false if name == :from_s || name == 'from_s' has_from_s? elsif name == :to_data || name == 'to_data' has_to_data? else super name, include_all end end |
#test(value) ⇒ Object
92 93 94 |
# File 'lib/nrser/types/type.rb', line 92 def test value raise NotImplementedError end |
#to_data(value) ⇒ Object
Dumps a value of this type to “data” - structures and values suitable for transport and storage, such as dumping to JSON or YAML, etc.
229 230 231 232 233 234 235 |
# File 'lib/nrser/types/type.rb', line 229 def to_data value if @to_data.nil? raise NoMethodError, "#to_data not defined" end @to_data.call value end |
#to_s ⇒ String Also known as: inspect
Returns a brief string description of the type - just it’s #name surrounded by some back-ticks to make it easy to see where it starts and stops.
246 247 248 |
# File 'lib/nrser/types/type.rb', line 246 def to_s "`#{ name }`" end |