nifty-model-json 1.0.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.
@@ -0,0 +1,52 @@
|
|
1
|
+
module Nifty
|
2
|
+
module ModelJSON
|
3
|
+
module ActiveRecordExtension
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_nifty_json_hash(options = {})
|
10
|
+
self.class.nifty_json_methods.select { |m| m[:options][:if].nil? || m[:options][:if].call(self, options) }.inject({}) do |full_hash, attr|
|
11
|
+
|
12
|
+
if attr[:options][:group]
|
13
|
+
hash = full_hash[attr[:options][:group]] ||= {}
|
14
|
+
else
|
15
|
+
hash = full_hash
|
16
|
+
end
|
17
|
+
|
18
|
+
value = self.send(attr[:name])
|
19
|
+
case value
|
20
|
+
when ActiveRecord::Base
|
21
|
+
value = value.to_nifty_json_hash
|
22
|
+
when ActiveRecord::Associations::CollectionProxy
|
23
|
+
value = value.map(&:to_nifty_json_hash)
|
24
|
+
end
|
25
|
+
|
26
|
+
hash[attr[:options][:as] || attr[:name]] = value
|
27
|
+
full_hash
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_nifty_json(options = {})
|
32
|
+
to_json_hash(options).to_json
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
|
37
|
+
def nifty_json_methods
|
38
|
+
@nifty_json_methods ||= [{:name => :id, :options => {}}]
|
39
|
+
end
|
40
|
+
|
41
|
+
def json(*attrs)
|
42
|
+
options = attrs.select { |a| a.is_a?(Hash) }.first || {}
|
43
|
+
attrs.select { |a| a.is_a?(Symbol) }.each do |attr|
|
44
|
+
self.nifty_json_methods << {:name => attr, :options => options}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Nifty
|
2
|
+
module ModelJSON
|
3
|
+
class Railtie < Rails::Railtie
|
4
|
+
|
5
|
+
initializer 'nifty.model_json.initialize' do
|
6
|
+
ActiveSupport.on_load(:active_record) do
|
7
|
+
require 'nifty/model_json/active_record_extension'
|
8
|
+
ActiveRecord::Base.send :include, Nifty::ModelJSON::ActiveRecordExtension
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nifty-model-json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Cooke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: A Rails extension for creating JSON hashes for Active Record classes
|
31
|
+
email:
|
32
|
+
- adam@niftyware.io
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/nifty/model_json/active_record_extension.rb
|
38
|
+
- lib/nifty/model_json/railtie.rb
|
39
|
+
- lib/nifty/model_json/version.rb
|
40
|
+
- lib/nifty/model_json.rb
|
41
|
+
homepage: https://github.com/niftyware/model-json
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.23
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: A Rails extension for creating JSON hashes for Active Record classes
|
65
|
+
test_files: []
|
66
|
+
has_rdoc:
|