angular_sprinkles 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1aa73b9e39b4a5a18d3c3b14d39bce928a4c78d6
4
- data.tar.gz: d53aee79dc7d3950758677702a1ee564883a3611
3
+ metadata.gz: 82a8542cbe3370f7c7a63a7e6b535827297d427d
4
+ data.tar.gz: 7d47544aeb6a441e35705226756741c849176d12
5
5
  SHA512:
6
- metadata.gz: c7b5ffa915e647ec7bfc49980e85b3bd93eebd7a349471b3cdbcb176f74422f8a27072dcc7f14007d2f6ccbf2db7237a3212eb409ae1662adfa6ebc5bc502e26
7
- data.tar.gz: ba0706e68ba26006a90394b297da96f80b05ff0ea30850200d3cd2c7600e11ed1395d068acfa4dd51c967993a8128d873012bffb9b075e284bfcb6643e5ede2b
6
+ metadata.gz: 98f6621a65033d9271a6ee611a7f8abaa046686f473c3b5af5cb238ca467247ad53a7569d4113f18e6fda93a79b25c1027d089b08d1407b9e918272bd7a79b22
7
+ data.tar.gz: 796d155b10422c2c6050dbf526afc110a6a0a1ff4319c58345909915305df22cad10c14dd11e7027ad192907bc085f6b7bcca55de01846fe0528c106661dc619
data/README.md CHANGED
@@ -91,6 +91,20 @@ Last Name: <input type="text" ng-model="<%= @user.bind(:last_name) %>" />
91
91
 
92
92
  We've got two-way data binding of persisted data without writing a single line of javascript!
93
93
 
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
+
94
108
  ## Custom Directives
95
109
 
96
110
  Sprinkles also comes packaged with a helper for instantiating custom directives.
@@ -139,6 +153,27 @@ sprinkles.directive('blink', function ($interval) {
139
153
  <input type="text" ng-model="<%= bind(:blink_text) %>" />
140
154
  ```
141
155
 
156
+ ## Custom Functions
157
+
158
+ Custom functions can be added to your application with the `sprinkles.func` function
159
+
160
+ ```js
161
+ // some js file
162
+
163
+ sprinkles.func('alertMe', function (input) {
164
+ alert(input);
165
+ });
166
+ ```
167
+
168
+ These can then be called with the `bindFunc` helper
169
+
170
+ ```erb
171
+ <%=# some view %>
172
+
173
+ <%= bindFunc(:alertMe, @user.bind(:first_name));
174
+ ```
175
+
176
+
142
177
  ## Including additional modules
143
178
 
144
179
  Adding additional modules is just a matter of appending them to `sprinkles.requires`. This is already a feature of Angular and not Sprinkles
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -2,11 +2,11 @@
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.2 ruby lib
5
+ # stub: angular_sprinkles 0.0.3 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "angular_sprinkles"
9
- s.version = "0.0.2"
9
+ s.version = "0.0.3"
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"]
@@ -1,9 +1,18 @@
1
1
  //= require angular
2
2
 
3
+ (function (window, document, angular) {
4
+
5
+ var fnQueue = [];
6
+
3
7
  window.sprinkles = angular.module('<%= AngularSprinkles::APP_NAME %>', []);
4
8
 
9
+ window.sprinkles.func = function () {
10
+ var args = [].slice.call(arguments, 0);
11
+ fnQueue.push({name: args[0], fn: args[1]});
12
+ };
13
+
5
14
  window.onload = function () {
6
- var angular = window.angular,
15
+ var queue,
7
16
  app = angular.module('<%= AngularSprinkles::APP_NAME %>'),
8
17
  doc = document.documentElement,
9
18
  ctrlName = (doc.attributes['ng-controller'] || doc.attributes['data-ng-controller']) || 'Ctrl';
@@ -11,8 +20,16 @@ window.onload = function () {
11
20
  ctrlName = ctrlName.split(' as ')[0];
12
21
 
13
22
  <%= AngularSprinkles::CONTROLLER_FN %> = <%= AngularSprinkles::CONTROLLER_FN %> || function(){};
23
+
24
+ for (var i = 0; i < fnQueue.length; i++) {
25
+ queue = fnQueue[i];
26
+ <%= AngularSprinkles::CONTROLLER_FN %>.prototype[queue.name] = queue.fn;
27
+ }
28
+
14
29
  app.controller(ctrlName, <%= AngularSprinkles::CONTROLLER_FN %>);
15
30
  doc.setAttribute('data-ng-controller', ctrlName + ' as <%= AngularSprinkles::CONTROLLER_NAME %>');
16
31
 
17
32
  angular.bootstrap(doc, [app.name]);
18
33
  };
34
+
35
+ }(window, document, window.angular));
@@ -25,6 +25,11 @@ module AngularSprinkles
25
25
  AngularSprinkles::Data::Bind.new(*input)
26
26
  end
27
27
 
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
+
28
33
  private
29
34
 
30
35
  def build_chain(input)
@@ -7,95 +7,109 @@ describe AngularSprinkles::Helpers::BindHelper do
7
7
 
8
8
  let(:stub) { StubClass.new }
9
9
 
10
- before { allow(stub).to receive(:content_for).and_return(true) }
10
+ describe '#bind' do
11
+ before { allow(stub).to receive(:content_for).and_return(true) }
11
12
 
12
- context 'when 0 arguments' do
13
- it 'raises an argument error' do
14
- expect { stub.bind }.to raise_error(ArgumentError)
13
+ context 'when 0 arguments' do
14
+ it 'raises an argument error' do
15
+ expect { stub.bind }.to raise_error(ArgumentError)
16
+ end
15
17
  end
16
- end
17
18
 
18
- context 'when 1 argument' do
19
- let(:var) { :brewhouse }
19
+ context 'when 1 argument' do
20
+ let(:var) { :brewhouse }
20
21
 
21
- it 'returns the variable name given as a js string' do
22
- expect(stub.bind(var).to_json).
23
- to(eq("#{AngularSprinkles::CONTROLLER_NAME}.#{var}"))
24
- end
22
+ it 'returns the variable name given as a js string' do
23
+ expect(stub.bind(var).to_json).
24
+ to(eq("#{AngularSprinkles::CONTROLLER_NAME}.#{var}"))
25
+ end
25
26
 
26
- it 'only yields the constructor definition to the view' do
27
- expect(stub).to receive(:yield_to_sprinkles).with(AngularSprinkles::CONSTRUCTOR_DEFINITION)
28
- stub.bind(var)
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
29
31
  end
30
- end
31
32
 
32
- context 'when 2 arguments' do
33
- let(:vars) { [:brewhouse, :software] }
33
+ context 'when 2 arguments' do
34
+ let(:vars) { [:brewhouse, :software] }
34
35
 
35
- it 'returns the variables chained together as a js string' do
36
- expect(stub.bind(*vars).to_json).
37
- to(eq("#{AngularSprinkles::CONTROLLER_NAME}.#{vars.join('.')}"))
38
- end
36
+ it 'returns the variables chained together as a js string' do
37
+ expect(stub.bind(*vars).to_json).
38
+ to(eq("#{AngularSprinkles::CONTROLLER_NAME}.#{vars.join('.')}"))
39
+ end
39
40
 
40
- it 'yields the contructor definition and the first variable to the function to prototype' do
41
- expect(stub).to receive(:yield_to_sprinkles).with(AngularSprinkles::CONSTRUCTOR_DEFINITION)
42
- expect(stub).to receive(:yield_to_sprinkles).
43
- with("#{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} || {};")
41
+ 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
+ expect(stub).to receive(:yield_to_sprinkles).
44
+ with("#{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} || {};")
44
45
 
45
- stub.bind(*vars)
46
+ stub.bind(*vars)
47
+ end
46
48
  end
47
- end
48
49
 
49
- context 'when 3 arguments' do
50
- let(:vars) { [:brewhouse, :software, :is_neat] }
50
+ context 'when 3 arguments' do
51
+ let(:vars) { [:brewhouse, :software, :is_neat] }
51
52
 
52
- it 'returns the variables chained together as a js string' do
53
- expect(stub.bind(*vars).to_json).
54
- to(eq("#{AngularSprinkles::CONTROLLER_NAME}.#{vars.join('.')}"))
55
- end
53
+ it 'returns the variables chained together as a js string' do
54
+ expect(stub.bind(*vars).to_json).
55
+ to(eq("#{AngularSprinkles::CONTROLLER_NAME}.#{vars.join('.')}"))
56
+ end
56
57
 
57
- it 'yields the constructor definition and the first variable to the function to prototype' do
58
- expect(stub).to receive(:yield_to_sprinkles).with(AngularSprinkles::CONSTRUCTOR_DEFINITION)
59
- expect(stub).to receive(:yield_to_sprinkles).
60
- with(%{#{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} || {};
58
+ 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
+ expect(stub).to receive(:yield_to_sprinkles).
61
+ with(%{#{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} || {};
61
62
  #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first(2).join('.')} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first(2).join('.')} || {};})
62
63
 
63
- stub.bind(*vars)
64
+ stub.bind(*vars)
65
+ end
64
66
  end
65
- end
66
67
 
67
- context 'when bind is called more than once' do
68
- let(:vars) { [:brewhouse, :software, :is_neat] }
68
+ context 'when bind is called more than once' do
69
+ let(:vars) { [:brewhouse, :software, :is_neat] }
69
70
 
70
- context 'and it is with the same variables' do
71
- it 'yields the constructor definition and the first variable to the function to prototype' do
72
- expect(stub).to receive(:content_for).twice
73
- 5.times { stub.bind(*vars) }
71
+ context 'and it is with the same variables' do
72
+ it 'yields the constructor definition and the first variable to the function to prototype' do
73
+ expect(stub).to receive(:content_for).twice
74
+ 5.times { stub.bind(*vars) }
75
+ end
76
+ end
77
+
78
+ context 'and it is called with different sets of variables' do
79
+ 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
81
+
82
+ stub.bind(vars.first) # yields the constructor
83
+ stub.bind(vars.first) # yields nothing
84
+ stub.bind(*vars.first(2)) # yields declaration for :brewhouse
85
+ stub.bind(*vars) # yields declaration for [:brewhouse, :software]
86
+ stub.bind(*vars) # yields nothing
87
+ end
74
88
  end
75
89
  end
76
90
 
77
- context 'and it is called with different sets of variables' do
78
- it 'yields the constructor definition, first variable, and a chain of the first two variables' do
79
- expect(stub).to receive(:content_for).exactly(3).times
91
+ context 'when the constructor has already been yielded' do
92
+ let(:var) { :brewhouse }
93
+
94
+ before { allow(stub).to receive(:app_initialized?).and_return(true) }
80
95
 
81
- stub.bind(vars.first) # yields the constructor
82
- stub.bind(vars.first) # yields nothing
83
- stub.bind(*vars.first(2)) # yields declaration for :brewhouse
84
- stub.bind(*vars) # yields declaration for [:brewhouse, :software]
85
- stub.bind(*vars) # yields nothing
96
+ it 'does not yield anything with only one argument' do
97
+ expect(stub).not_to receive(:content_for)
98
+ stub.bind(var)
86
99
  end
87
100
  end
88
101
  end
89
102
 
90
- context 'when the constructor has already been yielded' do
91
- let(:var) { :brewhouse }
103
+ describe '#bindFunc' do
104
+ let(:fn) { :brewhouse }
105
+ let(:input) { [5, 'software'] }
92
106
 
93
- before { allow(stub).to receive(:app_initialized?).and_return(true) }
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
94
110
 
95
- it 'does not yield anything with only one argument' do
96
- expect(stub).not_to receive(:content_for)
97
- stub.bind(var)
111
+ it 'raises with an input' do
112
+ expect { stub.bindFunc }.to raise_error(ArgumentError)
98
113
  end
99
114
  end
100
-
101
115
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angular_sprinkles
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabe Scholz