store_model 0.11.1 → 0.12.0

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
  SHA256:
3
- metadata.gz: 066a53b97ac50d61d82c10376abe79d3dac452ff2ff33f3a5d650e7e119e356f
4
- data.tar.gz: '0892deb0ac4827ad431ea76c0d1a121bacfbb0fe66204164fba1c6baec685cad'
3
+ metadata.gz: 59ca0d09ab27d1e2fb7d83c2463d6467879fc915343bef013cc45759c2c1d508
4
+ data.tar.gz: 58cf03e6d692d62715ee0564f79913a8212e73b95defae7572ef88314c244265
5
5
  SHA512:
6
- metadata.gz: 0333e12118a8a445a22ed591e1e887dfc3912f6e02f41e0cc63d87d8dde76a001abd30e5860afba0bde49a4f6163b7092e4fc9c931865586344959c52ddf2cdc
7
- data.tar.gz: 9eaffc2c1660ea778b5360410e1498ff3dd4add451cbcdc1cf6ee32a83fb65bd4286b76929c9cddf7d4c693811bee83cc5669391fa0ea84dcdd0254e5fc44a25
6
+ metadata.gz: 22e05c3bf0438d9add8abfed9950b3a8fba77766b90e5105ef766bf8bc64e233758d85ec841aefa026d6dbe075334999e969c90fbdf5057bd45cd38355a240ac
7
+ data.tar.gz: 508b2bddff9deac36022342f8069adfe66857301436347d9d7fd01fdf594207c5e83670581f2fefc50567c80d927b3344cb5ff62e5b9ae5e7269f0f86db833df
data/README.md CHANGED
@@ -79,6 +79,11 @@ end
79
79
  product.save
80
80
  ```
81
81
 
82
+ _Usage note: Rails and assigning Arrays/Hashes to records_
83
+
84
+ - Assigned attributes must be a String, Hash, Array of Hashes, or StoreModel. For example, if the attributes are coming from a controller, be sure to convert any ActionController::Parameters as needed.
85
+ - Any changes made to a StoreModel instance requires the attribute be re-assigned; Rails doesn't track mutations of objects. For example: `self.my_stored_models = my_stored_models.map(&:as_json)`
86
+
82
87
  ## Documentation
83
88
 
84
89
  1. [Installation](./docs/installation.md)
@@ -10,14 +10,15 @@ module StoreModel
10
10
  # @param kwargs [Object]
11
11
  def enum(name, values = nil, **kwargs)
12
12
  values ||= kwargs[:in] || kwargs
13
+ options = kwargs.slice(:_prefix, :_suffix, :default)
13
14
 
14
15
  ensure_hash(values).tap do |mapping|
15
- define_attribute(name, mapping, kwargs[:default])
16
+ define_attribute(name, mapping, options[:default])
16
17
  define_reader(name, mapping)
17
18
  define_writer(name, mapping)
18
19
  define_method("#{name}_value") { attributes[name.to_s] }
19
20
  define_method("#{name}_values") { mapping }
20
- define_predicate_methods(name, mapping)
21
+ define_predicate_methods(name, mapping, options)
21
22
  end
22
23
  end
23
24
 
@@ -36,8 +37,9 @@ module StoreModel
36
37
  define_method("#{name}=") { |value| super type.cast_value(value) }
37
38
  end
38
39
 
39
- def define_predicate_methods(name, mapping)
40
+ def define_predicate_methods(name, mapping, options)
40
41
  mapping.each do |label, value|
42
+ label = affixed_label(label, name, options[:_prefix], options[:_suffix])
41
43
  define_method("#{label}?") { send(name) == mapping.key(value).to_s }
42
44
  end
43
45
  end
@@ -51,5 +53,12 @@ module StoreModel
51
53
 
52
54
  values.zip(0...values.size).to_h
53
55
  end
56
+
57
+ def affixed_label(label, name, prefix = nil, suffix = nil)
58
+ prefix = prefix == true ? "#{name}_" : "#{prefix}_" if prefix
59
+ suffix = suffix == true ? "_#{name}" : "_#{suffix}" if suffix
60
+
61
+ "#{prefix}#{label}#{suffix}"
62
+ end
54
63
  end
55
64
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "0.11.1"
4
+ VERSION = "0.12.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: store_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-09 00:00:00.000000000 Z
11
+ date: 2021-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord