Module: Dynameek::Model::Structure
- Included in:
- ClassMethods
- Defined in:
- lib/dynameek/model/structure.rb
Instance Method Summary collapse
- #aws_item_to_hash(item) ⇒ Object
- #check_field(fieldname) ⇒ Object
- #current_range(key, range_val) ⇒ Object
- #field(fieldname, type) ⇒ Object
- #fields ⇒ Object
- #hash_key(fieldname) ⇒ Object
- #hash_key_info ⇒ Object
- #index_table? ⇒ Boolean
- #multi_column_hash_key(fieldnames) ⇒ Object
- #multi_column_hash_key? ⇒ Boolean
- #multi_column_hash_key_fields ⇒ Object
- #multi_column_join ⇒ Object
- #multi_column_join=(join) ⇒ Object
- #range(fieldname) ⇒ Object
- #range? ⇒ Boolean
- #range_info ⇒ Object
- #read_units ⇒ Object
- #read_write(vals) ⇒ Object
- #use_index_table ⇒ Object
- #write_units ⇒ Object
Instance Method Details
#aws_item_to_hash(item) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/dynameek/model/structure.rb', line 110 def aws_item_to_hash(item) (item.is_a?(AWS::DynamoDB::Item) || item.is_a?(AWS::DynamoDB::ItemData) ? item.attributes.to_hash : item ) end |
#check_field(fieldname) ⇒ Object
143 144 145 |
# File 'lib/dynameek/model/structure.rb', line 143 def check_field(fieldname) raise ("#{fieldname} is not a recognised field") if fields[fieldname].nil? end |
#current_range(key, range_val) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/dynameek/model/structure.rb', line 117 def current_range(key, range_val) return nil if !index_table? index_table.load_schema key.join!(multi_column_join) if key.is_a?(Array) item = index_table.items.query( { hash_value: key, select: :all, range_value: range_val }).first return 0 if item.nil? item_hsh = aws_item_to_hash(item) item_hsh['current_range_val'].to_i end |
#field(fieldname, type) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/dynameek/model/structure.rb', line 66 def field fieldname, type fieldname = fieldname.to_sym type = type.to_sym fields[fieldname] = type define_method(fieldname.to_s) { read_attribute(fieldname) } define_method("#{fieldname.to_s}?") { !read_attribute(fieldname).nil? } define_method("#{fieldname.to_s}=") {|value| write_attribute(fieldname, value) } end |
#fields ⇒ Object
5 6 7 |
# File 'lib/dynameek/model/structure.rb', line 5 def fields @fields ||= {} end |
#hash_key(fieldname) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/dynameek/model/structure.rb', line 75 def hash_key fieldname fieldname = fieldname.to_sym check_field(fieldname) hash_key_info.field = fieldname hash_key_info.type = fields[fieldname] define_method(:hash_key) { read_attribute(fieldname) } end |
#hash_key_info ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/dynameek/model/structure.rb', line 9 def hash_key_info if(@hash_key.nil?) @hash_key = OpenStruct.new @hash_key.field = nil @hash_key.type = nil end @hash_key end |
#index_table? ⇒ Boolean
27 28 29 |
# File 'lib/dynameek/model/structure.rb', line 27 def index_table? @uses_index_table|| false end |
#multi_column_hash_key(fieldnames) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dynameek/model/structure.rb', line 83 def multi_column_hash_key fieldnames fieldnames = fieldnames.map(&:to_sym) fieldnames.each{|f| check_field(f)} fieldnames.each {|f| multi_column_hash_key_fields << f} fieldname = fieldnames.map(&:to_s).join("_").to_sym fields[fieldname] = :string define_method(:hash_key) do self.class.multi_column_hash_key_fields.reduce([]) do |memo, field| memo << attributes[field] memo end.join(self.class.multi_column_join) end alias_method fieldname, :hash_key hash_key_info.field = fieldname hash_key_info.type = :string end |
#multi_column_hash_key? ⇒ Boolean
62 63 64 |
# File 'lib/dynameek/model/structure.rb', line 62 def multi_column_hash_key? !multi_column_hash_key_fields.empty? end |
#multi_column_hash_key_fields ⇒ Object
50 51 52 |
# File 'lib/dynameek/model/structure.rb', line 50 def multi_column_hash_key_fields @multi_column_hash_key_fields ||= [] end |
#multi_column_join ⇒ Object
54 55 56 |
# File 'lib/dynameek/model/structure.rb', line 54 def multi_column_join @multi_column_join ||= "|" end |
#multi_column_join=(join) ⇒ Object
58 59 60 |
# File 'lib/dynameek/model/structure.rb', line 58 def multi_column_join=(join) @multi_column_join =join end |
#range(fieldname) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/dynameek/model/structure.rb', line 100 def range fieldname fieldname = fieldname.to_sym check_field(fieldname) range_info.field = fieldname range_info.type = fields[fieldname] define_method(:range) do attributes[self.class.range_info.field] end end |
#range? ⇒ Boolean
32 33 34 |
# File 'lib/dynameek/model/structure.rb', line 32 def range? !range_info.field.nil? end |
#range_info ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/dynameek/model/structure.rb', line 18 def range_info if(@range.nil?) @range = OpenStruct.new @range.field = nil @range.type = nil end @range end |
#read_units ⇒ Object
36 37 38 39 |
# File 'lib/dynameek/model/structure.rb', line 36 def read_units @read_write ||= [10, 5] @read_write[0] end |
#read_write(vals) ⇒ Object
46 47 48 |
# File 'lib/dynameek/model/structure.rb', line 46 def read_write(vals) @read_write = vals end |
#use_index_table ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/dynameek/model/structure.rb', line 133 def use_index_table define_method(:act_hash_key) do [self.class.convert_to_dynamodb(self.class.hash_key_info.type, hash_key), self.class.multi_column_join, self.class.convert_to_dynamodb(self.class.range_info.type, range)].join('') end field :dynameek_index, :number @uses_index_table = true end |
#write_units ⇒ Object
41 42 43 44 |
# File 'lib/dynameek/model/structure.rb', line 41 def write_units @read_write ||= [10, 5] @read_write[1] end |