smart_attr 0.1.1 → 0.2.0

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: 3cf5b0d2587c7b36d9804217e246217bb9b2da21
4
- data.tar.gz: 4d76c6482d0ca12b216ca4d5161b54763d0436f8
3
+ metadata.gz: ffeafe2f5676d53d9e143be0123ad94130a5691c
4
+ data.tar.gz: 752c279773092e68fa8a6ac0a2e1efe9a2355fc6
5
5
  SHA512:
6
- metadata.gz: 7255744ece8b2cca8c2468f03e3fb0d3e8d6a1025b0ebaf32739ac576b7d69532411e5d3432c4b9de103c9860674218606f707cf0e21ce6e645bb4cca96b7c2c
7
- data.tar.gz: fb3b58541df5bb16772ac0f9526f39dface701dbde7c00ef993394cbbd3eedd708090d93bd40d1d04b86cc90e04bc03c29c50e0f93c6781ca4fbe674acbc7504
6
+ metadata.gz: d33fc6ee4b4868bea6285ff775c85d909333eadafa7bb55a7a1ec30d6a2159b250446dbb54764a87376b606af9bfa188df19feb7baba16309519e4ae3b1eae0a
7
+ data.tar.gz: a45c97aa6f0ef2570bb98df2cf2e81412071c46004a62d0f4d285b6ef07e686f40b25d942dd4c34a793fd654b6f1f679993f3448426942545b4dd58b988e2147
data/README.md CHANGED
@@ -79,13 +79,16 @@ movie.inspect # => "#<Movie:0x007fcc041b0490 @star=3>"
79
79
 
80
80
  It is almost the same like the basic usage when used with ActiveRecord.
81
81
 
82
- The only difference is that it will not create an instance_variable to store the value when you set value for the attribute.
82
+ There is just one difference between this and the basic usage, however, there is one extra functionality when used with ActiveRecord.
83
+
84
+ The only one difference is that it will not create an instance_variable to store the value when you set value for the attribute.
83
85
  This is because it will store the value in the database.
86
+ And the extra functionality is that it will define scope for you when used with ActiveRecord.
84
87
 
85
88
  For example, suppose you have a class named "Movie" with database table 'movies', then you should ensure that 'movies' have column 'star' before you use 'smart_attr :star, config: { # something }'
86
89
 
87
90
  ```ruby
88
- class Movie
91
+ class Song < ActiveRecord::Base
89
92
 
90
93
  include SmartAttr::Base
91
94
 
@@ -96,29 +99,36 @@ class Movie
96
99
  four: { value: 4, desc: 'four star' },
97
100
  five: { value: 5, desc: 'five star' }
98
101
  }
102
+
99
103
  end
100
104
 
101
- Movie.star_config_hash
102
- # => { :one=>{:value=>1, :desc=>"one star"},
103
- # :two=>{:value=>2, :desc=>"two star"},
104
- # :three=>{:value=>3, :desc=>"three star"},
105
- # :four=>{:value=>4, :desc=>"four star"},
106
- # :five=>{:value=>5, :desc=>"five star"}
107
- # }
108
105
 
109
- movie = Movie.new # => #<Movie:0x007fcc041b0490>
106
+ song = Song.new(star: 0)
110
107
 
111
- movie.star = 1 # => 1
112
- movie.star_name # => :one
113
- movie.star_desc # => "one star"
114
- movie.star_one? # => true
115
- movie.star_two? # => false
108
+ song.save
116
109
 
117
- movie.star_two! # => 2
118
- movie.star # => 2
119
- movie.star_two? # => true
110
+ song.star = 1 # => 1
111
+ song.star_name # => :one
112
+ song.star_desc # => "one star"
113
+ song.star_one? # => true
114
+ song.star_two? # => false
120
115
 
121
- movie.star_config # => {:value=>2, :desc=>"two star", :key=>:two}
116
+ song.star_two! # => 2
117
+ song.star # => 2
118
+ song.star_two? # => true
119
+
120
+ song.reload
121
+ song.star # => 1
122
+ song.star_two!
123
+ song.save
124
+ song.reload
125
+ song.star # => 2
126
+
127
+ song.star_config # => {:value=>2, :desc=>"two star", :key=>:two}
128
+
129
+ # scope
130
+ Song.star_one # The same as: Song.where(star: 1)
131
+ Song.star_two # The same as: Song.where(star: 2)
122
132
  ```
123
133
 
124
134
  ### Used With Mongoid
@@ -69,11 +69,15 @@ module SmartAttr
69
69
 
70
70
  all_instance_methods = self.private_instance_methods + self.instance_methods
71
71
  if all_instance_methods.include?(:write_attribute) && all_instance_methods.include?(:read_attribute)
72
+
72
73
  define_method "#{column_name}_name=".to_sym do |name|
73
74
  write_attribute(column_name, self.class.send("#{column_name}_config")[name].try(:[], :value))
74
75
  end
75
76
 
76
77
  config.keys.each do |_key|
78
+ define_singleton_method "#{column_name}_#{_key}".to_sym do
79
+ self.where("#{column_name}" => self.send("#{column_name}_config")[_key.to_sym].try(:[], :value))
80
+ end
77
81
 
78
82
  define_method "#{column_name}_#{_key}!".to_sym do
79
83
  write_attribute(column_name, self.class.send("#{column_name}_config")[_key.to_sym].try(:[], :value))
@@ -1,3 +1,3 @@
1
1
  module SmartAttr
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_attr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kunliu