json_serializable 0.1.0 → 0.2.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.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.3-p0@Playground
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_serializable (0.1.0)
4
+ json_serializable (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -0,0 +1,43 @@
1
+ #JsonSerializable
2
+ In my projects I have found the basic as_json method that you get from Rails to be quite limiting. For example, as_json takes :methods, but the hash key can only be the method's name. Similar restriction applies to the :only option. So, I am extending this method in JsonSerializable so that you can customize your JSON output with a simple declarative syntax.
3
+
4
+
5
+ ##In you Gemfile:
6
+
7
+ gem :json_serializable
8
+
9
+
10
+ ##Or install directly:
11
+
12
+ gem install json_serializable
13
+
14
+
15
+ ##In your model:
16
+
17
+ class Content
18
+ include JsonSerializable
19
+
20
+ json_serializable [:name, :description], private: lambda {|content| content.secret_data.upcase}, :street => :address
21
+
22
+ def street
23
+ "#{street_number} #{street_name}"
24
+ end
25
+
26
+ end
27
+
28
+ ##If your model has the following data:
29
+
30
+ calgaryNews = Content.new(name: 'Sunny', description: 'Awesome sunny and bright day', secret_data: 'ggh98',
31
+ street_number: 2301, street_number: 'Varsity DR NW')
32
+
33
+
34
+ ##JsonSerializable will produce the following Hash for your json:
35
+
36
+ calgaryNews.as_json
37
+ #==>
38
+ {
39
+ name: 'Sunny', description: 'Awesome sunny and bright day', private: 'GGH98',
40
+ address: "2301 Varsity DR NW'
41
+ }
42
+
43
+ Released under DWTFYW (Do What the F* You Want) License.
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = JsonSerializable::VERSION
8
8
  s.authors = ["smsohan"]
9
9
  s.email = ["sohan39@gmail.com"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/smsohan/json_serializer"
11
11
  s.summary = %q{Provides a clean approach to custom JSON serialize your AR models}
12
12
  s.description = %q{Use custom names in JSON keys and your custom logic in JSON values}
13
13
 
@@ -21,25 +21,34 @@ module JsonSerializable
21
21
  module InstanceMethods
22
22
 
23
23
  def as_json(options = {})
24
+ super(custom_options(self.class.json_field_names, options)).merge hash_from_mappings(self.class.json_mappings)
25
+ end
26
+
27
+ private
28
+ def custom_options(fields, options)
24
29
  options ||= {}
25
- custom_options = self.class.json_field_names.size > 0 ? options.merge(only: self.class.json_field_names) : options
26
- custom_hash = super(custom_options)
30
+ if fields.size > 0
31
+ options[:only] ||= []
32
+ options[:only] |= fields
33
+ end
34
+ options
35
+ end
27
36
 
28
- self.class.json_mappings.each_pair do |key, value|
37
+ def hash_from_mappings(mappings)
38
+ custom_hash = {}
39
+ mappings.each_pair do |key, value|
29
40
  if value.is_a?(Symbol)
30
41
  custom_hash[value] = self.send(key)
31
42
  else
32
43
  custom_hash[key] = value[self]
33
44
  end
34
45
  end
35
-
36
46
  custom_hash
37
47
  end
38
48
  end
39
49
 
40
50
  module ClassMethods
41
51
 
42
-
43
52
  def json_serializable field_names, mappings = {}
44
53
  @@json_field_names = field_names
45
54
  @@json_mappings = mappings
@@ -1,3 +1,3 @@
1
1
  module JsonSerializable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_serializable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2156365720 !ruby/object:Gem::Requirement
16
+ requirement: &2168965380 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2156365720
24
+ version_requirements: *2168965380
25
25
  description: Use custom names in JSON keys and your custom logic in JSON values
26
26
  email:
27
27
  - sohan39@gmail.com
@@ -31,14 +31,16 @@ extra_rdoc_files: []
31
31
  files:
32
32
  - .gitignore
33
33
  - .rspec
34
+ - .rvmrc
34
35
  - Gemfile
35
36
  - Gemfile.lock
37
+ - README.md
36
38
  - Rakefile
37
39
  - json_serializable.gemspec
38
40
  - lib/json_serializable.rb
39
41
  - lib/json_serializable/version.rb
40
42
  - spec/json_serializable_spec.rb
41
- homepage: ''
43
+ homepage: https://github.com/smsohan/json_serializer
42
44
  licenses: []
43
45
  post_install_message:
44
46
  rdoc_options: []
@@ -52,7 +54,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
54
  version: '0'
53
55
  segments:
54
56
  - 0
55
- hash: 329497193356031779
57
+ hash: 2466998277856228953
56
58
  required_rubygems_version: !ruby/object:Gem::Requirement
57
59
  none: false
58
60
  requirements:
@@ -61,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
63
  version: '0'
62
64
  segments:
63
65
  - 0
64
- hash: 329497193356031779
66
+ hash: 2466998277856228953
65
67
  requirements: []
66
68
  rubyforge_project: json_serializable
67
69
  rubygems_version: 1.8.17