Class: Fable::StringValue

Inherits:
Value show all
Defined in:
lib/fable/value.rb

Instance Attribute Summary collapse

Attributes inherited from Value

#value_object

Attributes inherited from RuntimeObject

#original_object, #own_debug_metadata, #parent, #path

Instance Method Summary collapse

Methods inherited from Value

#bad_cast_exception, #copy, create, #to_s

Methods inherited from RuntimeObject

#compact_path_string, #convert_path_to_relative, #copy, #debug_line_number_of_path, #debug_metadata, #indentation_string, #resolve_path, #root_content_container

Constructor Details

#initialize(*args) ⇒ StringValue

Returns a new instance of StringValue.



144
145
146
147
148
149
150
# File 'lib/fable/value.rb', line 144

def initialize(*args)
  if args.size == 1
    self.initialize_with_string(args[0])
  else
    super("")
  end
end

Instance Attribute Details

#is_inline_whitespaceObject Also known as: is_inline_whitespace?

Returns the value of attribute is_inline_whitespace.



127
128
129
# File 'lib/fable/value.rb', line 127

def is_inline_whitespace
  @is_inline_whitespace
end

#is_newlineObject Also known as: is_newline?

Returns the value of attribute is_newline.



127
128
129
# File 'lib/fable/value.rb', line 127

def is_newline
  @is_newline
end

Instance Method Details

#cast(new_type) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/fable/value.rb', line 167

def cast(new_type)
  if new_type == self.class
    return self
  end

  if new_type == IntValue
    begin
      return IntValue.new(Integer(self.value))
    rescue ArgumentError => e
      return nil
    end
  end

  if new_type == FloatValue
    begin
      return FloatValue.new(Float(self.value))
    rescue ArgumentError => e
      return nil
    end
  end

  raise bad_cast_exception(new_type)
end

#initialize_with_string(value) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/fable/value.rb', line 152

def initialize_with_string(value)
  #classify whitespace status
  self.is_newline = (value == "\n")
  self.is_inline_whitespace = true

  value.each_char do |character|
    if character != ' ' && character != "\t"
      self.is_inline_whitespace = false
      break
    end
  end

  self.value = value
end

#is_nonwhitespace?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/fable/value.rb', line 140

def is_nonwhitespace?
  return !is_newline? && !is_inline_whitespace?
end

#truthy?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/fable/value.rb', line 136

def truthy?
  return value.length > 0
end

#value_typeObject



132
133
134
# File 'lib/fable/value.rb', line 132

def value_type
  return OrderedValueTypes[StringValue]
end