measured-rails 0.0.1

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: 95f2b762444dc351bf41886d19054fe2679893ae
4
+ data.tar.gz: 233c3ddf282c3b5a54be7df01ed11c9220682a6d
5
+ SHA512:
6
+ metadata.gz: 4eed05964aa33c9319aa450b20a44976736e7be1db767a55a18aa1787484a78a6f8bf85514668047efa08fe4d585125029f6c175aff4ba6db2e07ff1f0bee6dc
7
+ data.tar.gz: 4f757002ed6692ab97feefd2d3b19be9093e6f74dfdfc470736b6d2323500a114cafc0c63734d28e4461cecc9a0f79d1835b82a6fba17b14adf14eae1e1178dc
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.log
16
+ *.sqlite3
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ install:
5
+ - gem uninstall bundler
6
+ - gem install bundler --version '1.8.4'
7
+ - bundle install
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in measured-rails.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kevin McPhillips
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # Measured Rails [![Build Status](https://travis-ci.org/Shopify/measured-rails.svg)](https://travis-ci.org/Shopify/measured-rails)
2
+
3
+ This gem is the Rails integration for the [measured](https://github.com/Shopify/measured) gem.
4
+
5
+ It provides ActiveRecord adapter for persisting and retrieving measurements with their units, and model validations.
6
+
7
+ ## Installation
8
+
9
+ Using bundler, add to the Gemfile:
10
+
11
+ ```ruby
12
+ gem 'measured-rails'
13
+ ```
14
+
15
+ Or stand alone:
16
+
17
+ $ gem install measured-rails
18
+
19
+ ## Usage
20
+
21
+ ### ActiveRecord
22
+
23
+ Columns are expected to have the `_value` and `_unit` suffix, and be `DECIMAL` and `VARCHAR`, and defaults are accepted:
24
+
25
+ ```ruby
26
+ class AddWeightAndLengthToThings < ActiveRecord::Migration
27
+ def change
28
+ add_column :things, :minimum_weight_value, :decimal, precision: 10, scale: 2
29
+ add_column :things, :minimum_weight_unit, :string, limit: 12
30
+
31
+ add_column :things, :total_length_value, :decimal, precision: 10, scale: 2, default: 0
32
+ add_column :things, :total_length_unit, :string, limit: 12, default: "cm"
33
+ end
34
+ end
35
+ ```
36
+
37
+ A column can be declared as a measurement with its measurement subclass:
38
+
39
+ ```ruby
40
+ class Thing < ActiveRecord::Base
41
+ measured Measured::Weight, :minimum_weight
42
+ measured Measured::Length, :total_length
43
+ end
44
+ ```
45
+
46
+ There are some simpler methods for predefined types:
47
+
48
+ ```ruby
49
+ class Thing < ActiveRecord::Base
50
+ measured_weight :minimum_weight
51
+ measured_length :total_length
52
+ end
53
+ ```
54
+
55
+ This will allow you to access and assign a measurement object:
56
+
57
+ ```ruby
58
+ thing = Thing.new
59
+ thing.minimum_weight = Measured::Weight.new(10, "g")
60
+ thing.minimum_weight_unit # "g"
61
+ thing.minimum_weight_value # 10
62
+ ```
63
+
64
+ Order of assignment does not matter, and each property can be assigned separately and with mass assignment:
65
+
66
+ ```ruby
67
+ params = { total_length_unit: "cm", total_length_value: "3" }
68
+ thing = Thing.new(params)
69
+ thing.total_length # #<Measured::Length: 3 cm>
70
+ ```
71
+
72
+ ### Validations
73
+
74
+ Validations are available:
75
+
76
+ ```ruby
77
+ class Thing < ActiveRecord::Base
78
+ measured_length :total_length
79
+
80
+ validates :total_length, measured: true
81
+ end
82
+ ```
83
+
84
+ This will validate that the unit is defined on the measurement, and that there is a value.
85
+
86
+ Rather than `true` the validation can accept a hash with the following options:
87
+
88
+ * `message`: Override the default "is invalid" message.
89
+ * `units`: A subset of units available for this measurement. Units must be in existing measurement.
90
+
91
+ Validations can be combined with `presence` validator.
92
+
93
+ **Note:** Validations are strongly recommended since assigning an invalid unit will cause the measurement to return `nil`, even if there is a value:
94
+
95
+ ```ruby
96
+ thing = Thing.new
97
+ thing.total_length_value = 1
98
+ thing.total_length_unit = "invalid"
99
+ thing.total_length # nil
100
+
101
+ ```
102
+
103
+
104
+ ## Tests
105
+
106
+ ```
107
+ $ bundle exec rake test
108
+ ```
109
+
110
+ ## Contributing
111
+
112
+ 1. Fork it ( https://github.com/Shopify/measured-rails/fork )
113
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
114
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
115
+ 4. Push to the branch (`git push origin my-new-feature`)
116
+ 5. Create a new Pull Request
117
+
118
+ ## Authors
119
+
120
+ * [Kevin McPhillips](https://github.com/kmcphillips) at [Shopify](http://shopify.com/careers)
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
5
+ require "measured/rails/version"
6
+
7
+ task default: :test
8
+
9
+ desc 'Run the test stuite'
10
+ Rake::TestTask.new do |t|
11
+ t.libs << "test"
12
+ t.libs << "lib/**/*"
13
+ t.test_files = FileList['test/**/*_test.rb']
14
+ t.verbose = true
15
+ end
16
+
17
+ task tag: :build do
18
+ system "git commit -m'Released version #{ Measured::VERSION }' --allow-empty"
19
+ system "git tag -a v#{ Measured::VERSION } -m 'Tagging #{ Measured::VERSION }'"
20
+ system "git push --tags"
21
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "measured/rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ require "measured/rails/base"
2
+
3
+ require "measured/rails/units/length"
4
+ require "measured/rails/units/weight"
@@ -0,0 +1,71 @@
1
+ module Measured::Rails::ActiveRecord
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def measured(measured_class, *fields)
6
+ options = fields.extract_options!
7
+ options = {}.merge(options)
8
+
9
+ measured_class = measured_class.constantize if measured_class.is_a?(String)
10
+
11
+ raise Measured::Rails::Error, "Expecting #{ measured_class } to be a subclass of Measured::Measurable" if !measured_class.is_a?(Class) || !measured_class.ancestors.include?(Measured::Measurable)
12
+
13
+ options[:class] = measured_class
14
+
15
+ fields.map(&:to_sym).each do |field|
16
+ raise Measured::Rails::Error, "The field #{ field } has already been measured" if measured_fields.keys.include?(field)
17
+ measured_fields[field] = options
18
+
19
+ # Reader to retrieve measured object
20
+ define_method(field) do
21
+ value = public_send("#{ field }_value")
22
+ unit = public_send("#{ field }_unit")
23
+
24
+ return nil unless value && unit
25
+
26
+ instance = instance_variable_get("@measured_#{ field }")
27
+ new_instance = begin
28
+ measured_class.new(value, unit)
29
+ rescue Measured::UnitError
30
+ nil
31
+ end
32
+
33
+ if instance && instance == new_instance
34
+ instance
35
+ else
36
+ instance_variable_set("@measured_#{ field }", new_instance)
37
+ end
38
+ end
39
+
40
+ # Writer to assign measured object
41
+ define_method("#{ field }=") do |incoming|
42
+ if incoming.is_a?(measured_class)
43
+ instance_variable_set("@measured_#{ field }", incoming)
44
+ public_send("#{ field }_value=", incoming.value)
45
+ public_send("#{ field }_unit=", incoming.unit)
46
+ else
47
+ instance_variable_set("@measured_#{ field }", nil)
48
+ public_send("#{ field }_value=", nil)
49
+ public_send("#{ field }_unit=", nil)
50
+ end
51
+ end
52
+
53
+ # Writer to override unit assignment
54
+ define_method("#{ field }_unit=") do |incoming|
55
+ incoming = measured_class.conversion.to_unit_name(incoming) if measured_class.valid_unit?(incoming)
56
+ write_attribute("#{ field }_unit", incoming)
57
+ end
58
+
59
+ end
60
+ end
61
+
62
+ def measured_fields
63
+ @measured_fields ||= {}
64
+ end
65
+
66
+ end
67
+ end
68
+
69
+ ActiveSupport.on_load(:active_record) do
70
+ ::ActiveRecord::Base.send :include, Measured::Rails::ActiveRecord
71
+ end
@@ -0,0 +1,17 @@
1
+ require "measured/rails/version"
2
+ require "measured"
3
+
4
+ require "active_support"
5
+
6
+ module Measured
7
+ module Rails
8
+ class Error < StandardError ; end
9
+ end
10
+ end
11
+
12
+ require "measured/rails/active_record"
13
+ require "measured/rails/validations"
14
+
15
+ if defined? Rails
16
+ require "measured/rails/railtie"
17
+ end
@@ -0,0 +1,4 @@
1
+ module Measured::Rails
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module Measured::Rails::ActiveRecord::Length
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def measured_length(*fields)
6
+ measured(Measured::Length, *fields)
7
+ end
8
+ end
9
+ end
10
+
11
+ ActiveSupport.on_load(:active_record) do
12
+ ::ActiveRecord::Base.send :include, Measured::Rails::ActiveRecord::Length
13
+ end
@@ -0,0 +1,13 @@
1
+ module Measured::Rails::ActiveRecord::Weight
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def measured_weight(*fields)
6
+ measured(Measured::Weight, *fields)
7
+ end
8
+ end
9
+ end
10
+
11
+ ActiveSupport.on_load(:active_record) do
12
+ ::ActiveRecord::Base.send :include, Measured::Rails::ActiveRecord::Weight
13
+ end
@@ -0,0 +1,18 @@
1
+ class MeasuredValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, measurable)
3
+ measured_config = record.class.measured_fields[attribute]
4
+
5
+ measured_class = measured_config[:class]
6
+ measurable_unit = record.send("#{ attribute }_unit")
7
+ measurable_value = record.send("#{ attribute }_value")
8
+
9
+ message = options[:message] || "is not a valid unit"
10
+
11
+ if options[:units]
12
+ valid_units = [options[:units]].flatten.map{|u| measured_class.conversion.to_unit_name(u) }
13
+ record.errors.add(attribute, message) unless valid_units.include?(measured_class.conversion.to_unit_name(measurable_unit))
14
+ end
15
+
16
+ record.errors.add(attribute, message) unless measured_class.valid_unit?(measurable_unit)
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module Measured
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'measured/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "measured-rails"
8
+ spec.version = Measured::Rails::VERSION
9
+ spec.authors = ["Kevin McPhillips"]
10
+ spec.email = ["github@kevinmcphillips.ca"]
11
+ spec.summary = %q{Rails adaptor for measured}
12
+ spec.description = %q{Rails adapter for assigning and managing measurements with their units provided by the measured gem.}
13
+ spec.homepage = "https://github.com/Shopify/measured-rails"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "rails", ">= 4.0"
22
+ spec.add_runtime_dependency "measured", "0.0.4"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.8"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.5.1"
27
+ spec.add_development_dependency "mocha", "~> 1.1.0"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "sqlite3"
30
+ end
data/repodb.yml ADDED
@@ -0,0 +1,3 @@
1
+ classification: library
2
+ ci_urls:
3
+ - https://travis-ci.org/Shopify/measured-rails
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: measured-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin McPhillips
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: measured
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 5.5.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 5.5.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.1.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.1.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
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
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Rails adapter for assigning and managing measurements with their units
126
+ provided by the measured gem.
127
+ email:
128
+ - github@kevinmcphillips.ca
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - LICENSE
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/setup
141
+ - lib/measured-rails.rb
142
+ - lib/measured/rails/active_record.rb
143
+ - lib/measured/rails/base.rb
144
+ - lib/measured/rails/railtie.rb
145
+ - lib/measured/rails/units/length.rb
146
+ - lib/measured/rails/units/weight.rb
147
+ - lib/measured/rails/validations.rb
148
+ - lib/measured/rails/version.rb
149
+ - measured-rails.gemspec
150
+ - repodb.yml
151
+ homepage: https://github.com/Shopify/measured-rails
152
+ licenses:
153
+ - MIT
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 2.2.2
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Rails adaptor for measured
175
+ test_files: []