jsonite 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 49c0a11e4a44bd3ab0ad87f0cbf27debc56afd10
4
+ data.tar.gz: beeb10b2cc59a4e812c492cfba50bce34dfe5b4c
5
+ SHA512:
6
+ metadata.gz: bee8032685d34443435ef821aaa94b8fe0b4170ecc6e0a1fb55b6ed1bb1ae95346f8b50dce5f22c57d3673f1f7027b881291c5e9199f08bd12fbcdd5797a4321
7
+ data.tar.gz: b3d8f45e00dd6164f2655988bbe6a5e2bc8880cde5ec1d9773087edc8518f047c07b2f8982e184d600c400d30ec847e0a2c05f29f3cb0fb54acfdf1fde65fe7b
@@ -0,0 +1,18 @@
1
+ require 'active_support/inflector'
2
+
3
+ class Jsonite
4
+
5
+ module Helper
6
+ module_function
7
+
8
+ def resource_name resource
9
+ if resource.respond_to? :model_name
10
+ resource.model_name.collection
11
+ elsif resource.class.respond_to? :model_name
12
+ resource.class.model_name.singular
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,5 @@
1
+ class Jsonite
2
+
3
+ VERSION = '0.0.1'
4
+
5
+ end
data/lib/jsonite.rb ADDED
@@ -0,0 +1,99 @@
1
+ require 'jsonite/helper'
2
+
3
+ # A simple JSON presenter for hypermedia applications.
4
+ # Loosely adheers to HAL: http://stateless.co/hal_specification.html
5
+
6
+ class Jsonite
7
+
8
+ class << self
9
+
10
+ def present resource, options = {}
11
+ presenter = options.fetch :with, self
12
+
13
+ case resource
14
+ when Jsonite
15
+ resource.as_json options
16
+ else
17
+ resource = if resource.respond_to?(:to_ary)
18
+ resource.to_ary.map do |r|
19
+ present presenter.new(r), options.merge(root: nil)
20
+ end
21
+ elsif resource.respond_to?(:as_json)
22
+ present presenter.new(resource), options.merge(root: nil)
23
+ else
24
+ resource
25
+ end
26
+
27
+ root = options.fetch :root, Helper.resource_name(resource)
28
+ root ? { root => resource } : resource
29
+ end
30
+ end
31
+
32
+ def property name, options = {}, &handler
33
+ properties[name] = handler || proc { send name }
34
+ end
35
+
36
+ def properties *properties
37
+ @properties ||= {}
38
+ properties.map(&method(:property)) if properties.present?
39
+ @properties
40
+ end
41
+
42
+ def embedded name, options = {}, &handler
43
+ unless options[:with].is_a?(Class) && options[:with] <= Jsonite
44
+ raise KeyError, ':with option must be a Jsonite'
45
+ end
46
+
47
+ property name, options do |context|
48
+ resource = handler ? instance_exec(context, &handler) : send(name)
49
+ present resource, { context: context, root: nil }.merge(options)
50
+ end
51
+ end
52
+
53
+ def link rel = :self, options = {}, &handler
54
+ links[rel] = options.merge handler: handler
55
+ end
56
+
57
+ def links
58
+ @links ||= {}
59
+ end
60
+
61
+ def inherited presenter
62
+ presenter.properties.update properties
63
+ presenter.links.update links
64
+ end
65
+
66
+ end
67
+
68
+ attr_reader :resource, :defaults
69
+
70
+ def initialize resource, defaults = {}
71
+ @resource, @defaults = resource, defaults
72
+ end
73
+
74
+ def as_json options = {}
75
+ return resource.as_json options if instance_of? Jsonite
76
+ context, options = options.delete(:context), defaults.merge(options)
77
+ properties(context).merge(_links: links(context)).as_json options
78
+ end
79
+
80
+ def properties context = nil
81
+ context ||= resource
82
+ self.class.properties.each_with_object({}) do |(name, handler), props|
83
+ catch :ignore do
84
+ props[name] = resource.instance_exec context, &handler
85
+ end
86
+ end
87
+ end
88
+
89
+ def links context = nil
90
+ context ||= resource
91
+ self.class.links.each_with_object({}) do |(name, link), props|
92
+ catch :ignore do
93
+ href = resource.instance_exec context, &link[:handler]
94
+ props[name] = { href: href }.merge link.except :handler
95
+ end
96
+ end
97
+ end
98
+
99
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Celis
8
+ - Evan Owen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: 3.1.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 3.1.0
28
+ description: Tiny JSON presenter
29
+ email:
30
+ - stephen@stephencelis.com
31
+ - kainosnoema@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/jsonite/helper.rb
37
+ - lib/jsonite/version.rb
38
+ - lib/jsonite.rb
39
+ homepage: https://github.com/barrelage/jsonite
40
+ licenses: []
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '>='
49
+ - !ruby/object:Gem::Version
50
+ version: 1.9.3
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.0.2
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Tiny JSON presenter
62
+ test_files: []