class Code

@example

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);
  # };

Constants

Inspectable

@!visibility private

NonCodePart

@api private @note used by {Code}, {::non_code} only.

Attributes

parts[R]

@!visibility private

Public Class Methods

new(parts, metadata = nil) click to toggle source

@api private @note used by {Code}, {::code}, {::non_code} only.

@param [Array<String, NonCodePart>] parts @param [Object, nil] metadata

# File lib/code.rb, line 51
def initialize(parts, metadata = nil)
  @parts = parts
  @metadata = metadata
end

Public Instance Methods

+(arg) click to toggle source

@overload + str

@param [String] str
@return [Code]

@overload + code

@param [Code] code
@return [Code]
# 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
<<(arg) click to toggle source

@overload << str

Appends +str+ to self.
@param [String] str
@return [self]

@overload << code

Appends +str+ to self.
@param [Code] code
@return [self]
# 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
inspect() click to toggle source

@return [String]

# File lib/code.rb, line 127
def inspect
  Inspectable.new(@parts, @metadata).inspect
end
map_non_code_parts(&f) click to toggle source

@yieldparam [Object] part @yieldreturn [String] @return [Code] a {Code} with {#non_code_parts} mapped by the passed block.

# 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(*args) click to toggle source

@overload metadata(obj)

@param [Object] obj
@return [Code] a {Code} with +obj+ attached to it. The +obj+ can later
  be retrieved with {#metadata}().

@overload metadata

@return [Object, nil] an {Object} attached to this {Code} with
  {#metadata}(obj) or nil if no {Object} was attached.
# File lib/code.rb, line 91
def metadata(*args)
  if args.empty?
  then @metadata
  else Code.new(@parts, args.first)
  end
end
non_code_parts() click to toggle source

@return [Enumerable<Object>] non-code parts of this {Code} introduced

with {::non_code}. See also {#map_non_code_parts}.
# File lib/code.rb, line 114
def non_code_parts
  @parts.select { |part| part.is_a? NonCodePart }.map(&:data)
end
to_s() click to toggle source

@return [String]

# 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