active_json 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 835afe571cf0afab18ab3016a0d7c2bd917247de
4
+ data.tar.gz: b8dacc03d9c3782340e26f1ec1560b73f118a221
5
+ SHA512:
6
+ metadata.gz: 94f07fb8d8cf34fff9195668f0a34a058b6d74c7edd4fa3fce16f20810d7fb36a6f5ccd5c10cbf1ca70bc320918a193f60d8ed53bc7fc02dcf195933f925066b
7
+ data.tar.gz: 543856071a14536c24131ead2c3ffbdd64f4f94251f3ec52d01dda07f1e2861da7ebe1c9f62d403296ffaf39d0576c3318bedc2af24e65a4b26ad3bb873c320c
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Sergey Novikov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,142 @@
1
+ # ActiveJson
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/active_json.svg)](https://badge.fury.io/rb/active_json)
4
+ [![Code Climate](https://codeclimate.com/github/droptheplot/active_json/badges/gpa.svg)](https://codeclimate.com/github/droptheplot/active_json)
5
+
6
+ Easy JSON storage.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'active_json'
14
+ ```
15
+
16
+ And then execute:
17
+ ```bash
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+ ```bash
23
+ $ gem install active_json
24
+ ```
25
+
26
+ ## Getting Started
27
+
28
+ * Create your `my_database.json` somewhere.
29
+ * Define database module with `ActiveJson::Base`.
30
+
31
+ ```ruby
32
+ module MyDatabase
33
+ extend ActiveJson::Base
34
+
35
+ configure do |config|
36
+ config.path = 'path/to/my_database.json'
37
+ end
38
+ end
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ ### JSON
44
+
45
+ You can access your json as any ruby `Hash` or `Array`. For example, if your database looks like this:
46
+
47
+ ```json
48
+ {
49
+ "website": "Mr. Buster Muller's Blog.",
50
+ "posts": [
51
+ {
52
+ "id": 1,
53
+ "title": "Sustainable twee kinfolk cronut williamsburg franzen.",
54
+ "published": true
55
+ },
56
+ {
57
+ "id": 2,
58
+ "title": "Marfa skateboard synth swag.",
59
+ "published": false
60
+ }
61
+ ]
62
+ }
63
+ ```
64
+
65
+ Then you can do something like this:
66
+
67
+ ```ruby
68
+ # Get website title
69
+ MyDatabase.website
70
+
71
+ # Iterate through posts
72
+ MyDatabase.posts each do |post|
73
+ puts post.title
74
+ end
75
+ ```
76
+
77
+ ### Update
78
+
79
+ Update you database as you want:
80
+
81
+ ```ruby
82
+ MyDatabase.website
83
+ # => "Mr. Buster Muller's Blog."
84
+
85
+ MyDatabase.website = "Someone else's Blog."
86
+
87
+ MyDatabase.website
88
+ # => "Someone else's Blog."
89
+ ```
90
+
91
+ ### Reload
92
+
93
+ If you changed something and don't want it anymore:
94
+
95
+ ```ruby
96
+ MyDatabase.website
97
+ # => "Mr. Buster Muller's Blog."
98
+
99
+ MyDatabase.website = "Someone else's Blog."
100
+
101
+ MyDatabase.website
102
+ # => "Someone else's Blog."
103
+
104
+ MyDatabase.reload
105
+
106
+ MyDatabase.website
107
+ # => "Mr. Buster Muller's Blog."
108
+ ```
109
+
110
+ ### Save
111
+
112
+ Or save you changes into `my_database.json` file:
113
+
114
+ ```ruby
115
+ MyDatabase.website
116
+ # => "Mr. Buster Muller's Blog."
117
+
118
+ MyDatabase.website = "Someone else's Blog."
119
+
120
+ MyDatabase.website
121
+ # => "Someone else's Blog."
122
+
123
+ MyDatabase.save
124
+ # => true
125
+
126
+ MyDatabase.reload
127
+
128
+ MyDatabase.website
129
+ # => "Someone else's Blog."
130
+ ```
131
+
132
+ ## Contributing
133
+
134
+ 1. Fork it (https://github.com/droptheplot/active_json/fork)
135
+ 2. Create your feature branch (git checkout -b my-new-feature)
136
+ 3. Commit your changes (git commit -am 'Add some feature')
137
+ 4. Push to the branch (git push origin my-new-feature)
138
+ 5. Create new Pull Request
139
+
140
+ ## License
141
+
142
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,19 @@
1
+ require 'benchmark'
2
+ require 'awesome_print'
3
+
4
+ require 'json'
5
+ require 'ostruct'
6
+
7
+ require_relative 'active_json/array'
8
+ require_relative 'active_json/hash'
9
+ require_relative 'active_json/base'
10
+
11
+ module ActiveJson
12
+ STRUCTURES = [ActiveJson::Array, ActiveJson::Hash].freeze
13
+
14
+ class ReadOnlyDatabase < StandardError
15
+ def initialize(*)
16
+ super("Can't modify readonly database.")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveJson
2
+ class Array < Array
3
+ def to_h
4
+ map do |value|
5
+ STRUCTURES.any? { |s| value.is_a?(s) } ? value.to_h : value
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,52 @@
1
+ module ActiveJson
2
+ module Base
3
+ def configure
4
+ @config ||= OpenStruct.new(path: nil, readonly: false)
5
+ yield(@config)
6
+ end
7
+
8
+ def method_missing(method_name, *args, &block)
9
+ database.public_send(method_name, *args, &block)
10
+ end
11
+
12
+ def respond_to_missing?(method_name, *)
13
+ database.respond_to?(method_name)
14
+ end
15
+
16
+ def [](index)
17
+ database.dig(index)
18
+ end
19
+
20
+ def reload
21
+ @database = nil
22
+
23
+ database
24
+ end
25
+
26
+ def save
27
+ raise ActiveJson::ReadOnlyDatabase if @config[:readonly]
28
+
29
+ result = JSON.pretty_generate(database.to_h)
30
+
31
+ File.open(@config[:path], 'w') do |file|
32
+ file.write(result)
33
+ end
34
+
35
+ true
36
+ end
37
+
38
+ def inspect
39
+ database.to_h
40
+ end
41
+
42
+ private
43
+
44
+ def database
45
+ @database ||= JSON.parse(
46
+ File.read(@config[:path]),
47
+ object_class: ActiveJson::Hash,
48
+ array_class: ActiveJson::Array
49
+ )
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,12 @@
1
+ module ActiveJson
2
+ class Hash < OpenStruct
3
+ def to_h
4
+ super.map do |key, value|
5
+ [
6
+ key,
7
+ STRUCTURES.any? { |s| value.is_a?(s) } ? value.to_h : value
8
+ ]
9
+ end.to_h
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveJson
2
+ VERSION = '0.0.2'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Novikov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: awesome_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - novikov359@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - lib/active_json.rb
65
+ - lib/active_json/array.rb
66
+ - lib/active_json/base.rb
67
+ - lib/active_json/hash.rb
68
+ - lib/active_json/version.rb
69
+ homepage: https://github.com/droptheplot/active_json
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.6.4
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Easy JSON storage.
93
+ test_files: []
94
+ has_rdoc: