Class: RubyLess::TypedString

Inherits:
String
  • Object
show all
Defined in:
lib/typed_string.rb

Overview

This is a special kind of string containing ruby code that retains some information from the elements that compose it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = "", opts = nil) ⇒ TypedString

Returns a new instance of TypedString.



8
9
10
11
12
13
14
15
# File 'lib/typed_string.rb', line 8

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

Instance Attribute Details

#klassObject (readonly)

Resulting class of the evaluated ruby code if it is not nil.



18
19
20
# File 'lib/typed_string.rb', line 18

def klass
  @klass
end

#optsObject (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/typed_string.rb', line 6

def opts
  @opts
end

Instance Method Details

#append_argument(typed_string) ⇒ Object

Append a typed string to build an argument list



42
43
44
45
46
47
48
49
# File 'lib/typed_string.rb', line 42

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

#condObject

Condition that could yield a nil result in the whole expression. For example in the following expression:

var1.spouse.name == ''

“var1.spouse” would be the condition that could yield ‘nil’.



31
32
33
# File 'lib/typed_string.rb', line 31

def cond
  @opts[:cond]
end

#could_be_nil?Boolean

Returns true if the evaluation of the ruby code represented by the string could be ‘nil’.

Returns:



23
24
25
# File 'lib/typed_string.rb', line 23

def could_be_nil?
  @opts[:nil]
end

#rawObject

raw result without nil checking: “var1.spouse.name” instead of “(var1.spouse ? var1.spouse.name : nil)”



37
38
39
# File 'lib/typed_string.rb', line 37

def raw
  @opts[:raw] || self.to_s
end