client_data 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7074dfc10a10c2d65cea097fb30b841a965ccad4
4
- data.tar.gz: cb532832ec22432dae24b5bc2eb41bf482bd2797
3
+ metadata.gz: 11f350caa58a1d9912dbf17d79541886b0ff30dd
4
+ data.tar.gz: aa876738e11ba40782ba84cf50ff4c82417c5f5c
5
5
  SHA512:
6
- metadata.gz: 3798648fed6ea6a73cd795b3bcd9c808cedbf4dbeceaaaf8336f150ed87e085be57f6daac6f5b85c7e883a5146710d07bd7dd8e06faff8a6fc0a325216af4db3
7
- data.tar.gz: a26502b655d6605430bd7b5e6061607a8646a050fa4b037cc40d9d1299f97c075fbadf5c2320fdd28b6d753ade10949319035cae5ec5dafa96f2054c280dedef
6
+ metadata.gz: 509626eced24f1ed1cf92d92da9a6f76830b63ded9bdbb62cb69d9e3eef5bcd0f098631900d999ddef064213bb920b337188a980d7ac7360803d3a36e9a02e5c
7
+ data.tar.gz: acc3f301f35175dfd8a529d214e7065c5f2006964e0d5c17c6ba45dbc049d8f8f2f5df5bf92487ae6a7dcf09de7652f11b3e54c3f658d4761b153ad19f7b49f9
@@ -0,0 +1,41 @@
1
+ # Thanks to: https://coderwall.com/p/ih8atg
2
+ module ClientData
3
+ module BeforeRender
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ alias_method_chain :render, :before_render_action
8
+ define_callbacks :render
9
+ end
10
+
11
+ def render_with_before_render_action(*options, &block)
12
+ run_callbacks :render do
13
+ render_without_before_render_action(*options, &block)
14
+ end
15
+ end
16
+
17
+ module ClassMethods
18
+ def append_before_render_action(*names, &block)
19
+ _insert_callbacks(names, block) do |name, options|
20
+ set_callback :render, :before, name, options
21
+ end
22
+ end
23
+
24
+ def prepend_before_render_action(*names, &block)
25
+ _insert_callbacks(names, block) do |name, options|
26
+ set_callback :render, :before, name, options.merge(:prepend => true)
27
+ end
28
+ end
29
+
30
+ def skip_before_render_action(*names, &block)
31
+ _insert_callbacks(names, block) do |name, options|
32
+ skip_callback :render, :before, name, options
33
+ end
34
+ end
35
+
36
+ alias_method :before_render, :append_before_render_action
37
+ alias_method :prepend_before_render, :prepend_before_render_action
38
+ alias_method :skip_before_render, :skip_before_render_action
39
+ end
40
+ end
41
+ end
@@ -5,6 +5,12 @@ module ClientData
5
5
  def initialize(controller = nil)
6
6
  @controller = controller
7
7
  end
8
+
9
+ def self.properties(*props)
10
+ props.each do |m|
11
+ define_method(m) { controller.instance_variable_get(:"@#{m}") }
12
+ end
13
+ end
8
14
  end
9
15
  end
10
16
 
@@ -1,62 +1,92 @@
1
1
  module ClientData
2
2
  module Methods
3
- def self.included(klass)
4
- klass.send(:before_filter, :client_data_filter) if klass.respond_to?(:before_filter)
5
- klass.class_eval %(
6
- class << self; attr_accessor :__cs_builders; end
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ send(:include, ClientData::BeforeRender) unless respond_to?(:before_render)
7
+ before_render :client_data_builder_filter
8
+
9
+ class_eval %(
10
+ class << self; attr_accessor :__cs_builders; end
7
11
  )
8
- klass.send(:include, InstanceMethods)
9
- klass.send(:extend, ClassMethods)
10
12
  end
11
13
 
12
14
  module ClassMethods
13
- def client_data(*configs)
14
- self.__cs_builders ||= []
15
- self.__cs_builders.concat(configs).uniq!
15
+ def client_data(*build_keys)
16
+ options = build_keys.extract_options!
17
+ self.__cs_builders ||= {}
18
+ build_keys.each do |key|
19
+ self.__cs_builders[key] = options
20
+ end
16
21
  end
17
22
  end
18
23
 
19
- module InstanceMethods
20
- def client_data_filter
21
- config_keys.each do |key|
22
- begin
23
- klass = "#{builder_namespace}#{key.to_s.capitalize}Builder"
24
- klass = klass.constantize
25
- rescue ::NameError => e
26
- raise ClientData::Error, "Unable to find constant #{klass}, " \
27
- "has ClientData.load_resources! been called? " \
28
- "Error: #{e.message}"
29
- end
30
-
31
- data = build_data(klass)
32
- provider.set(key, data)
24
+ def builders
25
+ @builders ||= begin
26
+ builders_hash = {}
27
+ builder_options_hash.each do |key, options|
28
+ name = options[:as].nil? ? key : options[:as]
29
+ builders_hash[name] = create_builder(key)
33
30
  end
31
+ builders_hash
34
32
  end
33
+ end
35
34
 
36
- protected
35
+ protected
37
36
 
38
- def build_data(klass)
39
- arity = klass.instance_method(:initialize).arity
40
- builder = arity == 0 ? klass.new : klass.new(self)
41
- builder.build
37
+ def client_data_builder_filter
38
+ builder_options_hash.each do |key, options|
39
+ name = options[:as].nil? ? key : options[:as]
40
+ builder = builders[name]
41
+ provider.set(name.to_s, builder.build) if should_build_for?(key)
42
42
  end
43
+ end
43
44
 
44
- def builder_namespace
45
- ClientData.configuration.builder_namespace
45
+ def should_build_for?(key)
46
+ options = builder_options_hash[key] || {}
47
+ action = self.action_name.to_sym
48
+ only = options[:only]
49
+ if only.nil?
50
+ true
51
+ else
52
+ if only.is_a?(Symbol)
53
+ only = [only]
54
+ end
55
+ only.include?(action)
46
56
  end
57
+ end
47
58
 
48
- def provider
49
- @provider ||= Adapters.factory(self)
59
+ def create_builder(key)
60
+ begin
61
+ klass = key
62
+ if klass.is_a?(Symbol) || klass.is_a?(String)
63
+ klass = "#{builder_namespace}#{key.to_s.camelize}Builder"
64
+ klass = klass.constantize
65
+ end
66
+ rescue ::NameError => e
67
+ raise ClientData::Error, "Unable to find constant #{klass}, " \
68
+ "has ClientData.load_resources! been called? " \
69
+ "Error: #{e.message}"
50
70
  end
51
71
 
52
- def config_keys
53
- @keys ||= begin
54
- keys = []
55
- klass = self.class
56
- loop do
57
- keys += klass.__cs_builders || [] if klass.respond_to?(:__cs_builders)
58
- break keys unless klass.respond_to?(:superclass) && klass = klass.superclass
59
- end
72
+ klass.new.tap { |o| o.controller = self }
73
+ end
74
+
75
+ def builder_namespace
76
+ ClientData.configuration.builder_namespace
77
+ end
78
+
79
+ def provider
80
+ @_builder_provider ||= Adapters.factory(self)
81
+ end
82
+
83
+ def builder_options_hash
84
+ @_builder_options_hash ||= begin
85
+ keys = {}
86
+ klass = self.class
87
+ loop do
88
+ keys.merge!(klass.__cs_builders || {}) if klass.respond_to?(:__cs_builders)
89
+ break keys unless klass.respond_to?(:superclass) && klass = klass.superclass
60
90
  end
61
91
  end
62
92
  end
@@ -1,3 +1,3 @@
1
1
  module ClientData
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/client_data.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'active_support/core_ext/hash'
2
2
 
3
3
  require 'client_data/configuration'
4
+ require 'client_data/before_render'
4
5
  require 'client_data/methods'
5
6
  require 'client_data/builder'
6
7
  require 'client_data/adapters'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: client_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stan Bondi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-20 00:00:00.000000000 Z
11
+ date: 2014-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -67,6 +67,7 @@ files:
67
67
  - lib/client_data.rb
68
68
  - lib/client_data/adapters.rb
69
69
  - lib/client_data/adapters/gon_adapter.rb
70
+ - lib/client_data/before_render.rb
70
71
  - lib/client_data/builder.rb
71
72
  - lib/client_data/configuration.rb
72
73
  - lib/client_data/methods.rb
@@ -97,3 +98,4 @@ signing_key:
97
98
  specification_version: 4
98
99
  summary: Use builder classes to build up data which JS can use.
99
100
  test_files: []
101
+ has_rdoc: