Class: RubyLess::TypedString
- Inherits:
-
String
- Object
- String
- RubyLess::TypedString
- Defined in:
- lib/ruby_less/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’.
-
#hash ⇒ Object
Hash arguments.
-
#initialize(content = "", klass_or_opts = nil) ⇒ TypedString
constructor
A new instance of TypedString.
-
#keys ⇒ Object
Used to keep hash order (this is useful for testing).
-
#list ⇒ Object
List of typed_strings that form the argument list.
-
#literal ⇒ Object
Return the literal value (string before inspect, number).
-
#raw ⇒ Object
raw result without nil checking: “node.spouse.name” instead of “(node.spouse ? node.spouse.name : nil)”.
- #rebuild_arguments ⇒ Object
- #rebuild_hash ⇒ Object
- #set_hash(key, value) ⇒ Object
Constructor Details
#initialize(content = "", klass_or_opts = nil) ⇒ TypedString
Returns a new instance of TypedString.
8 9 10 11 12 13 14 15 |
# File 'lib/ruby_less/typed_string.rb', line 8 def initialize(content = "", klass_or_opts = nil) klass_or_opts ||= {:class => String} replace(content) @opts = klass_or_opts.kind_of?(Hash) ? klass_or_opts.dup : {:class => klass_or_opts} 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/ruby_less/typed_string.rb', line 18 def klass @klass end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
6 7 8 |
# File 'lib/ruby_less/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
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ruby_less/typed_string.rb', line 64 def append_argument(typed_string) self.list << 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:
node.spouse.name == ''
“node.spouse” would be the condition that could yield ‘nil’.
31 32 33 |
# File 'lib/ruby_less/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/ruby_less/typed_string.rb', line 23 def could_be_nil? @opts[:nil] end |
#hash ⇒ Object
Hash arguments. This is only used to resolve parameter insertion with append_hash.
48 49 50 |
# File 'lib/ruby_less/typed_string.rb', line 48 def hash @hash ||= {} end |
#keys ⇒ Object
Used to keep hash order (this is useful for testing).
53 54 55 |
# File 'lib/ruby_less/typed_string.rb', line 53 def keys @hash_keys ||= [] end |
#list ⇒ Object
List of typed_strings that form the argument list. This is only used to resolve nil when the receiver of the arguments accepts nil values.
42 43 44 |
# File 'lib/ruby_less/typed_string.rb', line 42 def list @list ||= self.empty? ? [] : [self] end |
#literal ⇒ Object
Return the literal value (string before inspect, number)
36 37 38 |
# File 'lib/ruby_less/typed_string.rb', line 36 def literal @opts[:literal] end |
#raw ⇒ Object
raw result without nil checking: “node.spouse.name” instead of “(node.spouse ? node.spouse.name : nil)”
59 60 61 |
# File 'lib/ruby_less/typed_string.rb', line 59 def raw @opts[:raw] || self.to_s end |
#rebuild_arguments ⇒ Object
75 76 77 |
# File 'lib/ruby_less/typed_string.rb', line 75 def rebuild_arguments replace(list.map {|arg| arg.raw}.join(', ')) if @list end |
#rebuild_hash ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/ruby_less/typed_string.rb', line 86 def rebuild_hash if @hash result = [] @hash_keys.each do |k| result << "#{k.inspect} => #{@hash[k]}" end replace "{#{result.join(', ')}}" end end |
#set_hash(key, value) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/ruby_less/typed_string.rb', line 79 def set_hash(key, value) self.hash[key] = value self.keys << key unless self.keys.include?(key) @opts[:class] = {} unless self.klass.kind_of?(Hash) self.klass[key] = value.klass end |