dynamometer 0.0.7 → 0.0.8

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: 8381df54a2f01ec5e7a6fc5116c183830a40adc8
4
- data.tar.gz: 5ca5d4ff2ec9f94eb84cc404b82d29793c3efdf5
3
+ metadata.gz: 921d2fe3bb72e407fb99012ded3142f80d25ba28
4
+ data.tar.gz: 7419a085efb613ef2a5f78b22bdd450afb9f97d5
5
5
  SHA512:
6
- metadata.gz: a2bb08080e830c9cf61b00b774816195741e31be5393619bf2af076118fbb71e3504b17b381ee23589ee39ceb58c0a3c836ce0c701b54c5cb6d0ae02dcde6d27
7
- data.tar.gz: 3d8fae53ed967b10910ad15cfe0a8ea70e4095a3c41a5d8e742c789fecd0f2270e36e6e3e9759e46d5959112a94404216b1bfbc420095ff2317697841faddf75
6
+ metadata.gz: 988d4cef8655ad8c5524eb646fe4f77f9e9b3fea24250bc48d6fb00966364f7114b498b056f0e3be63486ece12a7e3b7edc22e9e8936085c38cb8c924efe42ed
7
+ data.tar.gz: ea42b47cfb048c87f34aec18ed908045cb5518e3801e8e5960a387cef5a1ddbbd656d3db0e15bea3c0577a0d2c571f8c3581b3da7f1f15d407954d0b49652f3d
data/README.md CHANGED
@@ -54,6 +54,20 @@ Dynamic attributes will appear in your model's attributes `user.attributes` as i
54
54
 
55
55
  You can access just the dynamic attributes by calling `dynamic_attributes`.
56
56
 
57
+ ## Validating
58
+
59
+ You can validate dynamic attributes if you declare them in your model:
60
+
61
+ class Person < ActiveRecord::Base
62
+ include DynamicAttributes
63
+
64
+ dynamic_attributes :hometown
65
+ validates_length_of :hometown, minimum: 2, allow_nil: true
66
+ end
67
+
68
+ Attempting to validate undeclared dynamic attributes will fail with NoMethodError if
69
+ the attribute hasn't been set at all; don't do that.
70
+
57
71
  ## Querying
58
72
 
59
73
  You can query for matches to dynamic attributes just like regular attributes.
@@ -8,6 +8,20 @@ module DynamicAttributes
8
8
  def partition_wheres(wheres)
9
9
  wheres.partition { |k, v| column_names.include?(k.to_s) || reflect_on_association(k.to_sym) }.map { |x| Hash[x] }
10
10
  end
11
+
12
+ def dynamic_attributes(*args)
13
+ args.each do |attr|
14
+ class_eval <<-ENDOFCODE
15
+ def #{attr}
16
+ read_dynamic_attribute(#{attr.inspect})
17
+ end
18
+
19
+ def #{attr}=(v)
20
+ write_dynamic_attribute(#{attr.inspect}, v)
21
+ end
22
+ ENDOFCODE
23
+ end
24
+ end
11
25
  end
12
26
 
13
27
  def [](attr_name)
@@ -1,3 +1,3 @@
1
1
  module Dynamometer
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -3,6 +3,10 @@ class Person < ActiveRecord::Base
3
3
 
4
4
  belongs_to :father, class_name: 'Person'
5
5
 
6
+ dynamic_attributes :hometown
7
+
8
+ validates_length_of :hometown, minimum: 3, allow_nil: true
9
+
6
10
  def active_model_serializer
7
11
  PersonSerializer
8
12
  end
data/test/person_test.rb CHANGED
@@ -103,6 +103,12 @@ class PersonTest < ActiveSupport::TestCase
103
103
  assert_equal @person, results.first
104
104
  end
105
105
 
106
+ test "can put validations on dynamic attributes" do
107
+ @person = Person.new(name: 'Nobody', hometown: 'X')
108
+ assert !@person.valid?
109
+ assert @person.errors.has_key?(:hometown)
110
+ end
111
+
106
112
  test "chaining does not damage original Relation" do
107
113
  original_relation = Person.where(id: 1)
108
114
  new_relation = original_relation.where(magic_level: 'over 9000')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamometer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Colvin