store_model 0.9.0 → 0.12.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
  SHA256:
3
- metadata.gz: e85a27e1f3a20217b3f6134d8f2679d556ce445c74b47ff223a3abec247219b9
4
- data.tar.gz: e4c5baceadbbd1049ff8c0616e31d41302178ed74929ee512e1625a8d983c870
3
+ metadata.gz: 59ca0d09ab27d1e2fb7d83c2463d6467879fc915343bef013cc45759c2c1d508
4
+ data.tar.gz: 58cf03e6d692d62715ee0564f79913a8212e73b95defae7572ef88314c244265
5
5
  SHA512:
6
- metadata.gz: 8e900e6ffeaffb83c1bc87b66b810d5e652871908c46542f1d3edbd7791761721d8a69bef57b9e187ce700c480dbd7c59d0dc9cda6a01092d64fcba27c4eb277
7
- data.tar.gz: 8744a4125da41b45bb0bb510296924758b5b07345e173cf4193a5ca0b3759f62aa2dfc54b4d9233d757d7c92e8ce0a0fed2c139f95c51ab91935bfc8e440001e
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
@@ -11,10 +11,13 @@ module StoreModel
11
11
  def self.included(base) # :nodoc:
12
12
  base.include ActiveModel::Model
13
13
  base.include ActiveModel::Attributes
14
+ base.include ActiveModel::AttributeMethods
14
15
  base.include StoreModel::NestedAttributes
15
16
 
16
17
  base.extend StoreModel::Enum
17
18
  base.extend StoreModel::TypeBuilders
19
+
20
+ base.attribute_method_suffix "?"
18
21
  end
19
22
 
20
23
  attr_accessor :parent
@@ -40,6 +43,13 @@ module StoreModel
40
43
  attributes.all? { |name, value| value == other.attributes[name] }
41
44
  end
42
45
 
46
+ # Returns hash for a StoreModel::Model instance based on attributes hash
47
+ #
48
+ # @return [Integer]
49
+ def hash
50
+ attributes.hash
51
+ end
52
+
43
53
  # Allows to call :presence validation on the association itself.
44
54
  #
45
55
  # @return [Boolean]
@@ -110,5 +120,14 @@ module StoreModel
110
120
  def unknown_attributes
111
121
  @unknown_attributes ||= {}
112
122
  end
123
+
124
+ private
125
+
126
+ def attribute?(attribute)
127
+ case value = attributes[attribute]
128
+ when 0 then false
129
+ else value.present?
130
+ end
131
+ end
113
132
  end
114
133
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "0.9.0"
4
+ VERSION = "0.12.0"
5
5
  end
data/lib/store_model.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "store_model/model"
4
4
  require "store_model/configuration"
5
- require "store_model/railtie"
5
+ require "store_model/railtie" if defined?(::Rails::Railtie)
6
6
  require "active_model/validations/store_model_validator"
7
7
 
8
8
  module StoreModel # :nodoc:
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.9.0
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-04-21 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