Class: MustacheRender::Mustache::Data

Inherits:
Hash
  • Object
show all
Defined in:
lib/mustache_render/mustache/data.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Data

Returns a new instance of Data.



4
5
6
# File 'lib/mustache_render/mustache/data.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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mustache_render/mustache/data.rb', line 50

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



21
22
23
# File 'lib/mustache_render/mustache/data.rb', line 21

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

#[]=(name, value) ⇒ Object



14
15
16
# File 'lib/mustache_render/mustache/data.rb', line 14

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

#deep_dupObject



46
47
48
# File 'lib/mustache_render/mustache/data.rb', line 46

def deep_dup
  impl_deep_dup self
end

#deep_merge(other_hash) ⇒ Object



25
26
27
# File 'lib/mustache_render/mustache/data.rb', line 25

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

#deep_merge!(other_hash) ⇒ Object

深层的合并



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mustache_render/mustache/data.rb', line 30

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



67
68
69
# File 'lib/mustache_render/mustache/data.rb', line 67

def stringify_keys!
  impl_stringify_keys! self
end

#symbolize_keys!Object



71
72
73
# File 'lib/mustache_render/mustache/data.rb', line 71

def symbolize_keys!
  impl_symbolize_keys! self
end

#to_aryObject



18
19
# File 'lib/mustache_render/mustache/data.rb', line 18

def to_ary
end