Class: RubyLess::TypedString
- Inherits:
-
String
- Object
- String
- RubyLess::TypedString
- 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
-
#klass ⇒ Object
readonly
Resulting class of the evaluated ruby code if it is not nil.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#append_argument(typed_string) ⇒ Object
Append a typed string to build an argument list.
-
#cond ⇒ Object
Condition that could yield a nil result in the whole expression.
-
#could_be_nil? ⇒ Boolean
Returns true if the evaluation of the ruby code represented by the string could be ‘nil’.
-
#initialize(content = "", opts = nil) ⇒ TypedString
constructor
A new instance of TypedString.
-
#raw ⇒ Object
raw result without nil checking: “var1.spouse.name” instead of “(var1.spouse ? var1.spouse.name : nil)”.
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
#klass ⇒ Object (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 |
#opts ⇒ Object (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 |
#cond ⇒ Object
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’.
23 24 25 |
# File 'lib/typed_string.rb', line 23 def could_be_nil? @opts[:nil] end |
#raw ⇒ Object
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 |