json_model 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee86600ff328e9d32d69b0100a2923f982738cd6
4
+ data.tar.gz: 71d6adbbe658e4c10b75f933fc1531dbfe251ec3
5
+ SHA512:
6
+ metadata.gz: 106ecccd9d424e3d1109910434acb4686cc91b0e2d31ad93d754c2ceac4d21f84847012479caadd619ec9ba4ac29c79cf13287dba535d9026acd2ba26b22014e
7
+ data.tar.gz: 3a64ccbf052ea85fe24a1b544817ae833a71b35d198536910f99e26b0821d329423c1820292bf5a94ba395c117e5544b2bda0d5aa19ce19e47f778064c684c96
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .DS_Store
2
+ pkg/
3
+ doc
4
+ .idea/
5
+ log
6
+ test/config.yml
7
+ *.sublime*
8
+ mymodel.json
9
+ test.json
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_model.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ json_model (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ansi (1.5.0)
10
+ builder (3.2.2)
11
+ minitest (5.5.1)
12
+ minitest-reporters (1.0.10)
13
+ ansi
14
+ builder
15
+ minitest (>= 5.0)
16
+ ruby-progressbar
17
+ rake (10.4.2)
18
+ ruby-progressbar (1.7.1)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 1.8)
25
+ json_model!
26
+ minitest (~> 5.5.0)
27
+ minitest-reporters (~> 1.0.10)
28
+ rake (~> 10.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 René Kersten
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # JsonModel
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/json_model`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'json_model'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install json_model
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/json_model/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_model/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_model"
8
+ spec.version = JsonModel::VERSION
9
+ spec.authors = ["René Kersten"]
10
+ spec.email = ["rene.kersten@gmail.com"]
11
+
12
+ spec.summary = %q{persist your model data to a .json file}
13
+ spec.homepage = "https://github.com/endorfin/json_model"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.8"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "minitest", "~> 5.5.0"
22
+ spec.add_development_dependency "minitest-reporters", "~> 1.0.10"
23
+ end
@@ -0,0 +1,3 @@
1
+ module JsonModel
2
+ VERSION = "0.1.0"
3
+ end
data/lib/json_model.rb ADDED
@@ -0,0 +1,149 @@
1
+ require 'json_model/version'
2
+ require 'json'
3
+ require 'json/add/core'
4
+
5
+ module JsonModel
6
+
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+
10
+ base.send(:field, :id)
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ def attr_accessor(*vars)
16
+ @attributes ||= []
17
+ @attributes.concat vars
18
+ super(*vars)
19
+ end
20
+
21
+ def attributes
22
+ @attributes
23
+ end
24
+
25
+ def field(name)
26
+ attr_accessor name
27
+ end
28
+
29
+ def all
30
+ entries = []
31
+ for entry in load_entries
32
+ entries << create_object(entry)
33
+ end
34
+
35
+ entries
36
+ end
37
+
38
+ def find(value, key='id')
39
+ entry = all.find{|e| e.send(key) == value }
40
+ end
41
+
42
+ def find_by(hash)
43
+ if hash.instance_of? Hash
44
+ hash.each {| key, value | return find(value, key) }
45
+ end
46
+ end
47
+
48
+ def new_id
49
+ ids = all.map(&:id)
50
+ ids.max.to_i + 1
51
+ end
52
+
53
+ def filename=(str)
54
+ @filename = str
55
+ end
56
+
57
+ def filename
58
+ @filename ||= "#{ancestors.first}.json".downcase
59
+ File.expand_path(@filename)
60
+ end
61
+
62
+
63
+ protected
64
+
65
+
66
+ def save(entry)
67
+ entries = all
68
+
69
+ if entry.new_record?
70
+ # new record
71
+ entry.id = ancestors.first.send(:new_id)
72
+ entries.push(entry)
73
+ else
74
+ # update record
75
+ index = entries.index {|e| e.id == entry.id }
76
+ entries[index] = entry
77
+ end
78
+
79
+ save_entries(entries)
80
+ entry
81
+ end
82
+
83
+ def destroy(entry)
84
+ entries = all
85
+
86
+ if entry.respond_to?(:id)
87
+ index = entries.index {|e| e.id == entry.id }
88
+ entries.delete_at(index)
89
+ save_entries(entries)
90
+ return true
91
+ end
92
+
93
+ false
94
+ end
95
+
96
+ def load_entries
97
+ entries = []
98
+ if File.exists?(filename)
99
+ file = File.read(filename)
100
+ file = '[]' if file.length.zero?
101
+ entries = JSON.parse(file)
102
+ end
103
+
104
+ entries
105
+ end
106
+
107
+ def save_entries(entries)
108
+ File.open(filename,"w") do |f|
109
+ f.write(entries.to_json)
110
+ end
111
+ end
112
+
113
+ def create_object(entry)
114
+ self.send(:new, entry)
115
+ end
116
+ end
117
+
118
+ def initialize(atts={})
119
+ atts.each do |k,v|
120
+ instance_variable_set("@#{k}", v)
121
+ end
122
+ end
123
+
124
+ def attributes
125
+ self.class.attributes
126
+ end
127
+
128
+ def save
129
+ self.class.send(:save, self)
130
+ end
131
+
132
+ def destroy
133
+ self.class.send(:destroy, self)
134
+ end
135
+
136
+ def new_record?
137
+ self.id.nil?
138
+ end
139
+
140
+ def to_json(*a)
141
+ hash = {}
142
+ self.instance_variables.each do |var|
143
+ key = var.to_s.delete '@'
144
+ hash[key] = self.instance_variable_get var
145
+ end
146
+ hash.to_json(*a)
147
+ end
148
+
149
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - René Kersten
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 5.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 5.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.10
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.10
69
+ description:
70
+ email:
71
+ - rene.kersten@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - json_model.gemspec
85
+ - lib/json_model.rb
86
+ - lib/json_model/version.rb
87
+ homepage: https://github.com/endorfin/json_model
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.14
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: persist your model data to a .json file
111
+ test_files: []