Class: Sitepress::Data::Record

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/sitepress/data.rb

Overview

Wraps a hash and returns managed elements

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Record

Returns a new instance of Record.



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

def initialize(hash)
  @hash = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sitepress/data.rb', line 72

def method_missing(name, *args, **kwargs, &block)
  if respond_to? name
    self.send name, *args, **kwargs, &block
  else
    key, modifier, _ = name.to_s.partition(/[!?]/)

    case modifier
    when ""
      self[key]
    when "!"
      self.fetch(key, *args, &block)
    when "?"
      !!self[key]
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



54
55
56
# File 'lib/sitepress/data.rb', line 54

def [](key)
  Data.manage(@hash[key.to_s])
end

#[]=(key, value) ⇒ Object



58
59
60
# File 'lib/sitepress/data.rb', line 58

def []=(key, value)
  Data.manage(@hash[key.to_s] = value)
end

#dig(*args, **kwargs, &block) ⇒ Object



89
90
91
# File 'lib/sitepress/data.rb', line 89

def dig(*args, **kwargs, &block)
  Data.manage @hash.dig(*args, **kwargs, &block)
end

#eachObject



62
63
64
65
66
# File 'lib/sitepress/data.rb', line 62

def each
  @hash.each do |key, value|
    yield key, Data.manage(value)
  end
end

#fetch(key, *args, &block) ⇒ Object



50
51
52
# File 'lib/sitepress/data.rb', line 50

def fetch(key, *args, &block)
  Data.manage(@hash.fetch(key.to_s, *args, &block))
end

#unmanageObject



68
69
70
# File 'lib/sitepress/data.rb', line 68

def unmanage
  @hash.transform_values { |value| Data.unmanage(value) }
end