Class: Sneaql::Core::RecordsetManager

Inherits:
Object
  • Object
show all
Defined in:
lib/sneaql_lib/recordset.rb

Overview

manages stored recordsets in sneaql transforms

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression_manager, logger = nil) ⇒ RecordsetManager

Returns a new instance of RecordsetManager.



9
10
11
12
13
# File 'lib/sneaql_lib/recordset.rb', line 9

def initialize(expression_manager, logger = nil)
  @logger = logger ? logger : Logger.new(STDOUT)
  @expression_manager = expression_manager
  @recordset = {}
end

Instance Attribute Details

#recordsetObject (readonly)

Returns the value of attribute recordset.



7
8
9
# File 'lib/sneaql_lib/recordset.rb', line 7

def recordset
  @recordset
end

Instance Method Details

#evaluate_expression_against_record(record, expressions) ⇒ Boolean

applies a conditional expression set against a record.

Parameters:

  • record (Hash)
  • expressions (Array<Hash>)

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sneaql_lib/recordset.rb', line 88

def evaluate_expression_against_record(record, expressions)
  conditions = []
  expressions.each do |exp|
    @logger.debug("applying #{exp} to #{record}")
    raw_result = @expression_manager.compare_expressions(
      exp[:operator],
      @expression_manager.evaluate_expression(record[exp[:field]]),
      @expression_manager.evaluate_expression(exp[:expression])
    )
    if exp[:condition] == 'include'
      conditions << raw_result
    elsif exp[:condition] == 'exclude'
      conditions << !raw_result
    end
  end
  return !conditions.include?(false)
end

#parse_recordset_expression(args) ⇒ Array<Hash>

Parses a recordset expression.

Parameters:

  • args (Array<Hash>)

Returns:

  • (Array<Hash>)


77
78
79
80
81
82
# File 'lib/sneaql_lib/recordset.rb', line 77

def parse_recordset_expression(args)
  # takes in argument array as an argument
  # returns array of expressions to be checked at run time
  args.delete_at(0) # get rid of the first element, recordset ref
  args.each_slice(4).to_a.map{ |x| { condition: x[0].downcase, field: x[1], operator: x[2], expression: x[3]} }
end

#recordset_name_conflicts_with_variables?(name) ⇒ Boolean

Validates that the recordset name doesn’t conflict with session var names

Parameters:

  • name (String)

Returns:

  • (Boolean)


70
71
72
# File 'lib/sneaql_lib/recordset.rb', line 70

def recordset_name_conflicts_with_variables?(name)
  @expression_manager.session_variables.key?(name)
end

#recordset_valid?(rs) ⇒ Boolean

Validates recordset. Must be an array of hashes with identical keys.

Parameters:

  • rs (Array<Hash>)

    recordset to validate

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sneaql_lib/recordset.rb', line 34

def recordset_valid?(rs)
  return false unless rs.class == Array
  # empty recordset is valid
  return true if rs == []
  r1 = rs[0].keys

  rs.each do |record|
    return false unless record.class == Hash
    return false unless r1 == record.keys
    record.keys {|k| puts k; return false unless valid_element_data_types.include?(record[k].class)}
  end
  true
end

#remove_recordset(name) ⇒ Object

Deletes a recordset if it exists

Parameters:

  • name (String)

    name for recordset to delete



26
27
28
29
30
# File 'lib/sneaql_lib/recordset.rb', line 26

def remove_recordset(name)
  if @recordset.has_key?(name)
    @recordset.delete(name)
  end
end

#store_recordset(name, rs) ⇒ Object

Stores a recordset if it is in a valid format.

Parameters:

  • name (String)

    name for recordset

  • rs (Array<Hash>)

    recordset to store

Raises:

  • (Sneaql::RecordsetIsNotAnArray)


18
19
20
21
22
# File 'lib/sneaql_lib/recordset.rb', line 18

def store_recordset(name, rs)
  raise Sneaql::RecordsetIsNotAnArray unless rs.class == Array
  raise Sneaql::RecordsetContainsInconsistentOrInvalidTypes unless recordset_valid?(rs)
  recordset[name] = rs
end

#valid_element_data_typesArray<Class>

Ruby data types that are valid as recordset fields.

Returns:

  • (Array<Class>)


50
51
52
# File 'lib/sneaql_lib/recordset.rb', line 50

def valid_element_data_types
  [Fixnum, String, Float]
end

#valid_recordset_name?(name) ⇒ Boolean

Validates that the string will make a valid recordset name.

Parameters:

  • name (String)

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
# File 'lib/sneaql_lib/recordset.rb', line 57

def valid_recordset_name?(name)
  return false unless name.match(/^\w+/)
  h = {}
  h[name] == 1
rescue
  return false
else
  return true
end