json_serializable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in json_serializable.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "json_serializable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "json_serializable"
7
+ s.version = JsonSerializable::VERSION
8
+ s.authors = ["smsohan"]
9
+ s.email = ["sohan39@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Provides a clean approach to custom JSON serialize your AR models}
12
+ s.description = %q{Use custom names in JSON keys and your custom logic in JSON values}
13
+
14
+ s.rubyforge_project = "json_serializable"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,45 @@
1
+ # json_serializable [:name, :description],
2
+ # :price => lambda{|content| content.price.to_i},
3
+ # :secret_key => :primary_key
4
+
5
+ require "json_serializable/version"
6
+
7
+ module JsonSerializable
8
+
9
+ def self.included(base)
10
+ base.send :extend, ClassMethods
11
+ base.send :include, InstanceMethods
12
+ end
13
+
14
+ module InstanceMethods
15
+
16
+ def as_json(options = {})
17
+ custom_options = self.class.json_field_names.present? ? options.merge(only: self.class.json_field_names) : options
18
+ custom_hash = super(custom_options)
19
+
20
+ self.class.json_mappings.each_pair do |key, value|
21
+ custom_hash[key] = value.is_a?(Symbol) ? self.send(value) : value[self]
22
+ end
23
+
24
+ custom_hash
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+
30
+ def json_serializable field_names, mappings = {}
31
+ @@json_field_names = field_names
32
+ @@json_mappings = mappings
33
+ end
34
+
35
+ def json_field_names
36
+ @@json_field_names ||= []
37
+ end
38
+
39
+ def json_mappings
40
+ @@json_mappings ||= []
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,3 @@
1
+ module JsonSerializable
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_serializable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - smsohan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2152654780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152654780
25
+ description: Use custom names in JSON keys and your custom logic in JSON values
26
+ email:
27
+ - sohan39@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - json_serializable.gemspec
36
+ - lib/json_serializable.rb
37
+ - lib/json_serializable/version.rb
38
+ homepage: ''
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project: json_serializable
58
+ rubygems_version: 1.8.11
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Provides a clean approach to custom JSON serialize your AR models
62
+ test_files: []