Sha256: e7dc4e494bd5a218b847b50abb4cda1fdeb1a3962da5656b8bc4a6e6886f15c2

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

module RubyLess
  
  # This is a special kind of string containing ruby code that retains some information from the
  # elements that compose it.
  class TypedString < String
    attr_reader :klass, :opts

    def initialize(content = "", opts = nil)
      opts ||= {:class => String}
      replace(content)
      @opts = opts.dup
      if could_be_nil? && !@opts[:cond]
        @opts[:cond] = [self.to_s]
      end
    end
    
    # Resulting class of the evaluated ruby code if it is not nil.
    def klass
      @opts[:class]
    end
    
    # Returns true if the evaluation of the ruby code represented by the string could be 'nil'.
    def could_be_nil?
      @opts[:nil]
    end
    
    # Condition that could yield a nil result in the whole expression.
    # For example in the following expression:
    #  node.spouse.name == ''
    # "node.spouse" would be the condition that could yield 'nil'.
    def cond
      @opts[:cond]
    end
    
    # raw result without nil checking:
    # "node.spouse.name" instead of "(node.spouse ? node.spouse.name : nil)"
    def raw
      @opts[:raw] || self.to_s
    end
    
    # Append a typed string to build an argument list
    def append_argument(typed_string)
      append_opts(typed_string)
      if self.empty?
        replace(typed_string.raw)
      else
        replace("#{self.raw}, #{typed_string.raw}")
      end
    end
    
    private
      def append_opts(typed_string)
        if self.empty?
          @opts = typed_string.opts.dup
        else
          if klass.kind_of?(Array)
            klass << typed_string.klass
          else
            @opts[:class] = [klass, typed_string.klass]
          end
          append_cond(typed_string.cond) if typed_string.could_be_nil?
        end
      end
    
      def append_cond(condition)
        @opts[:cond] ||= []
        @opts[:cond] += [condition].flatten
        @opts[:cond].uniq!
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubyless-0.4.0 lib/ruby_less/typed_string.rb