Class: StringHash

Inherits:
Hash
  • Object
show all
Includes:
RubyLess
Defined in:
app/models/string_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](value) ⇒ Object



18
19
20
# File 'app/models/string_hash.rb', line 18

def self.[](value)
  from_hash(value)
end

.from_hash(hash) ⇒ Object



10
11
12
13
14
15
16
# File 'app/models/string_hash.rb', line 10

def self.from_hash(hash)
  obj = new
  hash.each do |k,v|
    obj[k.to_s] = v.to_s
  end
  obj
end

.from_string(str) ⇒ Object



4
5
6
7
8
# File 'app/models/string_hash.rb', line 4

def self.from_string(str)
  from_hash(JSON.parse(str))
rescue
  from_hash({})
end

.json_create(serialized) ⇒ Object

Deserialization used by Property



23
24
25
26
27
28
29
# File 'app/models/string_hash.rb', line 23

def self.json_create(serialized)
  if data = serialized['data']
    StringHash[data]
  else
    nil
  end
end

Instance Method Details

#[]=(k, v) ⇒ Object



31
32
33
34
35
36
37
# File 'app/models/string_hash.rb', line 31

def []=(k, v)
  if v.blank?
    delete(k.to_s)
  else
    super(k.to_s, v.to_s)
  end
end

#merge(value) ⇒ Object



46
47
48
49
50
51
52
# File 'app/models/string_hash.rb', line 46

def merge(value)
  obj = dup
  value.each do |k,v|
    obj[k] = v
  end
  obj
end

#merge!(value) ⇒ Object



39
40
41
42
43
44
# File 'app/models/string_hash.rb', line 39

def merge!(value)
  value.each do |k,v|
    self[k] = v
  end
  self
end

#to_json(*args) ⇒ Object

Serialization used by Property



55
56
57
# File 'app/models/string_hash.rb', line 55

def to_json(*args)
  { 'json_class' => 'StringHash', 'data' => Hash[self] }.to_json
end

#to_sObject

This is used in case we show a form with the StringHash so that the value is not serialized to junk. This is the other side of “from_string”.



61
62
63
# File 'app/models/string_hash.rb', line 61

def to_s
  Hash[self].to_json
end