Module: OceanDynamo::Schema

Included in:
Table
Defined in:
lib/ocean-dynamo/schema.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, type = :string, default: nil, **extra) ⇒ Object



35
36
37
38
39
# File 'lib/ocean-dynamo/schema.rb', line 35

def attribute(name, type=:string, default: nil, **extra)
  raise DangerousAttributeError, "#{name} is defined by OceanDynamo" if self.dangerous_attributes.include?(name.to_s)
  attr_accessor name
  fields[name.to_s] = {type: type, default: default}.merge(extra)
end

#compute_table_nameObject



25
26
27
# File 'lib/ocean-dynamo/schema.rb', line 25

def compute_table_name
  name.pluralize.underscore.gsub('/', '_')
end

#define_attribute_accessors(name) ⇒ Object



17
18
19
20
21
22
# File 'lib/ocean-dynamo/schema.rb', line 17

def define_attribute_accessors(name)
  name = name.to_s
  self.class_eval "def #{name}; read_attribute('#{name}'); end"
  self.class_eval "def #{name}=(value); write_attribute('#{name}', value); end"
  self.class_eval "def #{name}?; read_attribute('#{name}').present?; end"
end

#dynamo_schemaObject


Class methods



10
11
12
13
14
# File 'lib/ocean-dynamo/schema.rb', line 10

def dynamo_schema(*)
  super
  # Finally return the full table name
  table_full_name
end

#global_secondary_index(hash_key, range_key = nil, projection: :keys_only, read_capacity_units: table_read_capacity_units, write_capacity_units: table_write_capacity_units) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ocean-dynamo/schema.rb', line 42

def global_secondary_index(hash_key, range_key=nil, 
                           projection: :keys_only,
                           read_capacity_units: table_read_capacity_units,
                           write_capacity_units: table_write_capacity_units)
  if range_key
    name = "#{hash_key}_#{range_key}_global"
    keys = [hash_key.to_s, range_key.to_s]
  else
    name = "#{hash_key}_global"
    keys = [hash_key.to_s]
  end
  self.global_secondary_indexes[name] = { 
    "keys" => keys, 
    "projection_type" => projection.to_s.upcase,
    "read_capacity_units" => read_capacity_units,
    "write_capacity_units" => write_capacity_units
  }   
end

#table_full_nameObject



30
31
32
# File 'lib/ocean-dynamo/schema.rb', line 30

def table_full_name
  "#{table_name_prefix}#{table_name}#{table_name_suffix}"
end