yml_record 0.1.3 → 0.1.4

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: 2fca9dae5bf10baf7aa88ebc253304b24b91f29b144fc0797883cb257436d694
4
- data.tar.gz: 7c0104c4d01ae2f4ec5c00b0c58fd9cdca6d686f925600cf494e4a540a08b2e4
3
+ metadata.gz: bc3ea52217e192ab11305116a1c9c4822209a106c8412cb59f4e17e399da6698
4
+ data.tar.gz: 7beec8530b22a8a6545c07dc4f443d536d95f8b0f0a85a92fd9c75ea9ee8af24
5
5
  SHA512:
6
- metadata.gz: '097bd7048303e136a95d7131c16c993f4a64dec847dca3a1a85fc8b30ab4a754f607758920905f4c94f3a045d7c70c881ade6b734076a6c31cc1600dd162c88b'
7
- data.tar.gz: b437e63384dd038a4b8a760b5a59189a9eec1ae61affc809dd6b6a95c2477ff90b8dc5995d9621110ea4f4f4b490ebb6f8cf3fd12ad6fd4c82556e619cc6f046
6
+ metadata.gz: 18e31398c0bccae297cc956c07b2bf9494beab4054f0f423eda53d1c729725407bbde37c0bdfc61342e4fbc565a6b732fac9461499460815449d3014bda5b750
7
+ data.tar.gz: 14af825e0bec0db3e3923b300adf7467acad081c2a6fce55bc2d10a6cb5c7e42fb683d227eadf49d8bcc6edef7a68173821cae10a5bbe7c201ee446166537bd7
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ .byebug_history
data/Gemfile CHANGED
@@ -5,3 +5,4 @@ gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
7
  gem "rspec", "~> 3.0"
8
+ gem "activesupport", '~> 5.0'
data/Gemfile.lock CHANGED
@@ -1,12 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yml_record (0.1.2)
4
+ yml_record (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ activesupport (5.2.8)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 0.7, < 2)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ concurrent-ruby (1.1.10)
9
15
  diff-lcs (1.4.4)
16
+ i18n (1.10.0)
17
+ concurrent-ruby (~> 1.0)
18
+ minitest (5.15.0)
10
19
  rake (12.3.3)
11
20
  rspec (3.10.0)
12
21
  rspec-core (~> 3.10.0)
@@ -21,11 +30,15 @@ GEM
21
30
  diff-lcs (>= 1.2.0, < 2.0)
22
31
  rspec-support (~> 3.10.0)
23
32
  rspec-support (3.10.2)
33
+ thread_safe (0.3.6)
34
+ tzinfo (1.2.9)
35
+ thread_safe (~> 0.1)
24
36
 
25
37
  PLATFORMS
26
38
  ruby
27
39
 
28
40
  DEPENDENCIES
41
+ activesupport (~> 5.0)
29
42
  rake (~> 12.0)
30
43
  rspec (~> 3.0)
31
44
  yml_record!
data/README.md CHANGED
@@ -29,7 +29,7 @@ Create an ApplicationYmlRecord model that inherits from YmlRecord::Base
29
29
  Create a model that will be backed by a yml data file and inherit from ApplicationYmlRecord.
30
30
  ```
31
31
  # app/models/example_model.rb
32
- class ExampleModel < YamlRecord::Base; end
32
+ class ExampleModel < ApplicationYmlRecord
33
33
  ```
34
34
 
35
35
  Add a yml file (named the same as your model) to the `config/data/` folder
@@ -51,8 +51,104 @@ ExampleModel.find(1) #=> first instance of ExampleModel
51
51
  etc
52
52
  ```
53
53
 
54
+ The automatic folder location for the yml files is `config/data/` but this can be manually overwritten
55
+ The automatic file name for each model is the demodulized snakecase class name (e.g `App::ExampleClass` => `example_class.yml`)
56
+
57
+ This automatic functionality can be overwritten when needed
58
+ ```
59
+ # to overwrite individual class's data filename to `bar.yml`
60
+ class Foo < ApplicationYmlRecord
61
+ self.filename = 'bar'
62
+ end
63
+
64
+ # to overwrite data folder location
65
+ class ApplicationYmlRecord < YamlRecord::Base
66
+ self.filepath = 'another/folder'
67
+ end
68
+ ```
69
+ will look for data in `another/folder/bar/yml`
70
+
54
71
  See below for specific functionality implementations.
72
+ The implementation examples assume the following setup
73
+ ```
74
+ # example.yaml
75
+ - id: 1
76
+ old: true
77
+ colour: :green
78
+ - id: 2
79
+ colour: :red
80
+ old: true
81
+ - id: 3
82
+ colour: :red
83
+ old: false
84
+
85
+ class Example < YamlRecord::Base
86
+ end
87
+ ```
55
88
 
89
+ ### Class methods
90
+ #### Scopes
91
+ Scopes will return a chainable relation with instances that match the attributes similar to active record relations
92
+ ```
93
+ Example.where(old: true).where(colour: :red) #=> [<Example id: 1 />]
94
+ is the same as
95
+ Example.where(old: true, colour: :red)
96
+ ```
97
+
98
+ ### Relating to YamlRecord classes
99
+ Other classes can relate to yaml record classes using an active record style integration
100
+ #### Has Many
101
+ When storing primary keys of yaml records on another class, the belongs_to method can be used to create a parent like relationship.
102
+ If the other class is dynamic (e.g: backed by a database) then the relationship can be set to dynamic (default).
103
+ Otherwise (e.g: that class is also a yaml record) the relationship can be set to being not dynamic.
104
+ ```
105
+ class Child
106
+ attr_accessor :example_id
107
+
108
+ YmlRecord.relationships.belongs_to self, :example
109
+ end
110
+ ```
111
+ This will add `Child#example` and `Child.example=(other_example)` instance methods.
112
+
113
+ The following will mark this relationship as static (not dynamic), which will not create the setter method (`Child.example=(other_example)`)
114
+ ```
115
+ YmlRecord.relationships.belongs_to self, :example, dynamic: false
116
+ ```
117
+
118
+ The following keys can all be manually set in the same way as with active record belongs to:
119
+ - foreign_key
120
+ - primary_key
121
+ - class_name
122
+
123
+ If using active record to relate to yaml records, the following can be added to the application record to streamline implementation
124
+
125
+ ```
126
+ class Car < ApplicationYmlRecord; end
127
+
128
+ class ApplicationRecord < ActiveRecord::Base
129
+ def self.belongs_to(name, scope = nil, yml_relationship: false, **opts, &block)
130
+ return super(name, scope, **opts, &block)
131
+
132
+ YmlRecord.relationships.belongs_to self, name, scope, **opts, &block
133
+ end
134
+ end
135
+
136
+ # has db column of car_id (of type corresponding to car primary key)
137
+ class Driver < ApplicationRecord
138
+ belongs_to :car, yml_relationship: true
139
+ end
140
+ ```
141
+
142
+ ### Instance methods
143
+ #### Boolean methods
144
+
145
+ When an attribute is a boolean a boolean instance method is automatically created.
146
+ ```
147
+ Example.find(1).old? #=> true
148
+ Example.find(2).old? #=> false
149
+ ```
150
+
151
+ ### TODO documentation
56
152
  TODO: document implementation of custom filename
57
153
  TODO: document implementation of custom identifier
58
154
  TODO: document implementation of scope
@@ -61,12 +157,10 @@ TODO: document implementation of enum
61
157
  ## Future implementations
62
158
  TODO: document
63
159
 
64
- - [ ] default boolean methods. Currently need to specifically set up boolean methods.
65
160
  - [ ] setting data folder in initializer for gem
66
161
  - [ ] allow setting of specific data for specific deployments
67
162
 
68
163
 
69
-
70
164
  ## Development
71
165
 
72
166
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -16,7 +16,11 @@ module YmlRecord
16
16
  end
17
17
 
18
18
  def filepath
19
- @filepath ||= "config/data/#{filename}.yml"
19
+ @filepath ||= 'config/data'
20
+ end
21
+
22
+ def filelocation
23
+ @filelocation ||= "#{filepath}/#{filename}.yml"
20
24
  end
21
25
  end
22
26
  end
@@ -0,0 +1,39 @@
1
+ require 'active_support/core_ext/string'
2
+
3
+ module YmlRecord
4
+ module RelationshipBuilders
5
+ class BelongsTo
6
+ def self.build(model, name, scope = nil, **options, &_block)
7
+ new(model, name, scope, **options).build
8
+ end
9
+
10
+ def initialize(model, name, scope, dynamic: true, **options)
11
+ @model = model
12
+ @name = name
13
+ raise(NotImplementedError, 'yml record belongs to with scope is not yet implemented') if !!scope
14
+ @dynamic = dynamic
15
+ @options = options
16
+ end
17
+
18
+ def build
19
+ klass = (options.fetch(:class_name, nil) || name.to_s.camelize).constantize
20
+ klass_primary_key = options.fetch(:primay_key, klass.primary_key)
21
+ foreign_key = options.fetch(:foreign_key, "#{name}_#{klass_primary_key}")
22
+
23
+ model.define_method name do
24
+ klass.find_by(klass_primary_key => send(foreign_key))
25
+ end
26
+
27
+ if dynamic
28
+ model.define_method "#{name}=" do |instance|
29
+ self.send("#{foreign_key}=", instance.send(klass_primary_key))
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :model, :name, :dynamic, :options
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+ require 'yml_record/relationship_builders/belongs_to.rb'
2
+
3
+ module YmlRecord
4
+ module RelationshipBuilders
5
+ def self.belongs_to(*args, **opts, &block)
6
+ BelongsTo.build(*args, **opts, &block)
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module YmlRecord
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/yml_record.rb CHANGED
@@ -1,6 +1,11 @@
1
1
  require 'yml_record/base'
2
2
  require "yml_record/version"
3
+ require 'yml_record/relationship_builders'
3
4
 
4
5
  module YmlRecord
5
6
  class Error < StandardError; end
7
+
8
+ def self.relationships
9
+ YmlRecord::RelationshipBuilders
10
+ end
6
11
  end
data/yml_record.gemspec CHANGED
@@ -13,7 +13,6 @@ Gem::Specification.new do |spec|
13
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
14
  spec.metadata["source_code_uri"] = "https://github.com/tjbarker/yml_record#readme"
15
15
 
16
- # TODO: do later cause I CBF now
17
16
  # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
17
 
19
18
  # spec.metadata["homepage_uri"] = spec.homepage
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yml_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Barker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-04 00:00:00.000000000 Z
11
+ date: 2022-07-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A gem for presenting ActiveRecord like interface for yml backed models
14
14
  email:
@@ -38,6 +38,8 @@ files:
38
38
  - lib/yml_record/builders/scope.rb
39
39
  - lib/yml_record/helpers/delegate_missing_to.rb
40
40
  - lib/yml_record/relation.rb
41
+ - lib/yml_record/relationship_builders.rb
42
+ - lib/yml_record/relationship_builders/belongs_to.rb
41
43
  - lib/yml_record/version.rb
42
44
  - yml_record.gemspec
43
45
  homepage: https://rubygems.org/gems/yml_record