Class: Lexigrapher::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/lexigrapher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/lexigrapher.rb', line 6

def type
  @type
end

#vObject (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

Returns:

  • (Boolean)


53
54
55
# File 'lib/lexigrapher.rb', line 53

def array?
  :array if v.match(StrRegexp::ARRAY)
end

#date?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/lexigrapher.rb', line 45

def date?
  :date if v.match(StrRegexp::DATE)
end

#email?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/lexigrapher.rb', line 61

def email?
  :email if v.match(StrRegexp::EMAIL)
end

#float?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/lexigrapher.rb', line 37

def float?
  :float if Float(v) rescue nil
end

#gender?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/lexigrapher.rb', line 77

def gender?
  :gender if v.match(StrRegexp::GENDER)
end

#hash?Boolean

Returns:

  • (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.

Returns:

  • (Boolean)


33
34
35
# File 'lib/lexigrapher.rb', line 33

def int?
  :integer if Integer(v) rescue nil
end

#ipv4?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/lexigrapher.rb', line 69

def ipv4?
  :ipv4 if v.match(StrRegexp::IPV4)
end

#ipv6?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/lexigrapher.rb', line 73

def ipv6?
  :ipv6 if v.match(StrRegexp::IPV6)
end

#methodsObject

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

Returns:

  • (Boolean)


87
88
89
# File 'lib/lexigrapher.rb', line 87

def nil?
  :nil if v.nil?
end

#phone?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/lexigrapher.rb', line 57

def phone?
  :phone if v.match(StrRegexp::PHONE)
end

#str?Boolean

Fallbacks

Returns:

  • (Boolean)


83
84
85
# File 'lib/lexigrapher.rb', line 83

def str?
  :str if v
end

#symbol?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


93
94
95
# File 'lib/lexigrapher.rb', line 93

def unknown?
  :unknown
end

#url?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/lexigrapher.rb', line 65

def url?
  :url if v.match(StrRegexp::URL)
end