jasonette-rails 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -1
  3. data/Rakefile +0 -1
  4. data/lib/jasonette-rails.rb +1 -0
  5. data/lib/jasonette.rb +26 -11
  6. data/lib/jasonette/action_view_extensions.rb +15 -0
  7. data/lib/jasonette/core/action.rb +34 -0
  8. data/lib/jasonette/core/base.rb +177 -60
  9. data/lib/jasonette/core/body.rb +14 -0
  10. data/lib/jasonette/core/body/footer.rb +7 -0
  11. data/lib/jasonette/core/body/footer/input.rb +24 -0
  12. data/lib/jasonette/core/body/footer/tabs.rb +11 -0
  13. data/lib/jasonette/core/body/header.rb +21 -0
  14. data/lib/jasonette/core/body/header/search.rb +6 -0
  15. data/lib/jasonette/core/body/layers.rb +4 -0
  16. data/lib/jasonette/core/body/sections.rb +5 -0
  17. data/lib/jasonette/core/components.rb +0 -9
  18. data/lib/jasonette/core/data.rb +4 -0
  19. data/lib/jasonette/core/item.rb +21 -0
  20. data/lib/jasonette/core/items.rb +99 -14
  21. data/lib/jasonette/core/layout.rb +2 -5
  22. data/lib/jasonette/core/map.rb +6 -0
  23. data/lib/jasonette/core/options.rb +9 -0
  24. data/lib/jasonette/{jason/body/header.rb → core/pins.rb} +1 -1
  25. data/lib/jasonette/core/properties.rb +151 -22
  26. data/lib/jasonette/engine.rb +10 -0
  27. data/lib/jasonette/handler.rb +14 -0
  28. data/lib/jasonette/helpers.rb +38 -0
  29. data/lib/jasonette/jason/body.rb +2 -9
  30. data/lib/jasonette/jason/head.rb +22 -2
  31. data/lib/jasonette/jason/head/actions.rb +5 -0
  32. data/lib/jasonette/jason/head/templates.rb +5 -0
  33. data/lib/jasonette/railtie.rb +30 -0
  34. data/lib/jasonette/template.rb +201 -0
  35. data/lib/jasonette/version.rb +1 -1
  36. metadata +55 -23
  37. data/lib/jasonette/core/sections.rb +0 -9
  38. data/lib/jasonette/jason/body/header/search.rb +0 -5
  39. data/lib/jasonette/jbuilder_extensions.rb +0 -19
@@ -0,0 +1,10 @@
1
+ module Jasonette
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Jasonette
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec
7
+ # g.fixture_replacement :factory_girl, :dir => 'spec/factories'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ require 'jasonette/template'
2
+
3
+ module Jasonette
4
+ class Handler
5
+ cattr_accessor :default_format
6
+ self.default_format = Mime[:json]
7
+
8
+ def self.call(template)
9
+ has_jasonette_handler = template.locals.include?("_jasonette_handler")
10
+ %{__already_defined = defined?(jason); jason ||= Jasonette::Template.load(self); if(jason && #{has_jasonette_handler}); jason.encode(_jasonette_handler, &Proc.new {#{template.source}}); else; if jason.has_layout?(#{template.object_id}); jason = jason.new_jason(#{template.object_id}); else; jason.jason(&Proc.new {#{template.source}}); end; end;
11
+ jason.target! unless ((__already_defined && __already_defined != "method") || #{has_jasonette_handler})}
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ module Jasonette
2
+ module Helpers
3
+ def jason_builder property_name=nil, context=nil, &block
4
+ klass = Jasonette::Jason.new("").klass_for_property property_name
5
+
6
+ builder = builder(klass, context, &block)
7
+ if ::Kernel.block_given?
8
+ builder
9
+ else
10
+ BlockBuilder.new builder
11
+ end
12
+ end
13
+
14
+ class BlockBuilder
15
+ attr_reader :builder
16
+ def initialize builder
17
+ @builder = builder
18
+ end
19
+
20
+ def method_missing name, *args, &block
21
+ next_builder = builder.public_send name, *args
22
+ j.encode(next_builder, &block) if ::Kernel.block_given?
23
+ next_builder
24
+ end
25
+
26
+ private
27
+ def j
28
+ JasonSingleton.fetch(builder.context)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def builder klass, context, &block
35
+ klass.new(context || self, &block)
36
+ end
37
+ end
38
+ end
@@ -1,12 +1,5 @@
1
1
  module Jasonette
2
- class Jason::Body < Jasonette::Base
3
- property :header
4
- property :sections
5
-
6
- def attributes!
7
- a = super
8
- a["sections"] = [a["sections"]] if a.has_key?("sections")
9
- return a
10
- end
2
+ class Jason::Body < Jasonette::Body
3
+ super_property
11
4
  end
12
5
  end
@@ -7,7 +7,13 @@ module Jasonette
7
7
  property :templates
8
8
 
9
9
  def template name, *args, &block
10
- property_sender templates, name, *args, &block
10
+ if block_given?
11
+ item = Jasonette::Body.new(context, &block)
12
+ append item, "templates", name
13
+ else
14
+ property_sender templates, name, *args
15
+ end
16
+ self
11
17
  end
12
18
 
13
19
  def datum name, *args, &block
@@ -19,7 +25,21 @@ module Jasonette
19
25
  end
20
26
 
21
27
  def action name, *args, &block
22
- property_sender actions, name, *args, &block
28
+ if block_given?
29
+ item = Jasonette::Action.new(context, &block)
30
+ append item, "actions", name
31
+ else
32
+ property_sender actions, name, *args
33
+ end
34
+ self
35
+ end
36
+
37
+ private
38
+ def append builder, property_name, msg
39
+ @attributes[property_name] ||= {}
40
+ @attributes[property_name][msg] ||= {}
41
+ @attributes[property_name][msg].merge! builder.attributes!
42
+ builder
23
43
  end
24
44
  end
25
45
  end
@@ -0,0 +1,5 @@
1
+ module Jasonette
2
+ class Jason::Head::Actions < Jasonette::Action
3
+ super_property
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Jasonette
2
+ class Jason::Head::Templates < Jasonette::Body
3
+ super_property
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ require 'rails/railtie'
2
+ require 'jasonette/handler'
3
+ require 'jasonette/action_view_extensions'
4
+ require 'jasonette/helpers'
5
+
6
+ module Jasonette
7
+ class Railtie < ::Rails::Railtie
8
+ initializer :jasonette do
9
+ ActiveSupport.on_load :action_view do
10
+ ActionView::Template.register_template_handler :jasonette, Jasonette::Handler
11
+ include Jasonette::Helpers
12
+ end
13
+
14
+ # if Rails::VERSION::MAJOR >= 5
15
+ # module ::ActionController
16
+ # module ApiRendering
17
+ # include ActionView::Rendering
18
+ # end
19
+ # end
20
+ #
21
+ # ActiveSupport.on_load :action_controller do
22
+ # if self == ActionController::API
23
+ # include ActionController::Helpers
24
+ # include ActionController::ImplicitRender
25
+ # end
26
+ # end
27
+ # end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,201 @@
1
+ module Jasonette
2
+
3
+ class JasonSingleton
4
+ private_class_method :new
5
+ @@instances = []
6
+
7
+ def self.fetch context
8
+ if instance = find(context)
9
+ instance
10
+ else
11
+ instance = Jasonette::Template.new(context)
12
+ @@instances << instance
13
+ instance
14
+ end
15
+ end
16
+
17
+ def self.find context
18
+ @@instances.select { |instance| instance.context == context }.first
19
+ end
20
+
21
+ def self.reset context
22
+ if instance = find(context)
23
+ instances.delete_if { |i| i == instance }
24
+ end
25
+ end
26
+
27
+ def self.instances
28
+ @@instances
29
+ end
30
+ end
31
+
32
+ class Template
33
+ class << self
34
+ attr_accessor :template_lookup_options
35
+
36
+ def load context
37
+ JasonSingleton.fetch context
38
+ end
39
+ end
40
+ self.template_lookup_options = { handlers: [:jasonette] }
41
+
42
+ def method_missing name, *args, &block
43
+ if _last_builder
44
+ if _last_builder.property_names.include? name
45
+ _last_builder.public_send name, *args, &block
46
+ else
47
+ begin
48
+ context_method name, *args, &block
49
+ rescue
50
+ _last_builder.public_send name, *args, &block
51
+ end
52
+ end
53
+ else
54
+ context_method name, *args, &block
55
+ end
56
+ end
57
+
58
+ attr_accessor :__layout, :__locals
59
+ attr_reader :context, :attributes
60
+
61
+ def initialize context
62
+ @context = context
63
+ @attributes = {}
64
+ @blocks = []
65
+ self.extend ContexEmbedder if @context.present?
66
+ end
67
+
68
+ def has_layout? template_id
69
+ template = _get_view_template(template_id)
70
+ _layout_path && _layout_path != template.virtual_path
71
+ end
72
+
73
+ def new_jason template_id
74
+ new_jason = self.class.new context
75
+ new_jason.attributes["_template"] = "#{template_id}"
76
+ new_jason
77
+ end
78
+
79
+ def encode builder, &block
80
+ if ::Kernel.block_given?
81
+ @blocks << block
82
+ block.instance_variable_set("@builder", builder)
83
+ instance_eval(&block)
84
+ @blocks.delete(block)
85
+ else
86
+ raise "Wrong encode"
87
+ end
88
+ self
89
+ end
90
+
91
+ def jason name=nil, &block
92
+ builder = Jasonette::Jason.new(context)
93
+ encode(builder, &block)
94
+ @attributes[name || "$jason"] = builder.attributes!
95
+ JasonSingleton.reset context
96
+ self
97
+ end
98
+ alias build jason
99
+
100
+ def attributes!
101
+ @attributes
102
+ end
103
+
104
+ def target!
105
+ ::MultiJson.dump attributes!
106
+ end
107
+
108
+ def set! name, object = nil, *args
109
+ super
110
+ end
111
+
112
+ def array! collection = [], *args
113
+ super
114
+ end
115
+
116
+ def extract! object, *attributes
117
+ super
118
+ end
119
+
120
+ def merge! key
121
+ case key
122
+ when ActiveSupport::SafeBuffer
123
+ values = ::MultiJson.load(key) || {}
124
+
125
+ if template = _get_view_template(values["_template"])
126
+ options = { locals: __locals, template: template.virtual_path }
127
+ _render_partial_with_options options
128
+ end
129
+ else
130
+ super
131
+ end
132
+ @attributes
133
+ end
134
+
135
+ def partial! *args
136
+ _render_explicit_partial(*args)
137
+ end
138
+
139
+ private
140
+
141
+ def context_method name, *args, &block
142
+ context.public_send(name, *args, &block)
143
+ end
144
+
145
+ def _last_builder
146
+ @blocks.last.instance_variable_get("@builder")
147
+ end
148
+
149
+ def _layout_path
150
+ __layout && __layout.try(:virtual_path)
151
+ end
152
+
153
+ def _get_view_template template_id
154
+ ObjectSpace._id2ref(template_id.to_i)
155
+ end
156
+
157
+ def _render_explicit_partial(name_or_options, locals = {})
158
+ case name_or_options
159
+ when ::Hash
160
+ # partial! partial: 'name', foo: 'bar'
161
+ options = name_or_options
162
+ else
163
+ # partial! 'name', locals: {foo: 'bar'}
164
+ if locals.one? && (locals.keys.first == :locals)
165
+ options = locals.merge(partial: name_or_options)
166
+ else
167
+ options = { partial: name_or_options, locals: locals }
168
+ end
169
+ # partial! 'name', foo: 'bar'
170
+ # TODO : Add feature for option :as and :collection
171
+ # as = locals.delete(:as)
172
+ # options[:as] = as if as.present?
173
+ # options[:collection] = locals[:collection] if locals.key?(:collection)
174
+ end
175
+
176
+ _render_partial_with_options options
177
+ end
178
+
179
+ def _render_partial_with_options(options)
180
+ options.reverse_merge! locals: {}
181
+ options.reverse_merge! self.class.template_lookup_options
182
+
183
+ _render_partial options
184
+ end
185
+
186
+ def _render_partial(options)
187
+ options[:locals].merge! _jasonette_handler: _last_builder
188
+ context.render options
189
+ end
190
+ end
191
+
192
+ module ContexEmbedder
193
+ def self.extended(klass_obj)
194
+ context = klass_obj.context
195
+ context.instance_variables.each do |var|
196
+ raise "Jason is using #{var} instance variable. Please change variable name." if klass_obj.instance_variable_get(var)
197
+ klass_obj.instance_variable_set var, context.instance_variable_get(var)
198
+ end
199
+ end
200
+ end
201
+ end
@@ -1,3 +1,3 @@
1
1
  module Jasonette
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jasonette-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mwlang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-20 00:00:00.000000000 Z
11
+ date: 2017-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -25,33 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: actionview
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 4.0.0
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: 4.0.0
41
- - !ruby/object:Gem::Dependency
42
- name: jbuilder
28
+ name: multi_json
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - "~>"
46
32
  - !ruby/object:Gem::Version
47
- version: 2.6.1
33
+ version: '1.2'
48
34
  type: :runtime
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
38
  - - "~>"
53
39
  - !ruby/object:Gem::Version
54
- version: 2.6.1
40
+ version: '1.2'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +94,20 @@ dependencies:
108
94
  - - "~>"
109
95
  - !ruby/object:Gem::Version
110
96
  version: 3.5.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rspec-its
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: 0.7.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: sqlite3
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
153
167
  description: Jbuilder bolt-on to make Jasonette JSON easier to produce
154
168
  email:
155
169
  - mwlang@cybrains.net
@@ -162,19 +176,37 @@ files:
162
176
  - Rakefile
163
177
  - lib/jasonette-rails.rb
164
178
  - lib/jasonette.rb
179
+ - lib/jasonette/action_view_extensions.rb
180
+ - lib/jasonette/core/action.rb
165
181
  - lib/jasonette/core/base.rb
182
+ - lib/jasonette/core/body.rb
183
+ - lib/jasonette/core/body/footer.rb
184
+ - lib/jasonette/core/body/footer/input.rb
185
+ - lib/jasonette/core/body/footer/tabs.rb
186
+ - lib/jasonette/core/body/header.rb
187
+ - lib/jasonette/core/body/header/search.rb
188
+ - lib/jasonette/core/body/layers.rb
189
+ - lib/jasonette/core/body/sections.rb
166
190
  - lib/jasonette/core/components.rb
191
+ - lib/jasonette/core/data.rb
192
+ - lib/jasonette/core/item.rb
167
193
  - lib/jasonette/core/items.rb
168
194
  - lib/jasonette/core/layout.rb
195
+ - lib/jasonette/core/map.rb
196
+ - lib/jasonette/core/options.rb
197
+ - lib/jasonette/core/pins.rb
169
198
  - lib/jasonette/core/properties.rb
170
- - lib/jasonette/core/sections.rb
171
199
  - lib/jasonette/core/style.rb
200
+ - lib/jasonette/engine.rb
201
+ - lib/jasonette/handler.rb
202
+ - lib/jasonette/helpers.rb
172
203
  - lib/jasonette/jason.rb
173
204
  - lib/jasonette/jason/body.rb
174
- - lib/jasonette/jason/body/header.rb
175
- - lib/jasonette/jason/body/header/search.rb
176
205
  - lib/jasonette/jason/head.rb
177
- - lib/jasonette/jbuilder_extensions.rb
206
+ - lib/jasonette/jason/head/actions.rb
207
+ - lib/jasonette/jason/head/templates.rb
208
+ - lib/jasonette/railtie.rb
209
+ - lib/jasonette/template.rb
178
210
  - lib/jasonette/version.rb
179
211
  - lib/tasks/jasonette_tasks.rake
180
212
  homepage: http://codeconnoisseur.org