class SheepAst::DataStore

User can store data or fetch data from the object. This is used by Let object's :record fuction

@api public

Public Class Methods

new() click to toggle source
Calls superclass method SheepAst::LetHelper::new
# File lib/sheep_ast/datastore.rb, line 24
def initialize
  @all_var = Set.new
  @ctime = Time.new
  # @temp_var = {}
  super()
end

Public Instance Methods

assign(sym, value, key = nil) click to toggle source
# File lib/sheep_ast/datastore.rb, line 37
def assign(sym, value, key = nil)
  t_sym = :"@#{sym}"

  is_defined = instance_variable_defined?(t_sym)

  if array_var(sym)
    usage and application_error 'trying to assing array with key' unless key.nil?
    instance_variable_set(t_sym, []) unless is_defined
    add(t_sym, value)
  elsif hash_l1_var(sym)
    usage and application_error 'trying to assign hash without key' if key.nil?
    instance_variable_set(t_sym, {}) unless is_defined
    add_pair(t_sym, key, value)
  elsif hash_var(sym)
    usage and application_error 'trying to assign hash without key' if key.nil?
    instance_variable_set(t_sym, {}) unless is_defined
    concat_pair(t_sym, key, value)
  elsif hash_arr_var(sym)
    usage and application_error 'trying to assign hash without key' if key.nil?
    instance_variable_set(t_sym, {}) unless is_defined
    add_arr_pair(t_sym, key, value)
  else
    unless key.nil?
      lfatal "key is not nil. Given sym should have _H suffix. sym = #{sym}"
      usage and application_error 'key is not nil, despite it is not enumerable'
    end
    instance_variable_set(t_sym, value)
  end

  unless is_defined
    @all_var << t_sym
  end
end
cleanup_all() click to toggle source
# File lib/sheep_ast/datastore.rb, line 93
def cleanup_all
  @all_var.each do |v|
    remove_instance_variable(v) if instance_variable_defined?(v)
  end
  @all_var = Set.new
end
compile(template_file, **options) click to toggle source
# File lib/sheep_ast/datastore.rb, line 100
def compile(template_file, **options)
  T.unsafe(self).let_compile(nil, self, template_file, **options)
end
Also aliased as: let_compile
dump(id = nil) click to toggle source
# File lib/sheep_ast/datastore.rb, line 161
def dump(id = nil)
  ldump dump_data(id).inspect
end
dump_data(id = nil) click to toggle source
# File lib/sheep_ast/datastore.rb, line 145
def dump_data(id = nil)
  data = {}
  if id.nil?
    @all_var.each do |elem|
      data[elem] = instance_variable_get(elem)
    end
  else
    data[id] = instance_variable_get(:"@#{id}")
  end
  return data
end
inspect() click to toggle source
# File lib/sheep_ast/datastore.rb, line 133
def inspect
  str = ''.dup
  str += "custom inspect : <#{self.class.name} object_id = #{object_id}, "
  str += dump_data.inspect
  str += '>'
  str
end
let_compile(template_file, **options)
Alias for: compile
remove(sym) click to toggle source
# File lib/sheep_ast/datastore.rb, line 76
def remove(sym)
  t_sym = :"@#{sym}"
  @all_var.delete(t_sym)
  remove_instance_variable(t_sym) if instance_variable_defined?(t_sym)
end
usage() click to toggle source
# File lib/sheep_ast/datastore.rb, line 112
def usage
  lfatal ''
  lfatal 'Please make sure the suffix of the store_id', :yellow
  lfatal ''
  lfatal 'Usage =========================================', :yellow
  lfatal '1. Use following store id depends on the types:', :yellow
  lfatal '  :xxx    - Hold single string', :yellow
  lfatal '  :xxx_A  - Hold Array of string', :yellow
  lfatal '  :xxx_H  - Hold Key Value pair of string. concat array, so dim is 1', :yellow
  lfatal '  :xxx_HA - Hold Key Value pair of string, push array so dim is 2', :yellow
  lfatal '  :xxx_HL - Hold Key and Last one Value pair of strin. One data', :yellow
  lfatal ''
  lfatal '  Note: let record_kv accept following kind:', :yellow
  lfatal '        xxx_H, xxx_HL, xxx_HA', :yellow
  lfatal ''
  lfatal '2. Available API', :yellow
  lfatal '   - compile: To compile given file with datastore data', :yellow
  lfatal '================================================', :yellow
  lfatal ''
end
value(sym) click to toggle source
# File lib/sheep_ast/datastore.rb, line 87
def value(sym)
  return val(:"@#{sym}")
end

Private Instance Methods

add(sym, value) click to toggle source
# File lib/sheep_ast/datastore.rb, line 177
def add(sym, value)
  val(sym).send(:<<, value)
end
add_arr_pair(sym, key, value) click to toggle source
# File lib/sheep_ast/datastore.rb, line 207
def add_arr_pair(sym, key, value)
  val_ = val(sym).send(:[], key)
  if val_.nil?
    val_ = []
  end
  val_ << value
  val(sym).send(:store, key, val_)
end
add_pair(sym, key, value) click to toggle source
# File lib/sheep_ast/datastore.rb, line 184
def add_pair(sym, key, value)
  val(sym).send(:store, key, value)
end
array_var(sym) click to toggle source
# File lib/sheep_ast/datastore.rb, line 219
def array_var(sym)
  return sym.to_s.end_with?('_A')
end
concat_pair(sym, key, value) click to toggle source
# File lib/sheep_ast/datastore.rb, line 191
def concat_pair(sym, key, value)
  val_ = val(sym).send(:[], key)
  if val_.nil?
    val_ = []
  end
  if value.is_a? Enumerable
    val_.concat(value)
  else
    val_ << value
  end
  val(sym).send(:store, key, val_)
end
ctime_get() click to toggle source
# File lib/sheep_ast/datastore.rb, line 244
def ctime_get
  @ctime
end
hash_arr_var(sym) click to toggle source
# File lib/sheep_ast/datastore.rb, line 240
def hash_arr_var(sym)
  return sym.to_s.end_with?('_HA')
end
hash_l1_var(sym) click to toggle source
# File lib/sheep_ast/datastore.rb, line 233
def hash_l1_var(sym)
  return sym.to_s.end_with?('_HL')
end
hash_var(sym) click to toggle source
# File lib/sheep_ast/datastore.rb, line 226
def hash_var(sym)
  return sym.to_s.end_with?('_H')
end
val(sym) click to toggle source
# File lib/sheep_ast/datastore.rb, line 170
def val(sym)
  return instance_variable_get(sym)
end