built_in_data 1.2.0 → 1.2.1

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: f76763097ab596cdc8993a507e28d53ed4cc58efaba3c8fad8ac202498fa0429
4
- data.tar.gz: e7ac337759999e488b7bb1c531c0f6b70649a38acb6e54c761b0fdb9e550001e
3
+ metadata.gz: 1dcb3fbcceaeff5cb4d43a1bc0c08edb3b375324ad4797c1ffcf24923913f0cd
4
+ data.tar.gz: 2b2195a4b426b877bb80cfb36c51e4244e05540ed71e8cb853c9eb0175373cde
5
5
  SHA512:
6
- metadata.gz: fd963620a173fb9cc213ce414c9d9126ac509bb57a75b951a7e0c44c2a75919864c5fb8cee7b622fe625d3e801cf4963a3943646a243b546209ffe449b9225e2
7
- data.tar.gz: 23420031c980b6e3fc420176dc50f84af1ef756bf6335d07f116cdf275b078b256da284771d926d7ed7968be8d01f552a4f653c4624c5e750e3135c42137fd49
6
+ metadata.gz: 8252552d62e0f142c16f51465205ff986e11f5f30d23c4af1db7d45359e36b90a32814d66d277fdb29cb982fadaee65e6a2419352a97c19c3c5b1efde5a59e8c
7
+ data.tar.gz: 973e5ec125c4e846c63ec464de0e3a3824491f5704d478d2dedab136ba269a4b8e0a0e00d387ceaf548c812fb75dacc631e239f44d2e431a0163e7c4db0d6638
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2012-2022 Brightways Learning https://www.brightwayslearning.org
1
+ Copyright 2012-2023 Brightways Learning https://www.brightwayslearning.org
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -53,3 +53,6 @@ Use **built_in?** to check if an object was loaded by BuiltInData:
53
53
  ```ruby
54
54
  object.built_in?
55
55
  ```
56
+
57
+ ## License
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/setup"
2
4
 
3
5
  require "bundler/gem_tasks"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BuiltInData
2
- VERSION = "1.2.0"
4
+ VERSION = "1.2.1"
3
5
  end
data/lib/built_in_data.rb CHANGED
@@ -1,32 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "built_in_data/version"
2
4
 
3
5
  module BuiltInData
4
6
  extend ActiveSupport::Concern
5
7
 
6
8
  included do
7
- # all built in data objects should have a built_in_key, model objects without a key will not be modified or removed
9
+ # All built in data objects should have a built_in_key, model objects without a key will not be modified or removed
8
10
  validates_uniqueness_of :built_in_key, allow_nil: true, case_sensitive: false
9
11
 
10
- scope :built_in, -> { where "#{table_name}.built_in_key IS NOT NULL" }
12
+ scope :built_in, -> { where.not(built_in_key: nil) }
11
13
  end
12
14
 
13
15
  module ClassMethods
16
+ # Inserts new, updates existing, and destorys removed built_in objects
14
17
  def load_built_in_data!(hash = nil)
15
18
  objects_hash = prepare_objects_hash(hash)
16
- Array.new.tap do |updated_objects|
17
-
18
- objects_hash.each do |key, attributes|
19
- updated_objects << create_or_update!(key, attributes)
20
- end
19
+ destroy_removed_built_in_objects!(objects_hash.keys)
21
20
 
22
- # destroy any built_in objects that have been removed from built_in_data_attributes
23
- self.built_in.each do |object|
24
- object.destroy unless objects_hash.has_key?(object.built_in_key)
25
- end
21
+ objects_hash.map do |key, attributes|
22
+ create_or_update!(key, attributes)
26
23
  end
27
24
  end
28
25
 
29
- # cached database id for fixture files
26
+ # Cached database id for fixture files
30
27
  def built_in_object_id(key)
31
28
  built_in_object_ids[key]
32
29
  end
@@ -39,20 +36,25 @@ module BuiltInData
39
36
  private
40
37
 
41
38
  def prepare_objects_hash(hash)
42
- return hash.nil? ? load_yaml_data : hash.with_indifferent_access
39
+ hash.nil? ? load_yaml_data : hash.with_indifferent_access
43
40
  end
44
41
 
45
42
  def load_yaml_data
46
43
  # allow a standard key to be used for defaults in YAML files
47
44
  YAML.safe_load(
48
45
  read_and_erb_process_yaml_file,
49
- permitted_classes: [Date],
50
- aliases: true
51
- ).except('DEFAULTS')
46
+ permitted_classes: [Date],
47
+ aliases: true
48
+ ).except("DEFAULTS")
52
49
  end
53
50
 
54
51
  def read_and_erb_process_yaml_file
55
- ERB.new(File.read(Rails.root.join('db', 'built_in_data', "#{table_name}.yml"))).result
52
+ ERB.new(Rails.root.join("db", "built_in_data", "#{table_name}.yml").read).result
53
+ end
54
+
55
+ # Destroys built_in objects with a built_in_key not present in built_in_keys array
56
+ def destroy_removed_built_in_objects!(built_in_keys)
57
+ built_in.where.not(built_in_key: built_in_keys).destroy_all
56
58
  end
57
59
 
58
60
  def create_or_update!(key, attributes)
@@ -62,15 +64,15 @@ module BuiltInData
62
64
  end
63
65
  end
64
66
 
65
- # memoized hash of built in object ids
67
+ # Memoized hash of built in object ids
66
68
  def built_in_object_ids
67
69
  @built_in_object_ids ||= Hash.new do |hash, key|
68
- hash[key] = where(built_in_key: key).pluck(:id).first
70
+ hash[key] = where(built_in_key: key).pick(:id)
69
71
  end
70
72
  end
71
73
  end
72
74
 
73
75
  def built_in?
74
- !built_in_key.blank?
76
+ built_in_key.present?
75
77
  end
76
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: built_in_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Baldwin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-01-27 00:00:00.000000000 Z
12
+ date: 2023-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  requirements: []
63
- rubygems_version: 3.4.1
63
+ rubygems_version: 3.4.5
64
64
  signing_key:
65
65
  specification_version: 4
66
66
  summary: Data management for Rails models.