Class: ToParsedObj::SingleParser

Inherits:
Object
  • Object
show all
Includes:
FromHash
Defined in:
lib/to_parsed_obj.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#convertorObject

Returns the value of attribute convertor.



8
9
10
# File 'lib/to_parsed_obj.rb', line 8

def convertor
  @convertor
end

#match_objObject

Returns the value of attribute match_obj.



8
9
10
# File 'lib/to_parsed_obj.rb', line 8

def match_obj
  @match_obj
end

#matcherObject

Returns the value of attribute matcher.



8
9
10
# File 'lib/to_parsed_obj.rb', line 8

def matcher
  @matcher
end

Instance Method Details

#match?(obj, m = matcher) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/to_parsed_obj.rb', line 9

def match?(obj,m=matcher)
  self.match_obj = if m.kind_of?(Regexp)
    m.match(obj.to_s.strip)
  elsif m.respond_to?(:call)
    m.call(obj)
  elsif m.kind_of?(Array)
    m.all? { |x| match?(obj,x) }
  elsif m.kind_of?(Class)
    obj.kind_of?(m)
  else
    m == obj
  end
  !!match_obj
end

#parse(obj) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/to_parsed_obj.rb', line 23

def parse(obj)
  if convertor.kind_of?(Symbol) || convertor.kind_of?(String)
    obj.send(convertor)
  elsif convertor.kind_of?(Class)
    convertor.new(obj)
  elsif convertor.respond_to?(:call)
    if convertor.arity == 1
      convertor.call(obj)
    elsif convertor.arity == 0
      convertor.call
    else
      convertor.call(obj,self)
    end
  else
    "dunno how to convert"
  end
end