yaml_enumeration 0.1.0 → 0.3.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
- SHA1:
3
- metadata.gz: d3b4ba65e62cb6d1cab8e1dff3e7bab95cda4e94
4
- data.tar.gz: 74f6a30f937b96db97add1ed81db44b31045523d
2
+ SHA256:
3
+ metadata.gz: 329c035e5c90f7cd2c444c1237f2c1c5bf00b50ae6cefcf6f8658c1cdda7fe45
4
+ data.tar.gz: debf948e5a235157517ef21d67c6d498f3d2a623cfc9cbe384ae7d81dc8ed05f
5
5
  SHA512:
6
- metadata.gz: 0a9501189bf47b9fe78c93d1b2020dfc614429be7eac9a93a49b3495d3930cca932ffbe11bff6e515a7d7092da1727c73c466abc43cbea10df2e2241e6f937d4
7
- data.tar.gz: 6d0a83d915f6eeb99d1df80201c7a26c9145f7277880cece0f96f3a9955225d676828de8d7883ae16ce2b8cedbd1449e90efba1be5ce43481a90698a2b46b396
6
+ metadata.gz: 730bdbbefa4291935287eda1f56d78cf5f4e4c349426caf4fc20597fdb2cf43d1a627300031a45716dc57b7ecd60c4453de7ee9bb47f76341c19d908d4332a36
7
+ data.tar.gz: 8b75285fa43d9440d635566104d5201f8a8192d58d4754c69c134470e8e348456e1b38519aea3b74b9be33fa0a93effd5872566e0d7f2257c86bb5f15c5e16fa
data/.travis.yml CHANGED
@@ -1,5 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.0
5
- before_install: gem install bundler -v 1.12.5
4
+ - 2.5.3
5
+ - 2.6.5
6
+ - 2.7.1
7
+ before_install: gem install bundler
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.3.0
4
+ * enable exclusion of blocks of YAML from the enumeration (support YAML anchors) [#6](https://github.com/alto/yaml_enumeration/pull/6)
5
+
6
+ ## 0.2.2
7
+ * enhance find-by methods [#4](https://github.com/alto/yaml_enumeration/pull/4)
8
+ * add support for Ruby `2.6` and `2.7` [#3](https://github.com/alto/yaml_enumeration/pull/3)
9
+
10
+ ## 0.2.1
11
+ * add support for Ruby `2.5.3` [#2](https://github.com/alto/yaml_enumeration/pull/2)
12
+
13
+ ## 0.2.0
14
+ * support _ActiveRecord_ methods `where`, `find_by` [#1](https://github.com/alto/yaml_enumeration/pull/1), thanks to [@richdrich](https://github.com/richdrich)
15
+
16
+ ## 0.1.0
17
+ * initial version
data/README.md CHANGED
@@ -1,12 +1,17 @@
1
- # YamlEnumeration
1
+ # Yaml Enumeration
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/yaml_enumeration`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version](https://badge.fury.io/rb/yaml_enumeration.svg)](http://badge.fury.io/rb/yaml_enumeration)
4
+ [![Build Status](https://travis-ci.org/alto/yaml_enumeration.svg?branch=master)](https://travis-ci.org/alto/yaml_enumeration)
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ Create classes which work (a bit) like
7
+ [ActiveRecord](http://guides.rubyonrails.org/active_record_basics.html)
8
+ classes, but are defined as fixed enumerations based on
9
+ [YAML](http://yaml.org)
10
+ files.
6
11
 
7
12
  ## Installation
8
13
 
9
- Add this line to your application's Gemfile:
14
+ Add this line to your application's `Gemfile`:
10
15
 
11
16
  ```ruby
12
17
  gem 'yaml_enumeration'
@@ -22,7 +27,98 @@ Or install it yourself as:
22
27
 
23
28
  ## Usage
24
29
 
25
- TODO: Write usage instructions here
30
+ Let's assume you have a `User` class representing a user who can log into your app.
31
+ This user is supposed to have a home country, and you want to do things like
32
+
33
+ ```ruby
34
+ user = User.new(country: Country.find_by_code('nz'))
35
+ puts user.country.code
36
+ # => 'nz'
37
+ puts user.country.name
38
+ # => 'New Zealand'
39
+ ```
40
+
41
+ You can then simply create a `Country` class:
42
+
43
+ ```ruby
44
+ # country.rb
45
+ class Country < YamlEnumeration::Enumeration
46
+ # imports the definitions from the yaml file countries.yml
47
+ load_values :countries
48
+
49
+ # all, where, find_by and find_by_type are provided
50
+ def self.find_by_code(code)
51
+ all.find_by(code: code)
52
+ end
53
+ end
54
+ ```
55
+
56
+ a `yaml` file containing all your countries:
57
+
58
+ ```yaml
59
+ # countries.yml
60
+ ---
61
+ nz:
62
+ id: 1 # has to be provided
63
+ type: new_zealand # has to be provided
64
+ name: New Zealand
65
+ code: nz
66
+ au:
67
+ id: 2
68
+ type: australia
69
+ name: Australia
70
+ code: au
71
+ ...
72
+ ```
73
+
74
+ and link that into your `User` class:
75
+
76
+ ```ruby
77
+ # user.rb
78
+ class User < ActiveRecord::Base
79
+ belongs_to_enumeration :country
80
+ end
81
+ ```
82
+
83
+ additional `yaml` syntax such as anchors can be removed from the enumeration by including `_exclude_from_enumeration`:
84
+
85
+ ```yaml
86
+ # countries.yml
87
+ ---
88
+ defaults_exclude_from_enumeration: &defaults
89
+ continent: Australia
90
+
91
+ nz:
92
+ <<: *defaults
93
+ id: 1 # has to be provided
94
+ type: new_zealand # has to be provided
95
+ name: New Zealand
96
+ code: nz
97
+ au:
98
+ <<: *defaults
99
+ id: 2
100
+ type: australia
101
+ name: Australia
102
+ code: au
103
+ uk:
104
+ <<: *defaults
105
+ id: 3
106
+ type: united_kingdom
107
+ name: United Kingdom
108
+ code: uk
109
+ continent: Europe
110
+ ...
111
+ ```
112
+
113
+ ## Accessing members
114
+
115
+ If you include a call to class method `with_named_items` you will get an item defined for each typed entry in the enumeration, e.g. `Country.NEW_ZEALAND` and `Country.AUSTRALIA`.
116
+
117
+ By passing a column name, e.g:
118
+ ```
119
+ with_named_items(:code)
120
+ ```
121
+ that column will be used, e.g: `Country.AU` and `Country.NZ`
26
122
 
27
123
  ## Development
28
124
 
@@ -32,10 +128,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
128
 
33
129
  ## Contributing
34
130
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yaml_enumeration. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
131
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alto/yaml_enumeration. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
132
 
37
133
 
38
134
  ## License
39
135
 
40
136
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -13,7 +13,7 @@ module YamlEnumeration
13
13
 
14
14
  def self.load_values(filename)
15
15
  file = File.join(Rails.root, 'db', 'enumerations', "#{filename}.yml")
16
- YAML.load(ERB.new(File.read(file)).result).values.each do |data|
16
+ remove_exclusions(YAML.load(ERB.new(File.read(file)).result)).values.each do |data|
17
17
  value data.symbolize_keys
18
18
  end
19
19
  end
@@ -73,6 +73,7 @@ module YamlEnumeration
73
73
 
74
74
  class << self
75
75
  def all
76
+ return [] unless values
76
77
  # values.keys.map {|id| new(id)} # override this if you do not want to keep associated obj loaded against
77
78
  self.all_instances ||= values.keys.map {|id| new(id)}
78
79
  end
@@ -89,10 +90,51 @@ module YamlEnumeration
89
90
  end
90
91
  alias_method :find_by_id, :find
91
92
 
93
+ def find_by(column, value=nil)
94
+ case column
95
+ when Hash
96
+ raise "Hashes with multiple filters are not support so far" if column.keys.size > 1
97
+
98
+ key = column.keys[0]
99
+ all.find {|item| item.send(key) == column[key] }
100
+ else
101
+ all.find {|item| item.send(column) == value }
102
+ end
103
+ end
104
+
105
+ def find_all_by(column, value=nil)
106
+ case column
107
+ when Hash
108
+ raise "Hashes with multiple filters are not support so far" if column.keys.size > 1
109
+
110
+ key = column.keys[0]
111
+ all.select {|item| item.send(key) == column[key] }
112
+ else
113
+ all.select {|item| item.send(column) == value }
114
+ end
115
+ end
116
+
92
117
  def find_by_type(type)
93
118
  all.detect {|a| a.type == type.to_s}
94
119
  end
95
120
 
121
+ def where(matches)
122
+ raise "Only the hash form of where is supported, not #{matches}" unless matches.is_a?(Hash)
123
+
124
+ all.select do |item|
125
+ matches.all? {|k,v| item.send(k) == v }
126
+ end
127
+ end
128
+
129
+ # Define singleton methods .BLAH for each type
130
+ def with_named_items(column = :type)
131
+ all.each do |item|
132
+ define_singleton_method "#{item.send(column).upcase}" do
133
+ find_by(column, item.send(column).downcase)
134
+ end
135
+ end
136
+ end
137
+
96
138
  protected :new
97
139
  end # class << self
98
140
 
@@ -102,5 +144,10 @@ module YamlEnumeration
102
144
  self.id = id
103
145
  end
104
146
 
147
+ def self.remove_exclusions(result)
148
+ keys_to_remove = result.keys.select {|key| key =~ /.*_exclude_from_enumeration$/ }
149
+ result.except(*keys_to_remove)
150
+ end
151
+
105
152
  end # Enumeration
106
153
  end
@@ -1,3 +1,3 @@
1
1
  module YamlEnumeration
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -10,7 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["boettger@mt7.de"]
11
11
 
12
12
  spec.summary = %q{Create ActiveRecord enumerations based on YAML files.}
13
- spec.description = %q{Create ActiveRecord enumerations based on YAML files.}
13
+ spec.description = %q{Create classes which work like ActiveRecord classes,
14
+ but are defined as fixed enumerations based on YAML files.}
14
15
  spec.homepage = "https://github.com/alto/yaml_enumeration"
15
16
  spec.license = "MIT"
16
17
 
@@ -19,11 +20,11 @@ Gem::Specification.new do |spec|
19
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
21
  spec.require_paths = ["lib"]
21
22
 
22
- spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "bundler"
23
24
  spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "minitest"
25
+ spec.add_development_dependency "maxitest"
25
26
  spec.add_development_dependency "shoulda-context"
26
27
 
27
- spec.add_dependency "activesupport"
28
- spec.add_dependency "railties"
28
+ spec.add_dependency "activesupport", '>= 6'
29
+ spec.add_dependency "railties", '>= 6'
29
30
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_enumeration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thorsten Boettger
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-05 00:00:00.000000000 Z
11
+ date: 2022-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.12'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.12'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest
42
+ name: maxitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -72,29 +72,31 @@ dependencies:
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '6'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '6'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: railties
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: '6'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
97
- description: Create ActiveRecord enumerations based on YAML files.
96
+ version: '6'
97
+ description: |-
98
+ Create classes which work like ActiveRecord classes,
99
+ but are defined as fixed enumerations based on YAML files.
98
100
  email:
99
101
  - boettger@mt7.de
100
102
  executables: []
@@ -103,6 +105,7 @@ extra_rdoc_files: []
103
105
  files:
104
106
  - ".gitignore"
105
107
  - ".travis.yml"
108
+ - CHANGELOG.md
106
109
  - CODE_OF_CONDUCT.md
107
110
  - Gemfile
108
111
  - LICENSE.txt
@@ -120,7 +123,7 @@ homepage: https://github.com/alto/yaml_enumeration
120
123
  licenses:
121
124
  - MIT
122
125
  metadata: {}
123
- post_install_message:
126
+ post_install_message:
124
127
  rdoc_options: []
125
128
  require_paths:
126
129
  - lib
@@ -135,9 +138,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
138
  - !ruby/object:Gem::Version
136
139
  version: '0'
137
140
  requirements: []
138
- rubyforge_project:
139
- rubygems_version: 2.5.1
140
- signing_key:
141
+ rubygems_version: 3.2.17
142
+ signing_key:
141
143
  specification_version: 4
142
144
  summary: Create ActiveRecord enumerations based on YAML files.
143
145
  test_files: []