backbone-computedfields-rails 0.0.7

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: 43cc9947b1c3511c55f9b9875661a0a848c9d0cc
4
+ data.tar.gz: 0a984cc2bc471d67ad3fdc7aa5034786c1239a21
5
+ SHA512:
6
+ metadata.gz: 87215b62f605ab2d32ae9295613495afc7712eefc1311d189cf5a896961f723bf86d1b0714827e473b10bdf09428acffdb5b229b9648ca33bd3d671d51995016
7
+ data.tar.gz: a2fbf63c6c283d200106c12eef0f71c3daf2acca4c7196010d49028a46721c83b4b1c4bba00e854ac57353c9cf84a773f6e5f3e27b9d27b052332dce0e04e3fa
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in backbone-computedfields-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Stafford Brunk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # backbone-computedfields-rails [![Gem Version](https://badge.fury.io/rb/backbone-computedfields-rails.png)](http://badge.fury.io/rb/backbone-computedfields-rails)
2
+
3
+ This gem vendors the [backbone-computedfields](https://github.com/alexanderbeletsky/backbone-computedfields) library for easy usage in the Rails asset pipeline.
4
+
5
+ ## Dependencies
6
+
7
+ ```backbone-computedfields``` requires Backbone.js >= 0.1.0 and Underscore.js >= 1.5.0
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'backbone-computedfields-rails'
14
+
15
+ Then require it in your javascript manifest file (```application.js``` by default)
16
+
17
+ //= require backbone-computedfields
18
+
19
+ ## Versioning
20
+
21
+ ```backbone-computedfields-rails``` will track the version of ```backbone-computedfields```.
22
+
23
+ ## Issues
24
+ For issues with ```backbone-computedfields```, please file an issue over at their [issue tracker](https://github.com/alexanderbeletsky/backbone-computedfields/issues). Otherwise, please file an issue here.
25
+
26
+
27
+ [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/wingrunr21/backbone-computedfields-rails/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require "bundler/gem_tasks"
2
+ require 'open-uri'
3
+ require 'json'
4
+
5
+
6
+ # Helper Functions
7
+ def name
8
+ @name ||= Dir['*.gemspec'].first.split('.').first
9
+ end
10
+
11
+ def version
12
+ line = File.read("lib/#{name}/version.rb")[/^\s*VERSION\s*=\s*.*/]
13
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
14
+ end
15
+
16
+ def latest_tag
17
+ tags = JSON.parse(open('https://api.github.com/repos/alexanderbeletsky/backbone-computedfields/tags').read)
18
+ tags.sort!{|a,b| b["name"] <=> a["name"]}
19
+ tags.first
20
+ end
21
+
22
+ namespace :backbone_computedfields do
23
+
24
+ desc "Fetches and displays the latest backbone-computedfields tag"
25
+ task :latest do
26
+ tag = latest_tag
27
+ puts "The latest backbone-computedfields tag is #{tag["name"]} with commit #{tag["commit"]["sha"]}"
28
+ end
29
+
30
+ desc "Updates the vendored backbone-computedfields version to the latest tag"
31
+ task :update do
32
+ tag = latest_tag
33
+
34
+ # Pull attributes we need
35
+ name = tag["name"]
36
+ sha = tag["commit"]["sha"]
37
+
38
+ # Cleanup the name
39
+ name.gsub!(/^v/, '')
40
+ name.gsub!(/-/, '.')
41
+
42
+ if name == version
43
+ puts "Gem version #{version} matches the latest backbone-computedfields version #{name}"
44
+ exit
45
+ end
46
+
47
+ # Update backbone-computedfields
48
+ puts "Updating backbone-computedfields..."
49
+ base_url = "https://raw.github.com/alexanderbeletsky/backbone-computedfields/#{sha}/lib"
50
+ files = %w{backbone.computedfields.js backbone.computedfields.min.js}
51
+ Dir.chdir './vendor/assets/javascripts' do
52
+ files.each {|file| `curl -O #{base_url}/#{file}`}
53
+ end
54
+
55
+ # Update version file
56
+ puts "Updating version.rb..."
57
+ `sed -i "" "s/ VERSION = '.*'/ VERSION = '#{name}'/g" lib/backbone-computedfields-rails/version.rb`
58
+ end
59
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'backbone-computedfields-rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "backbone-computedfields-rails"
8
+ spec.version = Backbone::ComputedFields::Rails::VERSION
9
+ spec.authors = ["Stafford Brunk"]
10
+ spec.email = ["stafford.brunk@gmail.com"]
11
+ spec.description = %q{Computed fields for Backbone.Models, polished for real project needs}
12
+ spec.summary = %q{Packages the backbone.computedfields library for use with the asset pipline}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,10 @@
1
+ require "backbone-computedfields-rails/version"
2
+
3
+ module Backbone
4
+ module ComputedFields
5
+ module Rails
6
+ class Engine < ::Rails::Engine
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Backbone
2
+ module ComputedFields
3
+ module Rails
4
+ VERSION = '0.0.7'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,137 @@
1
+ // Backbone.ComputedFields, v0.0.7
2
+ // Copyright (c)2013 alexander.beletsky@gmail.com
3
+ // Distributed under MIT license
4
+ // https://github.com/alexanderbeletsky/backbone.computedfields
5
+ Backbone.ComputedFields = (function(Backbone, _){
6
+
7
+ var ComputedFields = function (model) {
8
+ this.model = model;
9
+ this._computedFields = [];
10
+
11
+ this.initialize();
12
+ };
13
+
14
+ ComputedFields.VERSION = '0.0.6';
15
+
16
+ _.extend(ComputedFields.prototype, {
17
+ initialize: function () {
18
+ _.bindAll(
19
+ this,
20
+ '_bindModelEvents',
21
+ '_computeFieldValue',
22
+ '_dependentFields',
23
+ '_isModelInitialized',
24
+ '_lookUpComputedFields',
25
+ '_thenComputedChanges',
26
+ '_thenDependentChanges',
27
+ '_toJSON',
28
+ '_wrapJSON',
29
+ 'initialize'
30
+ );
31
+
32
+ this._lookUpComputedFields();
33
+ this._bindModelEvents();
34
+ this._wrapJSON();
35
+ },
36
+
37
+ _lookUpComputedFields: function () {
38
+ for (var obj in this.model.computed) {
39
+ var field = this.model.computed[obj];
40
+
41
+ if (field && (field.set || field.get)) {
42
+ this._computedFields.push({name: obj, field: field});
43
+ }
44
+ }
45
+ },
46
+
47
+ _bindModelEvents: function () {
48
+ _.each(this._computedFields, function (computedField) {
49
+ var fieldName = computedField.name;
50
+ var field = computedField.field;
51
+
52
+ var updateComputed = _.bind(function () {
53
+ var value = this._computeFieldValue(field);
54
+ this.model.set(fieldName, value, { skipChangeEvent: true });
55
+ }, this);
56
+
57
+ var updateDependent = _.bind(function (model, value, options) {
58
+ if (options && options.skipChangeEvent) {
59
+ return;
60
+ }
61
+
62
+ if (field.set) {
63
+ var fields = this._dependentFields(field.depends);
64
+ value = value || this.model.get(fieldName);
65
+
66
+ field.set.call(this.model, value, fields);
67
+ this.model.set(fields, options);
68
+ }
69
+ }, this);
70
+
71
+ this._thenDependentChanges(field.depends, updateComputed);
72
+ this._thenComputedChanges(fieldName, updateDependent);
73
+
74
+ if (this._isModelInitialized()) {
75
+ updateComputed();
76
+ }
77
+ }, this);
78
+ },
79
+
80
+ _isModelInitialized: function () {
81
+ return !_.isEmpty(this.model.attributes);
82
+ },
83
+
84
+ _thenDependentChanges: function (depends, callback) {
85
+ _.each(depends, function (name) {
86
+ if (typeof (name) === 'string') {
87
+ this.model.on('change:' + name, callback);
88
+ }
89
+
90
+ if (typeof (name) === 'function') {
91
+ name.call(this.model, callback);
92
+ }
93
+ }, this);
94
+ },
95
+
96
+ _thenComputedChanges: function (fieldName, callback) {
97
+ this.model.on('change:' + fieldName, callback);
98
+ },
99
+
100
+ _wrapJSON: function () {
101
+ this.model.toJSON = _.wrap(this.model.toJSON, this._toJSON);
102
+ },
103
+
104
+ _toJSON: function (toJSON) {
105
+ var args = Array.prototype.slice.call(arguments, 1),
106
+ attributes = toJSON.apply(this.model, args),
107
+ strip = !!(args[0] || {}).computedFields;
108
+
109
+ var stripped = strip ? {} : _.reduce(this._computedFields, function (memo, computed) {
110
+ if (computed.field.toJSON === false) {
111
+ memo.push(computed.name);
112
+ }
113
+ return memo;
114
+ },[]);
115
+
116
+ return _.omit(attributes, stripped);
117
+ },
118
+
119
+ _computeFieldValue: function (computedField) {
120
+ if (computedField && computedField.get) {
121
+ var fields = this._dependentFields(computedField.depends);
122
+ return computedField.get.call(this.model, fields);
123
+ }
124
+ },
125
+
126
+ _dependentFields: function (depends) {
127
+ return _.reduce(depends, function (memo, field) {
128
+ memo[field] = this.model.get(field);
129
+ return memo;
130
+ }, {}, this);
131
+ }
132
+
133
+ });
134
+
135
+ return ComputedFields;
136
+
137
+ })(Backbone, _);
@@ -0,0 +1 @@
1
+ Backbone.ComputedFields=function(a,b){var c=function(a){this.model=a,this._computedFields=[],this.initialize()};return c.VERSION="0.0.6",b.extend(c.prototype,{initialize:function(){b.bindAll(this,"_bindModelEvents","_computeFieldValue","_dependentFields","_isModelInitialized","_lookUpComputedFields","_thenComputedChanges","_thenDependentChanges","_toJSON","_wrapJSON","initialize"),this._lookUpComputedFields(),this._bindModelEvents(),this._wrapJSON()},_lookUpComputedFields:function(){for(var a in this.model.computed){var b=this.model.computed[a];b&&(b.set||b.get)&&this._computedFields.push({name:a,field:b})}},_bindModelEvents:function(){b.each(this._computedFields,function(a){var c=a.name,d=a.field,e=b.bind(function(){var a=this._computeFieldValue(d);this.model.set(c,a,{skipChangeEvent:!0})},this),f=b.bind(function(a,b,e){if((!e||!e.skipChangeEvent)&&d.set){var f=this._dependentFields(d.depends);b=b||this.model.get(c),d.set.call(this.model,b,f),this.model.set(f,e)}},this);this._thenDependentChanges(d.depends,e),this._thenComputedChanges(c,f),this._isModelInitialized()&&e()},this)},_isModelInitialized:function(){return!b.isEmpty(this.model.attributes)},_thenDependentChanges:function(a,c){b.each(a,function(a){"string"==typeof a&&this.model.on("change:"+a,c),"function"==typeof a&&a.call(this.model,c)},this)},_thenComputedChanges:function(a,b){this.model.on("change:"+a,b)},_wrapJSON:function(){this.model.toJSON=b.wrap(this.model.toJSON,this._toJSON)},_toJSON:function(a){var c=Array.prototype.slice.call(arguments,1),d=a.apply(this.model,c),e=!!(c[0]||{}).computedFields,f=e?{}:b.reduce(this._computedFields,function(a,b){return b.field.toJSON===!1&&a.push(b.name),a},[]);return b.omit(d,f)},_computeFieldValue:function(a){if(a&&a.get){var b=this._dependentFields(a.depends);return a.get.call(this.model,b)}},_dependentFields:function(a){return b.reduce(a,function(a,b){return a[b]=this.model.get(b),a},{},this)}}),c}(Backbone,_);
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backbone-computedfields-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Stafford Brunk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Computed fields for Backbone.Models, polished for real project needs
42
+ email:
43
+ - stafford.brunk@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - backbone-computedfields-rails.gemspec
54
+ - lib/backbone-computedfields-rails.rb
55
+ - lib/backbone-computedfields-rails/version.rb
56
+ - vendor/assets/javascripts/backbone.computedfields.js
57
+ - vendor/assets/javascripts/backbone.computedfields.min.js
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.14
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Packages the backbone.computedfields library for use with the asset pipline
82
+ test_files: []