dynamometer 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: d174af14645fe43c7e534deaad6c6c0abf04d38d
4
- data.tar.gz: ab541b9eddc391dcbe15a0f8f88fbda329d6ac68
3
+ metadata.gz: 2a5a9ab37e0048fa465edda592aa687552fe61b2
4
+ data.tar.gz: 4b9b62aec8ac9d6d13b2b6df5dc1dff205bd4e96
5
5
  SHA512:
6
- metadata.gz: a0181af7f0587740596cab412f42433763ad1b44f5e461a0b2c29776501ef286c65964e05be9f1ef46049e3d341780bb765fdf17213f6ee0483f6e648b6b5bf4
7
- data.tar.gz: 9233fa33bc59a4071638042f41daecf06bb715baea5bbcd5b126731061db5f47bfab4a1ede92b49135a285a7d01cf9abaaabfe7f058168af00bb616c7ba9da24
6
+ metadata.gz: 80be7c4de6046ba5007734148de1ad4568de04125569787cb73ec773cfcdc8d63c8b29c090e6731cacf1bc84c76a26656a8351397a5ee91a7fff04cf76d4b1ad
7
+ data.tar.gz: 486f7e1f582c0386475d5d621f1c8ecbd66fa9799c5db42166e6db5de1657e69b5e478264dbff2299e6c3d3a35839a346a162c0d5306d3dbee7599f66a91c46e
data/README.md CHANGED
@@ -1,29 +1,97 @@
1
1
  # Dynamometer
2
2
 
3
- TODO: Write a gem description
3
+ Adds dynamic attributes to ActiveRecord models
4
4
 
5
- ## Installation
5
+ ## Usage
6
6
 
7
- Add this line to your application's Gemfile:
7
+ Generate a migration to enable hstore
8
8
 
9
- gem 'dynamometer'
9
+ class EnableHstore < ActiveRecord::Migration
10
+ def up
11
+ enable_extension "hstore"
12
+ end
13
+
14
+ def down
15
+ disable_extension "hstore"
16
+ end
17
+ end
18
+
19
+ Add a dynamic_attributes hstore when creating a table
10
20
 
11
- And then execute:
21
+ create_table :users do |t|
22
+ t.hstore :dynamic_attributes
23
+ t.index :dynamic_attributes
24
+ end
25
+
26
+ or add dynamic_attributes to an existing table
12
27
 
13
- $ bundle
28
+ add_column :users, :dynamic_attributes, :hstore
29
+ add_index :users, :dynamic_attributes
14
30
 
15
- Or install it yourself as:
31
+ Add dynamic attributes to your ActiveRecord model
16
32
 
17
- $ gem install dynamometer
33
+ class User < ActiveRecord::Base
34
+ include DynamicAttributes
35
+ end
18
36
 
19
- ## Usage
37
+ ## Accessors
38
+
39
+ You can read dynamic attributes like you would typical attributes.
40
+
41
+ user.category
42
+ user[:category]
43
+ user['category']
44
+
45
+ You can write dynamic attributes like you would typical attributes, as well.
46
+
47
+ user.category = 'superuser'
48
+ user[:category] = 'superuser'
49
+ user['category'] = 'superuser'
50
+ user.update_attribute(:category, 'superuser')
51
+ user.update_attributes(category: 'superuser')
52
+
53
+ Dynamic attributes will appear in your model's attributes `user.attributes` as if they were typical database attributes.
54
+
55
+ You can access just the dynamic attributes by calling `dynamic_attributes`.
56
+
57
+ ## Querying
20
58
 
21
- TODO: Write usage instructions here
59
+ You can query for matches to dynamic_attributes by calling `where_dynamic_attributes`.
22
60
 
23
- ## Contributing
61
+ current_site.users.where_dynamic_attributes(category: 'superuser')
62
+ current_site.users.where_dynamic_attributes(category: 'superuser', name: 'Steve')
24
63
 
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
64
+ I can't figure out how to do this correctly in the gem. So, for now to use `where_dynamic_attributes`, put this in your `config/initializers`:
65
+
66
+ module ActiveRecord
67
+ module QueryMethods
68
+ extend ActiveSupport::Concern
69
+
70
+ def where_dynamic_attributes(filters)
71
+ (filters || {}).each do |k, v|
72
+ self.where_values += build_where("dynamic_attributes @> hstore(?, ?)", [k, v])
73
+ end
74
+ self
75
+ end
76
+
77
+ end
78
+ end
79
+
80
+ ## ActiveModel Serializers
81
+
82
+ If you want to serialize all of your dynamic attributes using activemodel serializers
83
+
84
+ class UserSerializer < ActiveModel::Serializer
85
+ include DynamicAttributesSerializer
86
+ attributes :id
87
+ end
88
+
89
+ ## Installation
90
+
91
+ Add this line to your application's Gemfile:
92
+
93
+ gem 'dynamometer'
94
+
95
+ And then execute:
96
+
97
+ $ bundle
@@ -2,12 +2,13 @@ module DynamicAttributes
2
2
 
3
3
  extend ActiveSupport::Concern
4
4
 
5
- def [](name)
6
- attribute_name = name.to_s
7
- if attribute_name == 'dynamic_attributes' || !has_dynamic_attribute?(attribute_name)
5
+ def [](attr_name)
6
+ if @attributes.keys.include?(attr_name.to_s)
8
7
  super
8
+ elsif has_dynamic_attribute?(attr_name)
9
+ read_dynamic_attribute(attr_name)
9
10
  else
10
- dynamic_attributes[attribute_name]
11
+ super
11
12
  end
12
13
  end
13
14
 
@@ -15,17 +16,15 @@ module DynamicAttributes
15
16
  begin
16
17
  super
17
18
  rescue ActiveModel::MissingAttributeError
18
- send "#{key}=", value
19
+ write_dynamic_attribute(key, value)
19
20
  end
20
21
  end
21
22
 
22
- def method_missing(name, *args)
23
- method_name = name.to_s
24
- if method_name =~ /^[\w]+\=$/
25
- self[:dynamic_attributes] = dynamic_attributes.merge(method_name.chop => args[0])
26
- args[0]
27
- elsif has_dynamic_attribute?(method_name)
28
- self[method_name]
23
+ def method_missing(method, *args)
24
+ if has_dynamic_attribute?(method)
25
+ read_dynamic_attribute(method)
26
+ elsif method =~ /^[\w]+\=$/
27
+ write_dynamic_attribute(method.to_s.chop, args[0])
29
28
  else
30
29
  super
31
30
  end
@@ -33,18 +32,31 @@ module DynamicAttributes
33
32
 
34
33
  def attributes
35
34
  attrs = super
36
- props = attrs.delete('dynamic_attributes') || {}
37
- attrs.merge(props)
35
+ dynamic_attrs = attrs.delete('dynamic_attributes') || {}
36
+ dynamic_attrs.merge(attrs)
38
37
  end
39
38
 
40
39
  def dynamic_attributes
41
- self[:dynamic_attributes] || {}
40
+ self['dynamic_attributes'] || {}
41
+ end
42
+
43
+ def has_attribute?(attr_name)
44
+ super || has_dynamic_attribute?(attr_name)
42
45
  end
43
46
 
44
47
  private
45
48
 
46
- def has_dynamic_attribute?(name)
47
- dynamic_attributes.has_key?(name.to_s)
49
+ def has_dynamic_attribute?(attr_name)
50
+ dynamic_attributes.has_key?(attr_name.to_s)
51
+ end
52
+
53
+ def read_dynamic_attribute(attr_name)
54
+ dynamic_attributes[attr_name.to_s]
55
+ end
56
+
57
+ def write_dynamic_attribute(attr_name, value)
58
+ @attributes.merge('dynamic_attributes' => dynamic_attributes.merge(attr_name.to_s => value))
59
+ value
48
60
  end
49
61
 
50
62
  end
data/dynamometer.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "dynamometer"
7
- spec.version = "0.0.2"
7
+ spec.version = "0.0.3"
8
8
  spec.authors = ["John Colvin"]
9
9
  spec.email = ["john.colvin@neo.com"]
10
10
  spec.description = "Adds support for searchable, sortable, dynamic ActiveRecord attributes"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamometer
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
  - John Colvin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-22 00:00:00.000000000 Z
11
+ date: 2013-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler