Class: Hiiro::PinRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/pin_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_table!(db) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/hiiro/pin_record.rb', line 7

def self.create_table!(db)
  db.create_table?(:pins) do
    primary_key :id
    String :command, null: false
    String :key, null: false
    String :value_json
    unique [:command, :key]
  end
end

.find_key(cmd, key) ⇒ Object



29
# File 'lib/hiiro/pin_record.rb', line 29

def self.find_key(cmd, key) = where(command: cmd.to_s, key: key.to_s).first

.for_command(cmd) ⇒ Object



28
# File 'lib/hiiro/pin_record.rb', line 28

def self.for_command(cmd) = where(command: cmd.to_s).all

Instance Method Details

#valueObject

Value accessor — tries JSON parse, falls back to raw string



18
19
20
21
22
# File 'lib/hiiro/pin_record.rb', line 18

def value
  Hiiro::DB::JSON.load(value_json)
rescue
  value_json
end

#value=(v) ⇒ Object



24
25
26
# File 'lib/hiiro/pin_record.rb', line 24

def value=(v)
  self.value_json = v.is_a?(String) ? v : Hiiro::DB::JSON.dump(v)
end