Class: Lexigrapher::Parser
- Inherits:
-
Object
- Object
- Lexigrapher::Parser
- Defined in:
- lib/lexigrapher.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#v ⇒ Object
readonly
Returns the value of attribute v.
Instance Method Summary collapse
- #array? ⇒ Boolean
- #date? ⇒ Boolean
- #email? ⇒ Boolean
- #float? ⇒ Boolean
- #gender? ⇒ Boolean
- #hash? ⇒ Boolean
-
#initialize(value) ⇒ Parser
constructor
A new instance of Parser.
-
#int? ⇒ Boolean
As much as I don’t like Rescue for flow control, it is considerably nicer than the potential REGEXP that would have replaced it.
- #ipv4? ⇒ Boolean
- #ipv6? ⇒ Boolean
-
#methods ⇒ Object
Define the fallthrough order.
- #nil? ⇒ Boolean
- #phone? ⇒ Boolean
-
#str? ⇒ Boolean
Fallbacks.
- #symbol? ⇒ Boolean
-
#try_types(methods) ⇒ Object
Tail Recurse through all methods specified.
-
#unknown? ⇒ Boolean
Emergency Fallback.
- #url? ⇒ Boolean
Constructor Details
#initialize(value) ⇒ Parser
Returns a new instance of Parser.
8 9 10 11 |
# File 'lib/lexigrapher.rb', line 8 def initialize(value) @v = value @type = value ? try_types(methods) : :nil end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/lexigrapher.rb', line 6 def type @type end |
#v ⇒ Object (readonly)
Returns the value of attribute v.
6 7 8 |
# File 'lib/lexigrapher.rb', line 6 def v @v end |
Instance Method Details
#array? ⇒ Boolean
53 54 55 |
# File 'lib/lexigrapher.rb', line 53 def array? :array if v.match(StrRegexp::ARRAY) end |
#date? ⇒ Boolean
45 46 47 |
# File 'lib/lexigrapher.rb', line 45 def date? :date if v.match(StrRegexp::DATE) end |
#email? ⇒ Boolean
61 62 63 |
# File 'lib/lexigrapher.rb', line 61 def email? :email if v.match(StrRegexp::EMAIL) end |
#float? ⇒ Boolean
37 38 39 |
# File 'lib/lexigrapher.rb', line 37 def float? :float if Float(v) rescue nil end |
#gender? ⇒ Boolean
77 78 79 |
# File 'lib/lexigrapher.rb', line 77 def gender? :gender if v.match(StrRegexp::GENDER) end |
#hash? ⇒ Boolean
49 50 51 |
# File 'lib/lexigrapher.rb', line 49 def hash? :hash if v.match(StrRegexp::HASH) end |
#int? ⇒ Boolean
As much as I don’t like Rescue for flow control, it is considerably nicer than the potential REGEXP that would have replaced it. The same can be said for everything using Rescue below.
33 34 35 |
# File 'lib/lexigrapher.rb', line 33 def int? :integer if Integer(v) rescue nil end |
#ipv4? ⇒ Boolean
69 70 71 |
# File 'lib/lexigrapher.rb', line 69 def ipv4? :ipv4 if v.match(StrRegexp::IPV4) end |
#ipv6? ⇒ Boolean
73 74 75 |
# File 'lib/lexigrapher.rb', line 73 def ipv6? :ipv6 if v.match(StrRegexp::IPV6) end |
#methods ⇒ Object
Define the fallthrough order. I would rather be explicit here, so as for customization.
22 23 24 25 26 27 28 |
# File 'lib/lexigrapher.rb', line 22 def methods [ :int?, :float?, :symbol?, :hash?, :array?, # Primatives :date?, :phone?, :email?, :url?, :ipv4?, :ipv6?, :gender?, # Extensions :str?, :nil?, :unknown? # Primitives fallback ] end |
#nil? ⇒ Boolean
87 88 89 |
# File 'lib/lexigrapher.rb', line 87 def nil? :nil if v.nil? end |
#phone? ⇒ Boolean
57 58 59 |
# File 'lib/lexigrapher.rb', line 57 def phone? :phone if v.match(StrRegexp::PHONE) end |
#str? ⇒ Boolean
Fallbacks
83 84 85 |
# File 'lib/lexigrapher.rb', line 83 def str? :str if v end |
#symbol? ⇒ Boolean
41 42 43 |
# File 'lib/lexigrapher.rb', line 41 def symbol? :symbol if v[0] == ':' end |
#try_types(methods) ⇒ Object
Tail Recurse through all methods specified
14 15 16 17 18 19 |
# File 'lib/lexigrapher.rb', line 14 def try_types(methods) method, remaining = methods.first, methods[1..-1] type = self.send method type ? type : try_types(remaining) end |
#unknown? ⇒ Boolean
Emergency Fallback
93 94 95 |
# File 'lib/lexigrapher.rb', line 93 def unknown? :unknown end |
#url? ⇒ Boolean
65 66 67 |
# File 'lib/lexigrapher.rb', line 65 def url? :url if v.match(StrRegexp::URL) end |