Class: LoyalPassport::DataUtil

Inherits:
Hash
  • Object
show all
Defined in:
lib/loyal_passport/utils/data_util.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DataUtil

Returns a new instance of DataUtil.



4
5
6
# File 'lib/loyal_passport/utils/data_util.rb', line 4

def initialize(options={})
  self.merge! options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/loyal_passport/utils/data_util.rb', line 44

def method_missing method_name, *args, &block
  # 如果方法是赋值, 则直接赋值
  if method_name.to_s.end_with?('=')
    self[method_name.to_s.chop!] = args.first
  else
    result = self[method_name.to_sym]

    if result.is_a?(Hash) && !(result.is_a?(self.class))
      self[method_name] = self.class.new(result)
    elsif result.nil?
      self[method_name] = self.class.new
    else
      result
    end
  end
end

Instance Method Details

#[](name) ⇒ Object



15
16
17
# File 'lib/loyal_passport/utils/data_util.rb', line 15

def [](name)
  super(name.to_sym)
end

#[]=(name, value) ⇒ Object



8
9
10
# File 'lib/loyal_passport/utils/data_util.rb', line 8

def []=(name, value)
  super(name.to_sym, value)
end

#deep_dupObject



40
41
42
# File 'lib/loyal_passport/utils/data_util.rb', line 40

def deep_dup
  impl_deep_dup self
end

#deep_merge(other_hash) ⇒ Object



19
20
21
# File 'lib/loyal_passport/utils/data_util.rb', line 19

def deep_merge(other_hash)
  self.deep_dup.deep_merge!(other_hash)
end

#deep_merge!(other_hash) ⇒ Object

深层的合并



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/loyal_passport/utils/data_util.rb', line 24

def deep_merge!(other_hash)
  other_hash = self.class.new(other_hash) unless other_hash.is_a?(self.class)

  other_hash.each_pair do |k,v|
    tv = self[k]

    unless v.is_a?(self.class)
      v = self.class.new(v) if v.is_a?(Hash)
    end

    self[k] = tv.is_a?(self.class) && v.is_a?(self.class) ? tv.deep_merge(v) : v
  end

  self
end

#stringify_keys!Object



61
62
63
# File 'lib/loyal_passport/utils/data_util.rb', line 61

def stringify_keys!
  impl_stringify_keys! self
end

#symbolize_keys!Object



65
66
67
# File 'lib/loyal_passport/utils/data_util.rb', line 65

def symbolize_keys!
  impl_symbolize_keys! self
end

#to_aryObject



12
13
# File 'lib/loyal_passport/utils/data_util.rb', line 12

def to_ary
end