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



16
17
18
# File 'app/models/string_hash.rb', line 16

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

.from_hash(hash) ⇒ Object



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

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
# File 'app/models/string_hash.rb', line 4

def self.from_string(str)
  from_hash(JSON.parse(str))
end

.json_create(serialized) ⇒ Object

Deserialization used by Property



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

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

Instance Method Details

#[]=(k, v) ⇒ Object



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

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

#merge(value) ⇒ Object



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

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

#merge!(value) ⇒ Object



37
38
39
40
41
42
# File 'app/models/string_hash.rb', line 37

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

#to_json(*args) ⇒ Object

Serialization used by Property



53
54
55
# File 'app/models/string_hash.rb', line 53

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”.



59
60
61
# File 'app/models/string_hash.rb', line 59

def to_s
  Hash[self].to_json
end