class Dynamodb::Relation

Constants

OPERATOR_MAP
QUERY_INSTANCE_VARS
QUERY_METHODS

Attributes

attribute_expressors[R]
consistent_read[RW]
expression_attribute_names[R]
expression_attribute_values[R]
filter_expression[R]
index_name[R]
key_condition_expression[R]
offset_key[R]
projection_expression[R]
scan_index_forward[RW]
source[RW]

Public Class Methods

new(source) click to toggle source
# File lib/dynamodb/relation.rb, line 35
def initialize(source)
  @source = source
  @attribute_expressors = {}
  @consistent_read = false
  @scan_index_forward = true
end

Public Instance Methods

all() click to toggle source
# File lib/dynamodb/relation.rb, line 63
def all
  source._query(build_query)
end
exclusive_start_key() click to toggle source
# File lib/dynamodb/relation.rb, line 89
def exclusive_start_key
  @offset_key
end
limit(int) click to toggle source
# File lib/dynamodb/relation.rb, line 51
def limit(int)
  @_limit = int
  build_expressions
  self
end
query(args = {}) click to toggle source
# File lib/dynamodb/relation.rb, line 71
def query(args = {})
  source._query(args)
end
select(*args) click to toggle source
# File lib/dynamodb/relation.rb, line 57
def select(*args)
  @projection_expression = args.flatten
  build_expressions
  self
end
to_query() click to toggle source
# File lib/dynamodb/relation.rb, line 67
def to_query
  build_query
end
where(args) click to toggle source
# File lib/dynamodb/relation.rb, line 42
def where(args)
  args.each do |k, v|
    setter = "#{k}=".to_sym
    self.has_method?(setter) ? self.send(setter, v) : add_attribute_query(k,v)
  end
  build_expressions
  self
end

Protected Instance Methods

has_method?(meth) click to toggle source
# File lib/dynamodb/relation.rb, line 95
def has_method?(meth)
  self.class.private_method_defined?(meth)
end
not_empty?(val) click to toggle source
# File lib/dynamodb/relation.rb, line 99
def not_empty?(val)
  !!val == val || !val.to_s.strip.empty?
end

Private Instance Methods

_define_expression_type(key) click to toggle source
# File lib/dynamodb/relation.rb, line 185
def _define_expression_type(key)
  schema = _expression_schema.detect { |x| x[:attribute_name] == key.to_s }
  schema ? "_#{schema[:key_type].downcase}_expression=" : "_filter_expression="
end
_expression_schema() click to toggle source
# File lib/dynamodb/relation.rb, line 190
def _expression_schema
  return source.key_schema if index_name.nil?

  index = source.indexes.detect { |x| x[:index_name] == index_name }
  index[:key_schema]
end
_filter_expression=(k, v) click to toggle source
# File lib/dynamodb/relation.rb, line 178
def _filter_expression=(k, v)
  f_name = add_attribute_name(k.to_s)
  f_op = OPERATOR_MAP[v.keys[0]]
  f_value = add_attribute_value(v.values[0])
  @filter_expression << "#{f_name} #{f_op} #{f_value}"
end
_hash_expression=(k, v) click to toggle source
# File lib/dynamodb/relation.rb, line 165
def _hash_expression=(k, v)
  h_name = add_attribute_name(k.to_s)
  h_value = add_attribute_value(v)
  @key_condition_expression << "#{h_name} = #{h_value}"
end
_range_expression=(k, v) click to toggle source
# File lib/dynamodb/relation.rb, line 171
def _range_expression=(k, v)
  r_name = add_attribute_name(k.to_s)
  r_op = OPERATOR_MAP[v.keys[0]]
  r_value = add_attribute_value(v.values[0])
  @key_condition_expression << "#{r_name} #{r_op} #{r_value}"
end
add_attribute_name(name) click to toggle source
# File lib/dynamodb/relation.rb, line 130
def add_attribute_name(name)
  ni = @expression_attribute_names.size + 1
  @expression_attribute_names.merge!({ "#n#{ni}" => name })
  "#n#{ni}"
end
add_attribute_query(attr, val) click to toggle source
# File lib/dynamodb/relation.rb, line 145
def add_attribute_query(attr, val)
  @attribute_expressors.merge!({ attr => val })
end
add_attribute_value(values) click to toggle source
# File lib/dynamodb/relation.rb, line 136
def add_attribute_value(values)
  values = [values].flatten # Set to an array to handle BETWEEN operator
  values.each_with_object([]) do |value, obj|
    vi = @expression_attribute_values.size + 1
    @expression_attribute_values.merge!({ ":v#{vi}" => value })
    obj << ":v#{vi}"
  end.join(" AND ")
end
build_expressions() click to toggle source
# File lib/dynamodb/relation.rb, line 149
def build_expressions
  reset_expressions

  attribute_expressors.each do |k, v|
    expression_type = _define_expression_type(k).to_sym
    self.send(expression_type, k, v)
  end
end
build_query() click to toggle source
# File lib/dynamodb/relation.rb, line 115
def build_query
  query = {}
  query.merge!({ table_name: source.table_name })
  build_expressions
  QUERY_METHODS.each do |meth|
    val = self.send(meth)
    query.merge!({ meth => val }) if not_empty?(val)
  end
  QUERY_INSTANCE_VARS.each do |var|
    val = self.instance_variable_get("@_#{var.to_s}")
    query.merge!({ var => val }) if not_empty?(val)
  end
  query
end
index_name=(val) click to toggle source

Can be set to nil

# File lib/dynamodb/relation.rb, line 106
def index_name=(val)
  @index_name = val.nil? ? val : val.to_s
end
offset_key=(val) click to toggle source

Builds the exclusive_start_key if val is a Hash

# File lib/dynamodb/relation.rb, line 111
def offset_key=(val)
  @offset_key = val.stringify_keys if val.is_a?(Hash)
end
reset_expressions() click to toggle source
# File lib/dynamodb/relation.rb, line 158
def reset_expressions
  @expression_attribute_names = {}
  @expression_attribute_values = {}
  @key_condition_expression = []
  @filter_expression = []
end