Class: HashKeyValueOperator

Inherits:
Object
  • Object
show all
Defined in:
lib/cauldron/operator/hash_key_value_operator.rb

Overview

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indexes) ⇒ HashKeyValueOperator



7
8
9
10
11
# File 'lib/cauldron/operator/hash_key_value_operator.rb', line 7

def initialize(indexes)
  @indexes = indexes
  @constant = :foo
  #@constant, @indexes = constant, indexes
end

Class Method Details

.find_constants(problems) ⇒ Object



22
23
24
# File 'lib/cauldron/operator/hash_key_value_operator.rb', line 22

def self.find_constants(problems)
  problems.collect {|x| x.arguments.first.keys }.flatten
end

.uses_constants?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/cauldron/operator/hash_key_value_operator.rb', line 18

def self.uses_constants?
  true
end

.viable?(arguments, response) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/cauldron/operator/hash_key_value_operator.rb', line 13

def self.viable?(arguments, response)
  return false unless arguments.all? { |x| x.kind_of?(Hash) }
  true
end

Instance Method Details

#sexp_indexObject



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

def sexp_index
  if @constant.kind_of?(Symbol)
    a = [
          :symbol_literal, 
          [:symbol, [:@ident, @constant]],
          [:string_add, [:@ident, @constant]]
        ]
    return a      
  elsif @constant.kind_of?(String)
    return [
              :string_literal, 
              [
                :string_add, 
                [:string_content], 
                [:@tstring_content, @constant ]
              ]
          ] 
  else
    raise StandardError.new('Unknown index')
  end
end

#successful?(problem) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/cauldron/operator/hash_key_value_operator.rb', line 26

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

#to_ruby(scope, operators) ⇒ Object



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

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

#to_sexp(scope, operators) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cauldron/operator/hash_key_value_operator.rb', line 37

def to_sexp(scope, operators)
  [:aref,
    [:vcall, 
      [:@ident, scope[0]]
    ], 
    [:args_add_block, 
      [
        :args_add, 
        [:args_new], 
        sexp_index
      ]
    ]
  ]
end