mack-data_mapper 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/lib/gems/addressable-2.0.0/lib/addressable/idna.rb +4867 -0
  2. data/lib/gems/addressable-2.0.0/lib/addressable/uri.rb +2469 -0
  3. data/lib/gems/addressable-2.0.0/lib/addressable/version.rb +35 -0
  4. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/adapters/data_objects_adapter.rb +85 -0
  5. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/aggregate_functions.rb +201 -0
  6. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/collection.rb +11 -0
  7. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/model.rb +11 -0
  8. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/repository.rb +7 -0
  9. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/support/symbol.rb +21 -0
  10. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/version.rb +7 -0
  11. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates.rb +15 -0
  12. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/abstract_adapter.rb +209 -0
  13. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/data_objects_adapter.rb +709 -0
  14. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/in_memory_adapter.rb +87 -0
  15. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/mysql_adapter.rb +136 -0
  16. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/postgres_adapter.rb +188 -0
  17. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/sqlite3_adapter.rb +105 -0
  18. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters.rb +22 -0
  19. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/many_to_many.rb +147 -0
  20. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/many_to_one.rb +107 -0
  21. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/one_to_many.rb +318 -0
  22. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/one_to_one.rb +61 -0
  23. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/relationship.rb +223 -0
  24. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/relationship_chain.rb +81 -0
  25. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations.rb +200 -0
  26. data/lib/gems/dm-core-0.9.7/lib/dm-core/auto_migrations.rb +105 -0
  27. data/lib/gems/dm-core-0.9.7/lib/dm-core/collection.rb +642 -0
  28. data/lib/gems/dm-core-0.9.7/lib/dm-core/dependency_queue.rb +32 -0
  29. data/lib/gems/dm-core-0.9.7/lib/dm-core/hook.rb +11 -0
  30. data/lib/gems/dm-core-0.9.7/lib/dm-core/identity_map.rb +42 -0
  31. data/lib/gems/dm-core-0.9.7/lib/dm-core/is.rb +16 -0
  32. data/lib/gems/dm-core-0.9.7/lib/dm-core/logger.rb +232 -0
  33. data/lib/gems/dm-core-0.9.7/lib/dm-core/migrations/destructive_migrations.rb +17 -0
  34. data/lib/gems/dm-core-0.9.7/lib/dm-core/migrator.rb +29 -0
  35. data/lib/gems/dm-core-0.9.7/lib/dm-core/model.rb +488 -0
  36. data/lib/gems/dm-core-0.9.7/lib/dm-core/naming_conventions.rb +84 -0
  37. data/lib/gems/dm-core-0.9.7/lib/dm-core/property.rb +663 -0
  38. data/lib/gems/dm-core-0.9.7/lib/dm-core/property_set.rb +169 -0
  39. data/lib/gems/dm-core-0.9.7/lib/dm-core/query.rb +628 -0
  40. data/lib/gems/dm-core-0.9.7/lib/dm-core/repository.rb +159 -0
  41. data/lib/gems/dm-core-0.9.7/lib/dm-core/resource.rb +637 -0
  42. data/lib/gems/dm-core-0.9.7/lib/dm-core/scope.rb +58 -0
  43. data/lib/gems/dm-core-0.9.7/lib/dm-core/support/array.rb +13 -0
  44. data/lib/gems/dm-core-0.9.7/lib/dm-core/support/assertions.rb +8 -0
  45. data/lib/gems/dm-core-0.9.7/lib/dm-core/support/errors.rb +23 -0
  46. data/lib/gems/dm-core-0.9.7/lib/dm-core/support/kernel.rb +11 -0
  47. data/lib/gems/dm-core-0.9.7/lib/dm-core/support/symbol.rb +41 -0
  48. data/lib/gems/dm-core-0.9.7/lib/dm-core/support.rb +7 -0
  49. data/lib/gems/dm-core-0.9.7/lib/dm-core/transaction.rb +267 -0
  50. data/lib/gems/dm-core-0.9.7/lib/dm-core/type.rb +160 -0
  51. data/lib/gems/dm-core-0.9.7/lib/dm-core/type_map.rb +80 -0
  52. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/boolean.rb +7 -0
  53. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/discriminator.rb +34 -0
  54. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/object.rb +24 -0
  55. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/paranoid_boolean.rb +34 -0
  56. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/paranoid_datetime.rb +33 -0
  57. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/serial.rb +9 -0
  58. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/text.rb +10 -0
  59. data/lib/gems/dm-core-0.9.7/lib/dm-core/types.rb +19 -0
  60. data/lib/gems/dm-core-0.9.7/lib/dm-core/version.rb +3 -0
  61. data/lib/gems/dm-core-0.9.7/lib/dm-core.rb +217 -0
  62. data/lib/gems/dm-core-0.9.7/script/all +5 -0
  63. data/lib/gems/dm-core-0.9.7/script/performance.rb +284 -0
  64. data/lib/gems/dm-core-0.9.7/script/profile.rb +87 -0
  65. data/lib/gems/dm-migrations-0.9.7/lib/dm-migrations/version.rb +5 -0
  66. data/lib/gems/dm-migrations-0.9.7/lib/dm-migrations.rb +1 -0
  67. data/lib/gems/dm-migrations-0.9.7/lib/migration.rb +215 -0
  68. data/lib/gems/dm-migrations-0.9.7/lib/migration_runner.rb +88 -0
  69. data/lib/gems/dm-migrations-0.9.7/lib/spec/example/migration_example_group.rb +73 -0
  70. data/lib/gems/dm-migrations-0.9.7/lib/spec/matchers/migration_matchers.rb +107 -0
  71. data/lib/gems/dm-migrations-0.9.7/lib/sql/column.rb +9 -0
  72. data/lib/gems/dm-migrations-0.9.7/lib/sql/mysql.rb +52 -0
  73. data/lib/gems/dm-migrations-0.9.7/lib/sql/postgresql.rb +78 -0
  74. data/lib/gems/dm-migrations-0.9.7/lib/sql/sqlite3.rb +43 -0
  75. data/lib/gems/dm-migrations-0.9.7/lib/sql/table.rb +19 -0
  76. data/lib/gems/dm-migrations-0.9.7/lib/sql/table_creator.rb +81 -0
  77. data/lib/gems/dm-migrations-0.9.7/lib/sql/table_modifier.rb +53 -0
  78. data/lib/gems/dm-migrations-0.9.7/lib/sql.rb +10 -0
  79. data/lib/gems/dm-observer-0.9.7/lib/dm-observer/version.rb +5 -0
  80. data/lib/gems/dm-observer-0.9.7/lib/dm-observer.rb +91 -0
  81. data/lib/gems/dm-serializer-0.9.7/lib/dm-serializer/version.rb +5 -0
  82. data/lib/gems/dm-serializer-0.9.7/lib/dm-serializer.rb +183 -0
  83. data/lib/gems/dm-timestamps-0.9.7/lib/dm-timestamps/version.rb +5 -0
  84. data/lib/gems/dm-timestamps-0.9.7/lib/dm-timestamps.rb +57 -0
  85. data/lib/gems/dm-types-0.9.7/lib/dm-types/bcrypt_hash.rb +31 -0
  86. data/lib/gems/dm-types-0.9.7/lib/dm-types/csv.rb +28 -0
  87. data/lib/gems/dm-types-0.9.7/lib/dm-types/enum.rb +70 -0
  88. data/lib/gems/dm-types-0.9.7/lib/dm-types/epoch_time.rb +27 -0
  89. data/lib/gems/dm-types-0.9.7/lib/dm-types/file_path.rb +27 -0
  90. data/lib/gems/dm-types-0.9.7/lib/dm-types/flag.rb +61 -0
  91. data/lib/gems/dm-types-0.9.7/lib/dm-types/ip_address.rb +30 -0
  92. data/lib/gems/dm-types-0.9.7/lib/dm-types/json.rb +40 -0
  93. data/lib/gems/dm-types-0.9.7/lib/dm-types/regexp.rb +20 -0
  94. data/lib/gems/dm-types-0.9.7/lib/dm-types/serial.rb +8 -0
  95. data/lib/gems/dm-types-0.9.7/lib/dm-types/slug.rb +37 -0
  96. data/lib/gems/dm-types-0.9.7/lib/dm-types/uri.rb +29 -0
  97. data/lib/gems/dm-types-0.9.7/lib/dm-types/uuid.rb +64 -0
  98. data/lib/gems/dm-types-0.9.7/lib/dm-types/version.rb +5 -0
  99. data/lib/gems/dm-types-0.9.7/lib/dm-types/yaml.rb +36 -0
  100. data/lib/gems/dm-types-0.9.7/lib/dm-types.rb +28 -0
  101. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/absent_field_validator.rb +60 -0
  102. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/acceptance_validator.rb +76 -0
  103. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/auto_validate.rb +153 -0
  104. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/block_validator.rb +60 -0
  105. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/confirmation_validator.rb +80 -0
  106. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/contextual_validators.rb +56 -0
  107. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/custom_validator.rb +72 -0
  108. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/format_validator.rb +97 -0
  109. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/formats/email.rb +40 -0
  110. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/formats/url.rb +20 -0
  111. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/generic_validator.rb +100 -0
  112. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/length_validator.rb +113 -0
  113. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/method_validator.rb +68 -0
  114. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/numeric_validator.rb +83 -0
  115. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/primitive_validator.rb +60 -0
  116. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/required_field_validator.rb +88 -0
  117. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/support/object.rb +5 -0
  118. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/uniqueness_validator.rb +64 -0
  119. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/validation_errors.rb +63 -0
  120. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/version.rb +5 -0
  121. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/within_validator.rb +53 -0
  122. data/lib/gems/dm-validations-0.9.7/lib/dm-validations.rb +234 -0
  123. data/lib/gems/json_pure-1.1.3/GPL +340 -0
  124. data/lib/gems/json_pure-1.1.3/VERSION +1 -0
  125. data/lib/gems/json_pure-1.1.3/bin/edit_json.rb +10 -0
  126. data/lib/gems/json_pure-1.1.3/bin/prettify_json.rb +76 -0
  127. data/lib/gems/json_pure-1.1.3/lib/json/Array.xpm +21 -0
  128. data/lib/gems/json_pure-1.1.3/lib/json/FalseClass.xpm +21 -0
  129. data/lib/gems/json_pure-1.1.3/lib/json/Hash.xpm +21 -0
  130. data/lib/gems/json_pure-1.1.3/lib/json/Key.xpm +73 -0
  131. data/lib/gems/json_pure-1.1.3/lib/json/NilClass.xpm +21 -0
  132. data/lib/gems/json_pure-1.1.3/lib/json/Numeric.xpm +28 -0
  133. data/lib/gems/json_pure-1.1.3/lib/json/String.xpm +96 -0
  134. data/lib/gems/json_pure-1.1.3/lib/json/TrueClass.xpm +21 -0
  135. data/lib/gems/json_pure-1.1.3/lib/json/add/core.rb +135 -0
  136. data/lib/gems/json_pure-1.1.3/lib/json/add/rails.rb +58 -0
  137. data/lib/gems/json_pure-1.1.3/lib/json/common.rb +354 -0
  138. data/lib/gems/json_pure-1.1.3/lib/json/editor.rb +1362 -0
  139. data/lib/gems/json_pure-1.1.3/lib/json/ext.rb +13 -0
  140. data/lib/gems/json_pure-1.1.3/lib/json/json.xpm +1499 -0
  141. data/lib/gems/json_pure-1.1.3/lib/json/pure/generator.rb +394 -0
  142. data/lib/gems/json_pure-1.1.3/lib/json/pure/parser.rb +259 -0
  143. data/lib/gems/json_pure-1.1.3/lib/json/pure.rb +75 -0
  144. data/lib/gems/json_pure-1.1.3/lib/json/version.rb +9 -0
  145. data/lib/gems/json_pure-1.1.3/lib/json.rb +235 -0
  146. data/lib/gems/launchy-0.3.2/bin/launchy +12 -0
  147. data/lib/gems/launchy-0.3.2/lib/launchy/application.rb +163 -0
  148. data/lib/gems/launchy-0.3.2/lib/launchy/browser.rb +85 -0
  149. data/lib/gems/launchy-0.3.2/lib/launchy/command_line.rb +48 -0
  150. data/lib/gems/launchy-0.3.2/lib/launchy/gemspec.rb +53 -0
  151. data/lib/gems/launchy-0.3.2/lib/launchy/specification.rb +133 -0
  152. data/lib/gems/launchy-0.3.2/lib/launchy/version.rb +18 -0
  153. data/lib/gems/launchy-0.3.2/lib/launchy.rb +58 -0
  154. data/lib/gems/uuidtools-1.0.3/lib/uuidtools/version.rb +32 -0
  155. data/lib/gems/uuidtools-1.0.3/lib/uuidtools.rb +648 -0
  156. data/lib/gems.rb +13 -0
  157. data/lib/mack-data_mapper/migration_generator/migration_generator.rb +5 -0
  158. data/lib/mack-data_mapper/migration_generator/templates/db/migrations/%=@migration_name%.rb.template +1 -1
  159. data/lib/mack-data_mapper/model_generator/manifest.yml +3 -3
  160. data/lib/mack-data_mapper/model_generator/model_generator.rb +8 -1
  161. data/lib/mack-data_mapper/model_generator/templates/model.rb.template +1 -1
  162. data/lib/mack-data_mapper/model_generator/templates/rspec.rb.template +1 -1
  163. data/lib/mack-data_mapper/model_generator/templates/test_case.rb.template +1 -1
  164. data/lib/mack-data_mapper.rb +3 -2
  165. data/lib/mack-data_mapper_tasks.rb +7 -0
  166. metadata +235 -86
@@ -0,0 +1,354 @@
1
+ require 'json/version'
2
+
3
+ module JSON
4
+ class << self
5
+ # If _object_ is string-like parse the string and return the parsed result
6
+ # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
7
+ # data structure object and return it.
8
+ #
9
+ # The _opts_ argument is passed through to generate/parse respectively, see
10
+ # generate and parse for their documentation.
11
+ def [](object, opts = {})
12
+ if object.respond_to? :to_str
13
+ JSON.parse(object.to_str, opts => {})
14
+ else
15
+ JSON.generate(object, opts => {})
16
+ end
17
+ end
18
+
19
+ # Returns the JSON parser class, that is used by JSON. This might be either
20
+ # JSON::Ext::Parser or JSON::Pure::Parser.
21
+ attr_reader :parser
22
+
23
+ # Set the JSON parser class _parser_ to be used by JSON.
24
+ def parser=(parser) # :nodoc:
25
+ @parser = parser
26
+ remove_const :Parser if const_defined? :Parser
27
+ const_set :Parser, parser
28
+ end
29
+
30
+ # Return the constant located at _path_. The format of _path_ has to be
31
+ # either ::A::B::C or A::B::C. In any case A has to be located at the top
32
+ # level (absolute namespace path?). If there doesn't exist a constant at
33
+ # the given path, an ArgumentError is raised.
34
+ def deep_const_get(path) # :nodoc:
35
+ path = path.to_s
36
+ path.split(/::/).inject(Object) do |p, c|
37
+ case
38
+ when c.empty? then p
39
+ when p.const_defined?(c) then p.const_get(c)
40
+ else raise ArgumentError, "can't find const #{path}"
41
+ end
42
+ end
43
+ end
44
+
45
+ # Set the module _generator_ to be used by JSON.
46
+ def generator=(generator) # :nodoc:
47
+ @generator = generator
48
+ generator_methods = generator::GeneratorMethods
49
+ for const in generator_methods.constants
50
+ klass = deep_const_get(const)
51
+ modul = generator_methods.const_get(const)
52
+ klass.class_eval do
53
+ instance_methods(false).each do |m|
54
+ m.to_s == 'to_json' and remove_method m
55
+ end
56
+ include modul
57
+ end
58
+ end
59
+ self.state = generator::State
60
+ const_set :State, self.state
61
+ end
62
+
63
+ # Returns the JSON generator modul, that is used by JSON. This might be
64
+ # either JSON::Ext::Generator or JSON::Pure::Generator.
65
+ attr_reader :generator
66
+
67
+ # Returns the JSON generator state class, that is used by JSON. This might
68
+ # be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
69
+ attr_accessor :state
70
+
71
+ # This is create identifier, that is used to decide, if the _json_create_
72
+ # hook of a class should be called. It defaults to 'json_class'.
73
+ attr_accessor :create_id
74
+ end
75
+ self.create_id = 'json_class'
76
+
77
+ NaN = (-1.0) ** 0.5
78
+
79
+ Infinity = 1.0/0
80
+
81
+ MinusInfinity = -Infinity
82
+
83
+ # The base exception for JSON errors.
84
+ class JSONError < StandardError; end
85
+
86
+ # This exception is raised, if a parser error occurs.
87
+ class ParserError < JSONError; end
88
+
89
+ # This exception is raised, if the nesting of parsed datastructures is too
90
+ # deep.
91
+ class NestingError < ParserError; end
92
+
93
+ # This exception is raised, if a generator or unparser error occurs.
94
+ class GeneratorError < JSONError; end
95
+ # For backwards compatibility
96
+ UnparserError = GeneratorError
97
+
98
+ # If a circular data structure is encountered while unparsing
99
+ # this exception is raised.
100
+ class CircularDatastructure < GeneratorError; end
101
+
102
+ # This exception is raised, if the required unicode support is missing on the
103
+ # system. Usually this means, that the iconv library is not installed.
104
+ class MissingUnicodeSupport < JSONError; end
105
+
106
+ module_function
107
+
108
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
109
+ #
110
+ # _opts_ can have the following
111
+ # keys:
112
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
113
+ # structures. Disable depth checking with :max_nesting => false, it defaults
114
+ # to 19.
115
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
116
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
117
+ # to false.
118
+ # * *create_additions*: If set to false, the Parser doesn't create
119
+ # additions even if a matchin class and create_id was found. This option
120
+ # defaults to true.
121
+ def parse(source, opts = {})
122
+ JSON.parser.new(source, opts).parse
123
+ end
124
+
125
+ # Parse the JSON string _source_ into a Ruby data structure and return it.
126
+ # The bang version of the parse method, defaults to the more dangerous values
127
+ # for the _opts_ hash, so be sure only to parse trusted _source_ strings.
128
+ #
129
+ # _opts_ can have the following keys:
130
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
131
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
132
+ # methods defaults to not doing max depth checking: This can be dangerous,
133
+ # if someone wants to fill up your stack.
134
+ # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
135
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
136
+ # to true.
137
+ # * *create_additions*: If set to false, the Parser doesn't create
138
+ # additions even if a matchin class and create_id was found. This option
139
+ # defaults to true.
140
+ def parse!(source, opts = {})
141
+ opts = {
142
+ :max_nesting => false,
143
+ :allow_nan => true
144
+ }.update(opts)
145
+ JSON.parser.new(source, opts).parse
146
+ end
147
+
148
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
149
+ # return it. _state_ is
150
+ # * a JSON::State object,
151
+ # * or a Hash like object (responding to to_hash),
152
+ # * an object convertible into a hash by a to_h method,
153
+ # that is used as or to configure a State object.
154
+ #
155
+ # It defaults to a state object, that creates the shortest possible JSON text
156
+ # in one line, checks for circular data structures and doesn't allow NaN,
157
+ # Infinity, and -Infinity.
158
+ #
159
+ # A _state_ hash can have the following keys:
160
+ # * *indent*: a string used to indent levels (default: ''),
161
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
162
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
163
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
164
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
165
+ # * *check_circular*: true if checking for circular data structures
166
+ # should be done (the default), false otherwise.
167
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
168
+ # generated, otherwise an exception is thrown, if these values are
169
+ # encountered. This options defaults to false.
170
+ # * *max_nesting*: The maximum depth of nesting allowed in the data
171
+ # structures from which JSON is to be generated. Disable depth checking
172
+ # with :max_nesting => false, it defaults to 19.
173
+ #
174
+ # See also the fast_generate for the fastest creation method with the least
175
+ # amount of sanity checks, and the pretty_generate method for some
176
+ # defaults for a pretty output.
177
+ def generate(obj, state = nil)
178
+ if state
179
+ state = State.from_state(state)
180
+ else
181
+ state = State.new
182
+ end
183
+ obj.to_json(state)
184
+ end
185
+
186
+ # :stopdoc:
187
+ # I want to deprecate these later, so I'll first be silent about them, and
188
+ # later delete them.
189
+ alias unparse generate
190
+ module_function :unparse
191
+ # :startdoc:
192
+
193
+ # Unparse the Ruby data structure _obj_ into a single line JSON string and
194
+ # return it. This method disables the checks for circles in Ruby objects, and
195
+ # also generates NaN, Infinity, and, -Infinity float values.
196
+ #
197
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
198
+ # _obj_ argument, because this will cause JSON to go into an infinite loop.
199
+ def fast_generate(obj)
200
+ obj.to_json(nil)
201
+ end
202
+
203
+ # :stopdoc:
204
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
205
+ alias fast_unparse fast_generate
206
+ module_function :fast_unparse
207
+ # :startdoc:
208
+
209
+ # Unparse the Ruby data structure _obj_ into a JSON string and return it. The
210
+ # returned string is a prettier form of the string returned by #unparse.
211
+ #
212
+ # The _opts_ argument can be used to configure the generator, see the
213
+ # generate method for a more detailed explanation.
214
+ def pretty_generate(obj, opts = nil)
215
+ state = JSON.state.new(
216
+ :indent => ' ',
217
+ :space => ' ',
218
+ :object_nl => "\n",
219
+ :array_nl => "\n",
220
+ :check_circular => true
221
+ )
222
+ if opts
223
+ if opts.respond_to? :to_hash
224
+ opts = opts.to_hash
225
+ elsif opts.respond_to? :to_h
226
+ opts = opts.to_h
227
+ else
228
+ raise TypeError, "can't convert #{opts.class} into Hash"
229
+ end
230
+ state.configure(opts)
231
+ end
232
+ obj.to_json(state)
233
+ end
234
+
235
+ # :stopdoc:
236
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
237
+ alias pretty_unparse pretty_generate
238
+ module_function :pretty_unparse
239
+ # :startdoc:
240
+
241
+ # Load a ruby data structure from a JSON _source_ and return it. A source can
242
+ # either be a string-like object, an IO like object, or an object responding
243
+ # to the read method. If _proc_ was given, it will be called with any nested
244
+ # Ruby object as an argument recursively in depth first order.
245
+ #
246
+ # This method is part of the implementation of the load/dump interface of
247
+ # Marshal and YAML.
248
+ def load(source, proc = nil)
249
+ if source.respond_to? :to_str
250
+ source = source.to_str
251
+ elsif source.respond_to? :to_io
252
+ source = source.to_io.read
253
+ else
254
+ source = source.read
255
+ end
256
+ result = parse(source, :max_nesting => false, :allow_nan => true)
257
+ recurse_proc(result, &proc) if proc
258
+ result
259
+ end
260
+
261
+ def recurse_proc(result, &proc)
262
+ case result
263
+ when Array
264
+ result.each { |x| recurse_proc x, &proc }
265
+ proc.call result
266
+ when Hash
267
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
268
+ proc.call result
269
+ else
270
+ proc.call result
271
+ end
272
+ end
273
+ private :recurse_proc
274
+ module_function :recurse_proc
275
+
276
+ alias restore load
277
+ module_function :restore
278
+
279
+ # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
280
+ # the result.
281
+ #
282
+ # If anIO (an IO like object or an object that responds to the write method)
283
+ # was given, the resulting JSON is written to it.
284
+ #
285
+ # If the number of nested arrays or objects exceeds _limit_ an ArgumentError
286
+ # exception is raised. This argument is similar (but not exactly the
287
+ # same!) to the _limit_ argument in Marshal.dump.
288
+ #
289
+ # This method is part of the implementation of the load/dump interface of
290
+ # Marshal and YAML.
291
+ def dump(obj, anIO = nil, limit = nil)
292
+ if anIO and limit.nil?
293
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
294
+ unless anIO.respond_to?(:write)
295
+ limit = anIO
296
+ anIO = nil
297
+ end
298
+ end
299
+ limit ||= 0
300
+ result = generate(obj, :allow_nan => true, :max_nesting => limit)
301
+ if anIO
302
+ anIO.write result
303
+ anIO
304
+ else
305
+ result
306
+ end
307
+ rescue JSON::NestingError
308
+ raise ArgumentError, "exceed depth limit"
309
+ end
310
+ end
311
+
312
+ module ::Kernel
313
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
314
+ # one line.
315
+ def j(*objs)
316
+ objs.each do |obj|
317
+ puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
318
+ end
319
+ nil
320
+ end
321
+
322
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
323
+ # indentation and over many lines.
324
+ def jj(*objs)
325
+ objs.each do |obj|
326
+ puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
327
+ end
328
+ nil
329
+ end
330
+
331
+ # If _object_ is string-like parse the string and return the parsed result as
332
+ # a Ruby data structure. Otherwise generate a JSON text from the Ruby data
333
+ # structure object and return it.
334
+ #
335
+ # The _opts_ argument is passed through to generate/parse respectively, see
336
+ # generate and parse for their documentation.
337
+ def JSON(object, opts = {})
338
+ if object.respond_to? :to_str
339
+ JSON.parse(object.to_str, opts)
340
+ else
341
+ JSON.generate(object, opts)
342
+ end
343
+ end
344
+ end
345
+
346
+ class ::Class
347
+ # Returns true, if this class can be used to create an instance
348
+ # from a serialised JSON string. The class has to implement a class
349
+ # method _json_create_ that expects a hash as first parameter, which includes
350
+ # the required data.
351
+ def json_creatable?
352
+ respond_to?(:json_create)
353
+ end
354
+ end