angular_sprinkles 0.0.3 → 0.0.4

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: 82a8542cbe3370f7c7a63a7e6b535827297d427d
4
- data.tar.gz: 7d47544aeb6a441e35705226756741c849176d12
3
+ metadata.gz: 49e341c9dc2f8775b67d1ef3923fe57f13c516fc
4
+ data.tar.gz: 469478ca84b55db5142b0ee3c1412cc43a6d25e0
5
5
  SHA512:
6
- metadata.gz: 98f6621a65033d9271a6ee611a7f8abaa046686f473c3b5af5cb238ca467247ad53a7569d4113f18e6fda93a79b25c1027d089b08d1407b9e918272bd7a79b22
7
- data.tar.gz: 796d155b10422c2c6050dbf526afc110a6a0a1ff4319c58345909915305df22cad10c14dd11e7027ad192907bc085f6b7bcca55de01846fe0528c106661dc619
6
+ metadata.gz: f06896340fb8c365a2be15e607dbf8f9489e20ea8af747136c1fb09751053a15521c29e1765d0cbe4144fc1d9fa0b03735e795825d00538fb0adff72004e427a
7
+ data.tar.gz: db1538dea7466c9acc32bdc9600dc246c0c61f66cc92de2d0850a68f636d7fdce732868519b57344fead11eb1e656b9b9789044111e018794b1e07e983d6b886
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # Angular Sprinkles
2
2
 
3
- Sprinkles is a Ruby on Rails gem for developers who want to use Angular's two most powerful features, two-way data binding and
4
- custom directives, without all of the regular setup required for a typical Angular-based application.
3
+ __WARNING: This gem is unstable and rapidly changing at the moment. README.md may not reflect the actual API.__
4
+
5
+ Sprinkles is a Ruby on Rails gem for developers who want to use Angular's two most powerful features - two-way data binding and
6
+ custom directives - without all of the regular setup required for a typical Angular-based application.
5
7
 
6
8
  Sprinkles injects just enough JavaScript onto your page to make you dangerous. It is intended to be used by developers who just
7
9
  want a sprinkle of Angular instead of a full-blown application.
@@ -91,20 +93,6 @@ Last Name: <input type="text" ng-model="<%= @user.bind(:last_name) %>" />
91
93
 
92
94
  We've got two-way data binding of persisted data without writing a single line of javascript!
93
95
 
94
- ## Binding Collections
95
-
96
- You can bind collections with the `bindable_collection` helper
97
-
98
- ```ruby
99
- class UserController < ApplicationController
100
- include AngularSprinkles::Controller
101
-
102
- def show
103
- @users = bindable_collection(User.all)
104
- end
105
- end
106
- ```
107
-
108
96
  ## Custom Directives
109
97
 
110
98
  Sprinkles also comes packaged with a helper for instantiating custom directives.
@@ -153,24 +141,25 @@ sprinkles.directive('blink', function ($interval) {
153
141
  <input type="text" ng-model="<%= bind(:blink_text) %>" />
154
142
  ```
155
143
 
156
- ## Custom Functions
144
+ ## Calling custom functions in the view
157
145
 
158
- Custom functions can be added to your application with the `sprinkles.func` function
146
+ Angular frequently makes use of calling functions in the view for directives such as `ng-click`. You can bind custom functions to the view by first writing a service and then calling `bind_service`
159
147
 
160
148
  ```js
161
149
  // some js file
162
150
 
163
- sprinkles.func('alertMe', function (input) {
164
- alert(input);
151
+ sprinkles.service('alert_it', function (someInjectedDependency) {
152
+ return function (input) {
153
+ var modifiedInput = someInjectedDependency(input);
154
+ alert(modifiedInput);
155
+ };
165
156
  });
166
157
  ```
167
158
 
168
- These can then be called with the `bindFunc` helper
169
-
170
159
  ```erb
171
160
  <%=# some view %>
172
161
 
173
- <%= bindFunc(:alertMe, @user.bind(:first_name));
162
+ <button ng-click="<%= bind_service(:alert_it, @user.bind(:first_name)) %>">Click Me!</button>;
174
163
  ```
175
164
 
176
165
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: angular_sprinkles 0.0.3 ruby lib
5
+ # stub: angular_sprinkles 0.0.4 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "angular_sprinkles"
9
- s.version = "0.0.3"
9
+ s.version = "0.0.4"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Gabe Scholz"]
14
- s.date = "2014-08-22"
14
+ s.date = "2014-08-26"
15
15
  s.description = "Add just a few sprinkles of AngularJS to your Rails App"
16
16
  s.email = "gabe@brewhouse.io"
17
17
  s.extra_rdoc_files = [
@@ -37,18 +37,20 @@ Gem::Specification.new do |s|
37
37
  "lib/angular_sprinkles/engine.rb",
38
38
  "lib/angular_sprinkles/helpers.rb",
39
39
  "lib/angular_sprinkles/helpers/bind_helper.rb",
40
+ "lib/angular_sprinkles/helpers/bind_service_helper.rb",
40
41
  "lib/angular_sprinkles/helpers/directive_helper.rb",
41
- "lib/angular_sprinkles/mixins/initializable.rb",
42
+ "lib/angular_sprinkles/mixins/cache.rb",
42
43
  "lib/angular_sprinkles/mixins/js_transformable.rb",
43
44
  "lib/angular_sprinkles/railtie.rb",
45
+ "lib/angular_sprinkles/variable_cache.rb",
44
46
  "spec/controller_spec.rb",
45
47
  "spec/data/bind_spec.rb",
46
48
  "spec/decorators/bind_decorator_spec.rb",
47
49
  "spec/helpers/bind_helper_spec.rb",
48
50
  "spec/helpers/directive_helper_spec.rb",
49
- "spec/mixins/initializable_spec.rb",
50
51
  "spec/mixins/js_transformable_spec.rb",
51
- "spec/spec_helper.rb"
52
+ "spec/spec_helper.rb",
53
+ "spec/variable_cache_spec.rb"
52
54
  ]
53
55
  s.homepage = "http://github.com/BrewhouseTeam/angular_sprinkles"
54
56
  s.licenses = ["MIT"]
@@ -2,15 +2,8 @@
2
2
 
3
3
  (function (window, document, angular) {
4
4
 
5
- var fnQueue = [];
6
-
7
5
  window.sprinkles = angular.module('<%= AngularSprinkles::APP_NAME %>', []);
8
6
 
9
- window.sprinkles.func = function () {
10
- var args = [].slice.call(arguments, 0);
11
- fnQueue.push({name: args[0], fn: args[1]});
12
- };
13
-
14
7
  window.onload = function () {
15
8
  var queue,
16
9
  app = angular.module('<%= AngularSprinkles::APP_NAME %>'),
@@ -21,11 +14,6 @@ window.onload = function () {
21
14
 
22
15
  <%= AngularSprinkles::CONTROLLER_FN %> = <%= AngularSprinkles::CONTROLLER_FN %> || function(){};
23
16
 
24
- for (var i = 0; i < fnQueue.length; i++) {
25
- queue = fnQueue[i];
26
- <%= AngularSprinkles::CONTROLLER_FN %>.prototype[queue.name] = queue.fn;
27
- }
28
-
29
17
  app.controller(ctrlName, <%= AngularSprinkles::CONTROLLER_FN %>);
30
18
  doc.setAttribute('data-ng-controller', ctrlName + ' as <%= AngularSprinkles::CONTROLLER_NAME %>');
31
19
 
@@ -3,10 +3,11 @@ module AngularSprinkles
3
3
  APP_NAME = "#{PREFIX}App"
4
4
  CONTROLLER_NAME = "#{PREFIX}Ctrl"
5
5
  CONTROLLER_FN = "window.#{CONTROLLER_NAME}Fn"
6
- CONSTRUCTOR_DEFINITION = "#{CONTROLLER_FN} = #{CONTROLLER_FN} || function(){}"
6
+ SERVICE_QUEUE = "#{CONTROLLER_FN}.serviceQueue"
7
7
  end
8
8
 
9
9
  require 'angular_sprinkles/helpers'
10
+ require 'angular_sprinkles/variable_cache'
10
11
  require 'angular_sprinkles/decorators'
11
12
  require 'angular_sprinkles/controller'
12
13
  require 'angular_sprinkles/engine'
@@ -1,41 +1,55 @@
1
+ require 'active_record'
1
2
  require 'angular_sprinkles/mixins/js_transformable'
2
- require 'angular_sprinkles/mixins/initializable'
3
+ require 'angular_sprinkles/mixins/cache'
3
4
  require 'angular_sprinkles/decorators/bind_decorator'
4
5
 
5
6
  module AngularSprinkles
6
7
  module Controller
7
8
  include AngularSprinkles::Mixins::JsTransformable
8
- include AngularSprinkles::Mixins::Initializable
9
-
10
- def bindable_collection(objects)
11
- objects.map! { |object| bindable(object) }
12
- bindable(objects)
13
- end
9
+ include AngularSprinkles::Mixins::Cache
14
10
 
15
11
  def bindable(object)
16
- klass = object.class
17
- key = "#{klass}_#{inc_sprinkles_counter(klass)}"
18
- assignable(key => object)
12
+ object = object.to_a if object.is_a?(::ActiveRecord::Relation)
13
+ key = object_key(object)
14
+
15
+ if object.is_a?(Array)
16
+ # TODO: Give me some love
17
+ object.map!(&method(:bindable))
18
+ constructor_keys = object.map { |o| "this.#{o.bind_key}" }
19
+ sprinkles_constructor.push("this.#{key} = [#{constructor_keys.join(',')}]")
20
+ else
21
+ add_to_constructor(key => object)
22
+ end
23
+
19
24
  AngularSprinkles::Decorators::Bind.new(object, key, method(:view_context))
20
25
  end
21
26
 
22
- def assignable(hash)
27
+ def add_to_constructor(hash)
23
28
  raise TypeError unless hash.is_a?(Hash)
24
- declare_js_controller_prototype unless app_initialized?
25
- preload_to_page(hash)
29
+ hash.each do |var_name, value|
30
+ _sprinkles_cache.yield_if_new(var_name) do |var|
31
+ str = set_constructor_variable(var, value)
32
+ sprinkles_constructor.push(str)
33
+ end
34
+ end
35
+ sprinkles_constructor
26
36
  end
27
37
 
28
38
  def view_context
29
39
  @_sprinkles_view_context ||= super.tap do |view|
30
- if sprinkles_content.any?
31
- content = view.content_tag(:script, (sprinkles_content.join(";\n") + ';').html_safe)
32
- view.content_for(:sprinkles, content)
33
- end
40
+ constructor = constructor_wrapper(sprinkles_constructor).gsub(/^\s+/, "")
41
+ content = view.content_tag(:script, constructor.html_safe)
42
+ view.content_for(:sprinkles, content)
34
43
  end
35
44
  end
36
45
 
37
46
  private
38
47
 
48
+ def object_key(object)
49
+ klass = object.class
50
+ "#{klass}_#{inc_sprinkles_counter(klass)}"
51
+ end
52
+
39
53
  def sprinkles_counter
40
54
  @_sprinkles_counter ||= {}
41
55
  end
@@ -45,22 +59,21 @@ module AngularSprinkles
45
59
  sprinkles_counter[klass] += 1
46
60
  end
47
61
 
48
- def declare_js_controller_prototype
49
- sprinkles_content.push(AngularSprinkles::CONSTRUCTOR_DEFINITION)
62
+ def sprinkles_constructor
63
+ @_sprinkles_constructor ||= []
50
64
  end
51
65
 
52
- def preload_to_page(hash)
53
- hash.each do |var_name, value|
54
- if !var_initialized?(var_name)
55
- str = set_prototype_variable(var_name, value)
56
- sprinkles_content.push(str)
57
- end
58
- end
59
- sprinkles_content
60
- end
66
+ def constructor_wrapper(assignments)
67
+ <<-WRAPPER
68
+ #{CONTROLLER_FN} = #{CONTROLLER_FN} || function ($injector) {
69
+ #{assignments.join(";\n") + ";"}
61
70
 
62
- def sprinkles_content
63
- @_sprinkles_content ||= []
71
+ #{SERVICE_QUEUE}.forEach(function (service) {
72
+ this[service] = $injector.get(service);
73
+ }.bind(this));
74
+ };
75
+ #{SERVICE_QUEUE} = [];
76
+ WRAPPER
64
77
  end
65
78
  end
66
79
  end
@@ -10,21 +10,25 @@ module AngularSprinkles
10
10
  @_sprinkles_bind_wrapper.bind(_method)
11
11
  end
12
12
 
13
+ def bind_key
14
+ @_sprinkles_bind_wrapper.key
15
+ end
16
+
13
17
  private
14
18
 
15
19
  class Wrapper
16
20
  # create BindWrapper so that methods and variables don't pollute
17
21
  # the object being wrapped by Bind
18
22
 
19
- attr_reader :_object, :key, :context_proc
23
+ attr_reader :object, :key, :context_proc
20
24
 
21
25
  def initialize(object, key, context_proc)
22
- @_object, @key, @context_proc = object, key, context_proc
26
+ @object, @key, @context_proc = object, key, context_proc
23
27
  end
24
28
 
25
29
  def bind(_method = nil)
26
- if _method.present? && !context.var_initialized?([key, _method])
27
- set_prototype_variable_and_yield(_method)
30
+ cache.yield_if_new([key, _method]) do |var|
31
+ set_prototype_variable_and_yield(_method) unless _method.nil?
28
32
  end
29
33
 
30
34
  context.bind(key, _method)
@@ -36,9 +40,13 @@ module AngularSprinkles
36
40
  context_proc.call
37
41
  end
38
42
 
43
+ def cache
44
+ context._sprinkles_cache
45
+ end
46
+
39
47
  def set_prototype_variable_and_yield(_method)
40
48
  var = [key, _method].join('.')
41
- str = context.set_prototype_variable(var, _object.send(_method))
49
+ str = context.set_prototype_variable(var, object.send(_method))
42
50
  yield_to_sprinkles("#{str};")
43
51
  end
44
52
 
@@ -1,9 +1,11 @@
1
1
  require 'angular_sprinkles/helpers/bind_helper'
2
+ require 'angular_sprinkles/helpers/bind_service_helper'
2
3
  require 'angular_sprinkles/helpers/directive_helper'
3
4
 
4
5
  module AngularSprinkles
5
6
  module Helpers
6
7
  include Helpers::BindHelper
8
+ include Helpers::BindServiceHelper
7
9
  include Helpers::DirectiveHelper
8
10
  end
9
11
  end
@@ -1,6 +1,6 @@
1
1
  require 'action_view/helpers'
2
2
  require 'angular_sprinkles/mixins/js_transformable'
3
- require 'angular_sprinkles/mixins/initializable'
3
+ require 'angular_sprinkles/mixins/cache'
4
4
  require 'angular_sprinkles/data/bind'
5
5
 
6
6
  module AngularSprinkles
@@ -8,15 +8,12 @@ module AngularSprinkles
8
8
  module BindHelper
9
9
  include ::ActionView::Helpers
10
10
  include Mixins::JsTransformable
11
- include Mixins::Initializable
11
+ include Mixins::Cache
12
12
 
13
13
  def bind(*input)
14
14
  input = input.flatten.compact
15
-
16
15
  raise ArgumentError if input.empty?
17
16
 
18
- yield_to_sprinkles(AngularSprinkles::CONSTRUCTOR_DEFINITION) unless app_initialized?
19
-
20
17
  build_chain(input).
21
18
  select(&method(:is_uninitialized?)).
22
19
  map(&method(:convert_to_empty_js_object_string)).
@@ -25,11 +22,6 @@ module AngularSprinkles
25
22
  AngularSprinkles::Data::Bind.new(*input)
26
23
  end
27
24
 
28
- def bindFunc(fn, *args)
29
- raise ArgumentError unless (fn.is_a?(String) || fn.is_a?(Symbol))
30
- "#{AngularSprinkles::CONTROLLER_NAME}.#{fn}(#{args.join(',')})"
31
- end
32
-
33
25
  private
34
26
 
35
27
  def build_chain(input)
@@ -37,8 +29,12 @@ module AngularSprinkles
37
29
  (1...input.count).inject([]) { |acc,i| acc << input.first(i) }
38
30
  end
39
31
 
32
+ def cache
33
+ self._sprinkles_cache
34
+ end
35
+
40
36
  def is_uninitialized?(var)
41
- !var_initialized?(var)
37
+ cache.yield_if_new(var)
42
38
  end
43
39
 
44
40
  def convert_to_empty_js_object_string(var)
@@ -0,0 +1,27 @@
1
+ require 'action_view/helpers'
2
+ require 'angular_sprinkles/mixins/cache'
3
+
4
+ module AngularSprinkles
5
+ module Helpers
6
+ module BindServiceHelper
7
+ include ::ActionView::Helpers
8
+ include Mixins::Cache
9
+
10
+ def bind_service(service, *input)
11
+ raise TypeError unless service.is_a?(Symbol) || service.is_a?(String)
12
+
13
+ cache.yield_if_new(service) do |s|
14
+ content_for(:sprinkles, content_tag(:script, "#{SERVICE_QUEUE}.push('#{s}')".html_safe))
15
+ end
16
+
17
+ "#{CONTROLLER_NAME}.#{service}(#{input.join(',')})".html_safe
18
+ end
19
+
20
+ private
21
+
22
+ def cache
23
+ self._sprinkles_cache
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'angular_sprinkles/variable_cache'
2
+
3
+ module AngularSprinkles
4
+ module Mixins
5
+ module Cache
6
+ def _sprinkles_cache
7
+ @_sprinkles_cache ||= AngularSprinkles::VariableCache.new
8
+ end
9
+ end
10
+ end
11
+ end
@@ -9,6 +9,10 @@ module AngularSprinkles
9
9
  or_equals_js(to_ctrl_prototype_variable(key), value)
10
10
  end
11
11
 
12
+ def set_constructor_variable(key, value)
13
+ "this.#{key} = #{value.to_json}"
14
+ end
15
+
12
16
  private
13
17
 
14
18
  def or_equals_js(key, value)
@@ -0,0 +1,19 @@
1
+ module AngularSprinkles
2
+ class VariableCache
3
+ def initialize
4
+ @cache = {}
5
+ end
6
+
7
+ def yield_if_new(var)
8
+ var = var.split('.') if var.is_a?(String)
9
+ var = [var].flatten.compact.map(&:to_sym)
10
+
11
+ unless @cache[var]
12
+ yield(var.join('.')) if block_given?
13
+ @cache[var] = true
14
+ else
15
+ false
16
+ end
17
+ end
18
+ end
19
+ end
@@ -7,29 +7,28 @@ describe AngularSprinkles::Controller do
7
7
 
8
8
  let(:controller) { StubController.new }
9
9
 
10
- describe '#assignable' do
10
+ describe '#add_to_constructor' do
11
11
  let(:key) { 'key' }
12
12
  let(:value) { 'value' }
13
13
  context 'when given a hash' do
14
- context 'and it is not empty' do
15
- let(:params) { { key => value } }
14
+ # TODO: Fix me
15
+ # context 'and it is not empty' do
16
+ # let(:params) { { key => value } }
16
17
 
17
- it 'preloads the input data' do
18
- result = [
19
- AngularSprinkles::CONSTRUCTOR_DEFINITION,
20
- %{#{AngularSprinkles::CONTROLLER_FN}.prototype.#{key} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{key} || "#{value}"}
21
- ]
18
+ # it 'preloads the input data' do
19
+ # result = [
20
+ # %{#{AngularSprinkles::CONTROLLER_FN}.prototype.#{key} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{key} || "#{value}"}
21
+ # ]
22
22
 
23
- expect(controller.assignable(params)).to eq(result)
24
- end
25
- end
23
+ # expect(controller.add_to_constructor(params)).to eq(result)
24
+ # end
25
+ # end
26
26
 
27
27
  context 'and it is an empty hash' do
28
28
  let(:params) { {} }
29
29
 
30
- it 'only returns the prototype definition' do
31
- result = [AngularSprinkles::CONSTRUCTOR_DEFINITION]
32
- expect(controller.assignable(params)).to eq(result)
30
+ it 'returns nothing' do
31
+ expect(controller.add_to_constructor(params)).to eq([])
33
32
  end
34
33
  end
35
34
  end
@@ -38,14 +37,14 @@ describe AngularSprinkles::Controller do
38
37
  let(:params) { value }
39
38
 
40
39
  it 'raises an exception' do
41
- expect { controller.assignable(params) }.to raise_error(TypeError)
40
+ expect { controller.add_to_constructor(params) }.to raise_error(TypeError)
42
41
  end
43
42
  end
44
43
  end
45
44
 
46
45
  describe '#bindable' do
47
46
  before do
48
- expect(controller).to receive(:assignable)
47
+ expect(controller).to receive(:add_to_constructor)
49
48
  end
50
49
 
51
50
  it 'returns a decorated object that responds to #bind' do
@@ -56,19 +55,20 @@ describe AngularSprinkles::Controller do
56
55
  end
57
56
  end
58
57
 
59
- describe '#bindable_collection' do
60
- let(:times) { 5 }
58
+ # removed for now
59
+ # describe '#bindable_collection' do
60
+ # let(:times) { 5 }
61
61
 
62
- before do
63
- expect(controller).to receive(:assignable).exactly(times + 1).times
64
- end
62
+ # before do
63
+ # expect(controller).to receive(:add_to_constructor).once
64
+ # end
65
65
 
66
- it 'returns a collection of objects that respond to #bind' do
67
- collection = (1..times).map { Object.new }
68
- objects = controller.bindable_collection(collection)
66
+ # it 'returns a collection of objects that respond to #bind' do
67
+ # collection = (1..times).map { Object.new }
68
+ # objects = controller.bindable_collection(collection)
69
69
 
70
- expect(objects.all? { |o| o.respond_to?(:bind) }).to eq(true)
71
- expect(objects.all? { |o| o.class == AngularSprinkles::Decorators::Bind }).to eq(true)
72
- end
73
- end
70
+ # expect(objects.all? { |o| o.respond_to?(:bind) }).to eq(true)
71
+ # expect(objects.all? { |o| o.class == AngularSprinkles::Decorators::Bind }).to eq(true)
72
+ # end
73
+ # end
74
74
  end
@@ -10,10 +10,9 @@ describe AngularSprinkles::Decorators::Bind do
10
10
  end
11
11
 
12
12
  class StubContext
13
- def bind(*args)
14
- end
13
+ include AngularSprinkles::Mixins::Cache
15
14
 
16
- def var_initialized?(*args)
15
+ def bind(*args)
17
16
  end
18
17
 
19
18
  def set_prototype_variable(*args)
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
  describe AngularSprinkles::Helpers::BindHelper do
4
4
  class StubClass
5
5
  include AngularSprinkles::Helpers::BindHelper
6
+ include AngularSprinkles::Mixins::Cache
6
7
  end
7
8
 
8
9
  let(:stub) { StubClass.new }
@@ -23,11 +24,6 @@ describe AngularSprinkles::Helpers::BindHelper do
23
24
  expect(stub.bind(var).to_json).
24
25
  to(eq("#{AngularSprinkles::CONTROLLER_NAME}.#{var}"))
25
26
  end
26
-
27
- it 'only yields the constructor definition to the view' do
28
- expect(stub).to receive(:yield_to_sprinkles).with(AngularSprinkles::CONSTRUCTOR_DEFINITION)
29
- stub.bind(var)
30
- end
31
27
  end
32
28
 
33
29
  context 'when 2 arguments' do
@@ -39,7 +35,6 @@ describe AngularSprinkles::Helpers::BindHelper do
39
35
  end
40
36
 
41
37
  it 'yields the contructor definition and the first variable to the function to prototype' do
42
- expect(stub).to receive(:yield_to_sprinkles).with(AngularSprinkles::CONSTRUCTOR_DEFINITION)
43
38
  expect(stub).to receive(:yield_to_sprinkles).
44
39
  with("#{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} || {};")
45
40
 
@@ -56,7 +51,6 @@ describe AngularSprinkles::Helpers::BindHelper do
56
51
  end
57
52
 
58
53
  it 'yields the constructor definition and the first variable to the function to prototype' do
59
- expect(stub).to receive(:yield_to_sprinkles).with(AngularSprinkles::CONSTRUCTOR_DEFINITION)
60
54
  expect(stub).to receive(:yield_to_sprinkles).
61
55
  with(%{#{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} || {};
62
56
  #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first(2).join('.')} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first(2).join('.')} || {};})
@@ -70,14 +64,14 @@ describe AngularSprinkles::Helpers::BindHelper do
70
64
 
71
65
  context 'and it is with the same variables' do
72
66
  it 'yields the constructor definition and the first variable to the function to prototype' do
73
- expect(stub).to receive(:content_for).twice
67
+ expect(stub).to receive(:content_for).once
74
68
  5.times { stub.bind(*vars) }
75
69
  end
76
70
  end
77
71
 
78
72
  context 'and it is called with different sets of variables' do
79
73
  it 'yields the constructor definition, first variable, and a chain of the first two variables' do
80
- expect(stub).to receive(:content_for).exactly(3).times
74
+ expect(stub).to receive(:content_for).exactly(2).times
81
75
 
82
76
  stub.bind(vars.first) # yields the constructor
83
77
  stub.bind(vars.first) # yields nothing
@@ -91,25 +85,10 @@ describe AngularSprinkles::Helpers::BindHelper do
91
85
  context 'when the constructor has already been yielded' do
92
86
  let(:var) { :brewhouse }
93
87
 
94
- before { allow(stub).to receive(:app_initialized?).and_return(true) }
95
-
96
88
  it 'does not yield anything with only one argument' do
97
89
  expect(stub).not_to receive(:content_for)
98
90
  stub.bind(var)
99
91
  end
100
92
  end
101
93
  end
102
-
103
- describe '#bindFunc' do
104
- let(:fn) { :brewhouse }
105
- let(:input) { [5, 'software'] }
106
-
107
- it 'returns a javascript function binding string' do
108
- expect(stub.bindFunc(fn, *input)).to eq("#{AngularSprinkles::CONTROLLER_NAME}.#{fn}(#{input.join(',')})")
109
- end
110
-
111
- it 'raises with an input' do
112
- expect { stub.bindFunc }.to raise_error(ArgumentError)
113
- end
114
- end
115
94
  end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe AngularSprinkles::VariableCache do
4
+ let(:cache) { AngularSprinkles::VariableCache.new }
5
+ let(:desired_result) { 'a.b.c' }
6
+
7
+ it 'turns strings into symbolized arrays' do
8
+ var = 'a.b.c'
9
+
10
+ return_value = cache.yield_if_new(var) do |new_var|
11
+ expect(new_var).to eq(desired_result)
12
+ end
13
+
14
+ expect(return_value).to eq(true)
15
+ end
16
+
17
+ it 'flattens nested arrays' do
18
+ var = [:a, ['b', :c]]
19
+
20
+ cache.yield_if_new(var) do |new_var|
21
+ expect(new_var).to eq(desired_result)
22
+ end
23
+ end
24
+
25
+ it 'turns single elements into arrays' do
26
+ var = :a
27
+
28
+ cache.yield_if_new(var) do |new_var|
29
+ expect(new_var).to eq('a')
30
+ end
31
+
32
+ var = 'b'
33
+
34
+ cache.yield_if_new(var) do |new_var|
35
+ expect(new_var).to eq('b')
36
+ end
37
+ end
38
+
39
+ it 'removes nils' do
40
+ var = [:a, nil]
41
+
42
+ cache.yield_if_new(var) do |new_var|
43
+ expect(new_var).to eq('a')
44
+ end
45
+ end
46
+
47
+ it 'prevents the block from running multple times' do
48
+ some_lambda = -> { }
49
+ expect(some_lambda).to receive(:call).twice
50
+
51
+ cache.yield_if_new(:a) { |var| some_lambda.call }
52
+ cache.yield_if_new(:a) { |var| some_lambda.call }
53
+ cache.yield_if_new(:a) { |var| some_lambda.call }
54
+ cache.yield_if_new(:b) { |var| some_lambda.call }
55
+ cache.yield_if_new(:b) { |var| some_lambda.call }
56
+ end
57
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angular_sprinkles
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabe Scholz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-22 00:00:00.000000000 Z
11
+ date: 2014-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -162,18 +162,20 @@ files:
162
162
  - lib/angular_sprinkles/engine.rb
163
163
  - lib/angular_sprinkles/helpers.rb
164
164
  - lib/angular_sprinkles/helpers/bind_helper.rb
165
+ - lib/angular_sprinkles/helpers/bind_service_helper.rb
165
166
  - lib/angular_sprinkles/helpers/directive_helper.rb
166
- - lib/angular_sprinkles/mixins/initializable.rb
167
+ - lib/angular_sprinkles/mixins/cache.rb
167
168
  - lib/angular_sprinkles/mixins/js_transformable.rb
168
169
  - lib/angular_sprinkles/railtie.rb
170
+ - lib/angular_sprinkles/variable_cache.rb
169
171
  - spec/controller_spec.rb
170
172
  - spec/data/bind_spec.rb
171
173
  - spec/decorators/bind_decorator_spec.rb
172
174
  - spec/helpers/bind_helper_spec.rb
173
175
  - spec/helpers/directive_helper_spec.rb
174
- - spec/mixins/initializable_spec.rb
175
176
  - spec/mixins/js_transformable_spec.rb
176
177
  - spec/spec_helper.rb
178
+ - spec/variable_cache_spec.rb
177
179
  homepage: http://github.com/BrewhouseTeam/angular_sprinkles
178
180
  licenses:
179
181
  - MIT
@@ -1,20 +0,0 @@
1
- module AngularSprinkles
2
- module Mixins
3
- module Initializable
4
- def app_initialized?
5
- @_sprinkles_app_initialized or (@_sprinkles_app_initialized = true) && false
6
- end
7
-
8
- def var_initialized?(variable)
9
- variable = [variable].flatten
10
- initialized_variables[variable] or (initialized_variables[variable] = true) && false
11
- end
12
-
13
- private
14
-
15
- def initialized_variables
16
- @_sprinkles_initialized_variables ||= {}
17
- end
18
- end
19
- end
20
- end
@@ -1,31 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe AngularSprinkles::Mixins::Initializable do
4
- class StubClass
5
- include AngularSprinkles::Mixins::Initializable
6
- end
7
-
8
- let(:stub_class) { StubClass.new }
9
-
10
- describe '#app_initialized?' do
11
- it 'returns false the first time and true after that' do
12
- expect(stub_class.app_initialized?).to eq(false)
13
- expect(stub_class.app_initialized?).to eq(true)
14
- expect(stub_class.app_initialized?).to eq(true)
15
- end
16
- end
17
-
18
- describe '#var_initialized?' do
19
- it 'returns false the first time and true after that' do
20
- var = 'a.b'
21
-
22
- expect(stub_class.var_initialized?(var)).to eq(false)
23
- expect(stub_class.var_initialized?(var)).to eq(true)
24
- expect(stub_class.var_initialized?(var)).to eq(true)
25
-
26
- expect(stub_class.var_initialized?(var + '.c')).to eq(false)
27
- expect(stub_class.var_initialized?(var + '.c')).to eq(true)
28
- end
29
- end
30
-
31
- end