Class: JSON::Ext::Generator::State
- Inherits:
-
Object
- Object
- JSON::Ext::Generator::State
show all
- Defined in:
- lib/oj/state.rb
Overview
This class exists for json gem compatibility only. While it can be used as the options for other than compatibility a simple Hash is recommended as it is simpler and performs better. The only bit missing by not using a state object is the depth availability which may be the depth during dumping or maybe not since it can be set and the docs for depth= is the same as max_nesting. Note: Had to make this a subclass of Object instead of Hash like EashyHash due to conflicts with the json gem.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opts = {}) ⇒ State
Returns a new instance of State.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/oj/state.rb', line 22
def initialize(opts = {})
@attrs = {}
@attrs[:indent] = ''
@attrs[:space] = ''
@attrs[:space_before] = ''
@attrs[:array_nl] = ''
@attrs[:object_nl] = ''
@attrs[:allow_nan] = false
@attrs[:buffer_initial_length] = 1024 @attrs[:depth] = 0
@attrs[:max_nesting] = 100
@attrs[:check_circular?] = true
@attrs[:ascii_only] = false
@attrs.merge!(opts)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Boolean
Handles requests for Hash values. Others cause an Exception to be raised.
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/oj/state.rb', line 114
def method_missing(m, *args, &block)
if m.to_s.end_with?('=')
raise ArgumentError.new("wrong number of arguments (#{args.size} for 1 with #{m}) to method #{m}") if args.nil? or 1 != args.length
m = m.to_s[0..-2]
m = m.to_sym
return @attrs.store(m, args[0])
end
if @attrs.has_key?(m.to_sym)
raise ArgumentError.new("wrong number of arguments (#{args.size} for 0 with #{m}) to method #{m}") unless args.nil? or args.empty?
return @attrs[m.to_sym]
end
return @attrs.send(m, *args, &block)
end
|
Class Method Details
.from_state(opts) ⇒ Object
15
16
17
18
19
20
|
# File 'lib/oj/state.rb', line 15
def self.from_state(opts)
s = self.new()
s.clear()
s.merge(opts)
s
end
|
Instance Method Details
#[](key) ⇒ Object
91
92
93
94
|
# File 'lib/oj/state.rb', line 91
def [](key)
key = key.to_sym
@attrs.fetch(key, nil)
end
|
#[]=(key, value) ⇒ Object
96
97
98
99
|
# File 'lib/oj/state.rb', line 96
def []=(key, value)
key = key.to_sym
@attrs[key] = value
end
|
#allow_nan? ⇒ Boolean
51
52
53
|
# File 'lib/oj/state.rb', line 51
def allow_nan?()
@attrs[:allow_nan]
end
|
#ascii_only? ⇒ Boolean
55
56
57
|
# File 'lib/oj/state.rb', line 55
def ascii_only?()
@attrs[:ascii_only]
end
|
#buffer_initial_length=(len) ⇒ Object
74
75
76
77
|
# File 'lib/oj/state.rb', line 74
def buffer_initial_length=(len)
len = 1024 if 0 >= len
@attrs[:buffer_initial_length] = len
end
|
#clear ⇒ Object
101
102
103
|
# File 'lib/oj/state.rb', line 101
def clear()
@attrs.clear()
end
|
59
60
61
62
63
|
# File 'lib/oj/state.rb', line 59
def configure(opts)
raise TypeError.new('expected a Hash') unless opts.respond_to?(:to_h)
@attrs.merge!(opts.to_h)
end
|
#generate(obj) ⇒ Object
65
66
67
|
# File 'lib/oj/state.rb', line 65
def generate(obj)
JSON.generate(obj)
end
|
#has_key?(k) ⇒ Boolean
105
106
107
|
# File 'lib/oj/state.rb', line 105
def has_key?(k)
@attrs.has_key?(key.to_sym)
end
|
#merge(opts) ⇒ Object
69
70
71
|
# File 'lib/oj/state.rb', line 69
def merge(opts)
@attrs.merge!(opts)
end
|
#respond_to?(m, include_all = false) ⇒ Boolean
Replaces the Object.respond_to?() method.
83
84
85
86
87
88
89
|
# File 'lib/oj/state.rb', line 83
def respond_to?(m, include_all = false)
return true if super
return true if has_key?(key)
return true if has_key?(key.to_s)
has_key?(key.to_sym)
end
|
#to_h ⇒ Object
43
44
45
|
# File 'lib/oj/state.rb', line 43
def to_h()
return @attrs.dup
end
|
#to_hash ⇒ Object
47
48
49
|
# File 'lib/oj/state.rb', line 47
def to_hash()
return @attrs.dup
end
|