Class: Get::Parser

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

Constant Summary collapse

CLASS_REGEX =
/^(.*)(By|From|JoinedWith)(.*)/
ALL_CLASS_REGEX =
/^(All)(.*)/
CLASS_NAME_BY_ERROR =
'You have multiple instances of "By" in your class. Please use open-ended form ie. Get::UserBy.run(params)'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
12
13
# File 'lib/get/parser.rb', line 8

def initialize(class_name)
  raise Get::Errors::InvalidClassName.new(CLASS_NAME_BY_ERROR) if class_name.to_s.split('By').length > 2

  @class_name = class_name
  @match = match_result
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



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

def class_name
  @class_name
end

#methodObject

Returns the value of attribute method.



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

def method
  @method
end

#result_entityObject

Returns the value of attribute result_entity.



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

def result_entity
  @result_entity
end

Instance Method Details

#keyObject



19
20
21
# File 'lib/get/parser.rb', line 19

def key
  @key_string.present? ? @key_string.symbolize : nil
end

#match?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/get/parser.rb', line 15

def match?
  !!@match
end

#match_resultObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/get/parser.rb', line 23

def match_result
  @match = class_name.to_s.match(ALL_CLASS_REGEX)
  if @match
    @method, @result_entity = @match.values_at(1, 2)
  else
    @match = class_name.to_s.match(CLASS_REGEX)
    @result_entity, @method, @key_string = @match.values_at(1, 2, 3) if @match
  end
  @match
end