module TinyDyno::Document::ClassMethods
Public Instance Methods
where(options = {})
click to toggle source
TODO, extract into its own class to allow better testing
# File lib/tiny_dyno/document.rb, line 57 def where(options = {}) validate_option_keys(options) get_query = build_where_query(options) attributes = TinyDyno::Adapter.get_item(get_item_request: get_query) if attributes.nil? return nil else record = self.new(attributes) record.instance_variable_set(:@new_record, false) record.instance_variable_set(:@changed_attributes, {}) record end end
Private Instance Methods
build_where_query(options)
click to toggle source
minimimum implementation for now build simple query to retrieve document via get_item docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#get_item-instance_method
# File lib/tiny_dyno/document.rb, line 91 def build_where_query(options) query_keys = {} options.each do |k,v| # as expected by DynamoDB typed_key = k.to_s query_keys[typed_key] = dyno_typed_key(key: typed_key, val: v) end { table_name: self.table_name, attributes_to_get: attribute_names, key: query_keys } end
valid_field_selector?(field_name:)
click to toggle source
# File lib/tiny_dyno/document.rb, line 83 def valid_field_selector?(field_name:) key_schema.map {|k| k[:attribute_name] }.include?(field_name) end
validate_option_keys(options)
click to toggle source
minimimum implementation for now check that each option key relates to a hash_key present on the model do not permit scan queries
# File lib/tiny_dyno/document.rb, line 76 def validate_option_keys(options) raise TinyDyno::Errors::HashKeyOnly.new(klass: self.class, name: 'primary_key') if primary_key.nil? options.keys.each do |key| raise TinyDyno::Errors::InvalidSelector.new(klass: self.class, name: key) unless valid_field_selector?(field_name: key.to_s) end end