Class: Code

Inherits:
Object show all
Defined in:
lib/code.rb

Overview

Examples:


method_names = {
  :eat => "M1",
  :cut => "M2"
}

c = code <<
  "class Apple \n" <<
  "{ \n" <<
  "public: \n" <<
  "    void " << non_code(:eat) << "(); \n" <<
  "    void " << non_code(:cut) << "(int PiecesCount); \n" <<
  "}; \n"
c.non_code_parts.each do |part|
  if part.is_a? Symbol then
    if not method_names.key? part then
      raise "method name not found!"
    end
  end
end
c = c.map_non_code_parts do |part|
  if part.is_a? Symbol then
    method_names[part]
  end
end
puts c
  # class Apple 
  # { 
  # public: 
  #     void M1(); 
  #     void M2(int PiecesCount); 
  # }; 

Instance Method Summary collapse

Instance Method Details

#+(str) ⇒ Code #+(code) ⇒ Code



62
63
64
65
66
67
# File 'lib/code.rb', line 62

def + arg
  case arg
  when String then self + Code.new([arg])
  when Code then Code.new(self.parts + arg.parts)
  end
end

#<<(str) ⇒ self #<<(code) ⇒ self

Overloads:

  • #<<(str) ⇒ self

    Appends str to self.

  • #<<(code) ⇒ self

    Appends str to self.



77
78
79
80
81
82
# File 'lib/code.rb', line 77

def << arg
  case arg
  when String then self << Code.new([arg])
  when Code then @parts.concat(arg.parts); self
  end
end

#inspectString



127
128
129
# File 'lib/code.rb', line 127

def inspect
  Inspectable.new(@parts, ).inspect
end

#map_non_code_parts {|part| ... } ⇒ Code

Returns a Code with #non_code_parts mapped by the passed block.

Yield Parameters:

Yield Returns:

  • (String)


101
102
103
104
105
106
107
108
109
110
# File 'lib/code.rb', line 101

def map_non_code_parts(&f)
  Code.new(
    @parts.map do |part|
      case part
      when String then part
      when NonCodePart then f.(part.data)
      end
    end
  )
end

#metadata(obj) ⇒ Code #metadataObject?



91
92
93
94
95
96
# File 'lib/code.rb', line 91

def (*args)
  if args.empty?
  then 
  else Code.new(@parts, args.first)
  end
end

#non_code_partsEnumerable<Object>



114
115
116
# File 'lib/code.rb', line 114

def non_code_parts
  @parts.select { |part| part.is_a? NonCodePart }.map(&:data)
end

#to_sString



119
120
121
122
123
124
# File 'lib/code.rb', line 119

def to_s
  if (x = @parts.find { |part| part.is_a? NonCodePart }) then
    raise "non-code part: #{x.inspect}"
  end
  @parts.join
end