Class: OpenObject

Inherits:
Hash
  • Object
show all
Defined in:
lib/ruby_ext/core/open_object.rb

Constant Summary collapse

PUBLIC_METHODS =

delete methods

%w(
  as send each each_pair size is_a? clone dup empty? blank? present? merge merge! stringify_keys stringify_keys! symbolize_keys symbolize_keys! to_query
).collect{|m| m.to_sym}
PUBLIC_METHODS_RE =
/(^__|^object_|^must|^stub)/

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, arg = nil, &block) ⇒ Object (protected)



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ruby_ext/core/open_object.rb', line 136

def method_missing m, arg = nil, &block
  type = m[-1,1]
  if type == '='
    self[m[0..-2]] = arg
  elsif type == '!'
    warn 'deprecated'
    self[m[0..-2]]
  elsif type == '?'
    !self[m[0..-2]].blank?
  else
    self[m]
  end
end

Class Method Details

.initialize_from(hash, deep = false) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby_ext/core/open_object.rb', line 109

def self.initialize_from hash, deep = false
  unless deep
    ::OpenObject.new.update hash
  else
    r = ::OpenObject.new
    hash.each do |k, v|
      r[k] = if v.is_a? Hash
        v.to_openobject
      else
        v
      end
    end
    r
  end
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/ruby_ext/core/open_object.rb', line 61

def == other
  return false unless other.respond_to?(:each) and other.respond_to?(:size) and other.respond_to?(:[]) and self.size == other.size
  other.each do |k, v|
    return false unless self[k] == v
  end
  true
end

#[](k) ⇒ Object



73
74
75
# File 'lib/ruby_ext/core/open_object.rb', line 73

def [] k
  super k.to_sym
end

#[]=(k, v) ⇒ Object



69
70
71
# File 'lib/ruby_ext/core/open_object.rb', line 69

def []= k, v
  super k.to_sym, v
end

#deep_cloneObject



128
129
130
131
132
133
# File 'lib/ruby_ext/core/open_object.rb', line 128

def deep_clone
  clone = super
  clone.clear
  each{|k, v| clone[k.deep_clone] = v.deep_clone}
  clone
end

#delete(key) ⇒ Object



57
58
59
# File 'lib/ruby_ext/core/open_object.rb', line 57

def delete key
  super key.to_sym
end

#each(&block) ⇒ Object



37
# File 'lib/ruby_ext/core/open_object.rb', line 37

def each █ super(&block) end

#extractable_options?Boolean

support :extract_options for OpenObject (Rails integration)

Returns:

  • (Boolean)


126
# File 'lib/ruby_ext/core/open_object.rb', line 126

def extractable_options?; true end

#include?(k) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/ruby_ext/core/open_object.rb', line 77

def include? k
  super k.to_sym
end

#inspectObject



11
12
13
14
# File 'lib/ruby_ext/core/open_object.rb', line 11

def inspect
  # "#<#{self.class}:#{self.object_id} #{super}>"
  "<#{super}>"
end

#merge(other) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/ruby_ext/core/open_object.rb', line 39

def merge other
  d = dup
  other.each{|k, v| d[k] = v}
  d
  # d = dup
  # d.send(:update, other)
  # d
end

#merge!(other) ⇒ Object



48
49
50
# File 'lib/ruby_ext/core/open_object.rb', line 48

def merge! other
  other.each{|k, v| self[k] = v}
end

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/ruby_ext/core/open_object.rb', line 105

def respond_to? m
  true
end

#shouldObject

hack to works well with RSpec



102
# File 'lib/ruby_ext/core/open_object.rb', line 102

def should; super end

#should_notObject



103
# File 'lib/ruby_ext/core/open_object.rb', line 103

def should_not; super end

#to_aObject



16
# File 'lib/ruby_ext/core/open_object.rb', line 16

def to_a; super end

#to_hash(deep = false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruby_ext/core/open_object.rb', line 81

def to_hash deep = false
  unless deep
    {}.update(self)
  else
    h = {}
    each do |k, v|
      if v.is_a? OpenObject
        h[k] = v.to_hash(true)
      else
        h[k] = v
      end
    end
    h
  end
end

#to_json(*args) ⇒ Object



97
98
99
# File 'lib/ruby_ext/core/open_object.rb', line 97

def to_json *args
  to_hash.to_json *args
end

#to_openobject(deep = false) ⇒ Object Also known as: to_oo



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_ext/core/open_object.rb', line 20

def to_openobject deep = false
  unless deep
    self
  else
    r = OpenObject.new
    each do |k, v|
      r[k] = if v.is_a? Hash
        v.to_openobject
      else
        v
      end
    end
    r
  end
end

#to_procObject



18
# File 'lib/ruby_ext/core/open_object.rb', line 18

def to_proc; super end

#update(other) ⇒ Object



52
53
54
55
# File 'lib/ruby_ext/core/open_object.rb', line 52

def update other
  other.to_hash.each{|k,v| self[k.to_sym] = v}
  self
end