Class: ConcatOperator

Inherits:
Object
  • Object
show all
Includes:
Cauldron::Operator
Defined in:
lib/cauldron/operator/concat_operator.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cauldron::Operator

included

Constructor Details

#initialize(indexes) ⇒ ConcatOperator

Returns a new instance of ConcatOperator.



5
6
7
8
# File 'lib/cauldron/operator/concat_operator.rb', line 5

def initialize(indexes)
  @indexes = indexes
  @constant = 'bar'
end

Class Method Details

.find_constants(problems) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/cauldron/operator/concat_operator.rb', line 17

def self.find_constants(problems)
  problems.examples.inject([]) do |total, x| 
    result = x.response.gsub( Regexp.new('^'+x.arguments.first),'')
    total << result unless result == x.response
    total
  end.uniq
end

.uses_block?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/cauldron/operator/concat_operator.rb', line 29

def self.uses_block?
  false
end

.uses_constants?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/cauldron/operator/concat_operator.rb', line 25

def self.uses_constants?
  true
end

.viable?(arguments, response) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/cauldron/operator/concat_operator.rb', line 10

def self.viable?(arguments, response)
  return false unless arguments.all? { |x| x.kind_of?(String) }
  return false unless response.kind_of?(String)
  # TODO - Only accpets one argument
  true
end

Instance Method Details

#branch?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/cauldron/operator/concat_operator.rb', line 33

def branch?
  false
end

#build(operators, scope) ⇒ Object



49
50
51
# File 'lib/cauldron/operator/concat_operator.rb', line 49

def build(operators, scope)
  to_sexp(scope)
end

#successful?(problem) ⇒ Boolean

Operator for “x.concat(”bar“)”

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/cauldron/operator/concat_operator.rb', line 38

def successful?(problem)
  if (problem[:arguments].first + @constant) == problem[:response]
    return true
  end
  return false
end

#to_ruby(scope, operators) ⇒ Object



45
46
47
# File 'lib/cauldron/operator/concat_operator.rb', line 45

def to_ruby(scope, operators)
  Sorcerer.source self.to_sexp(scope, operators)
end

#to_sexp(scope, operators) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cauldron/operator/concat_operator.rb', line 53

def to_sexp(scope, operators)
  first_variable = 'var'+@indexes[0].to_s
  [:program,
   [:stmts_add,
    [:stmts_new],
    [:method_add_arg,
     [:call,
      [:vcall, [:@ident, first_variable ]],
      :".",
      [:@ident, "concat"]],
     [:arg_paren,
      [:args_add_block,
       [:args_add,
        [:args_new],
        [:string_literal,
         [:string_add, [:string_content], [:@tstring_content, @constant]]]],
       false]]]]]
end