yaml_enumeration 0.1.0 → 0.2.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: 939fc41bc3dae93b1f734a20300fb3b28e8a06d39d53606dea2ad550c6fc329a
4
+ data.tar.gz: caf13ede07f9ddd88489e73b53f13e95b0b5f62129ad79130fe5c53a04a901f9
5
5
  SHA512:
6
- metadata.gz: 0a9501189bf47b9fe78c93d1b2020dfc614429be7eac9a93a49b3495d3930cca932ffbe11bff6e515a7d7092da1727c73c466abc43cbea10df2e2241e6f937d4
7
- data.tar.gz: 6d0a83d915f6eeb99d1df80201c7a26c9145f7277880cece0f96f3a9955225d676828de8d7883ae16ce2b8cedbd1449e90efba1be5ce43481a90698a2b46b396
6
+ metadata.gz: 268c4a9f2aaeb23b5b5692c0d2ae0824c7701eed23fc076c4beb42a90f4a0336fee9bce62b324e5e3e2cf31ced72a23fba6aceaa85e40cd9f88171bebdfc6e54
7
+ data.tar.gz: ea9a72ecb8bf7214e63ded302257f73e16dac0d289b909f4b377a0d5a43f336dbac72836633871a1627f7de6352c9dc19bfe05c8210b62ec3db54838ee86fdea
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.2.0
4
+
5
+ * support _ActiveRecord_ methods `where`, `find_by` [#1])(https://github.com/alto/yaml_enumeration/pull/1), thanks to [@richdrich](https://github.com/richdrich)
6
+
7
+ ## 0.1.0
8
+
9
+ * 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,68 @@ 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
+ ## Accessing members
84
+
85
+ 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`.
86
+
87
+ By passing a column name, e.g:
88
+ ```
89
+ with_named_items(:code)
90
+ ```
91
+ that column will be used, e.g: `Country.AU` and `Country.NZ`
26
92
 
27
93
  ## Development
28
94
 
@@ -32,10 +98,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
98
 
33
99
  ## Contributing
34
100
 
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.
101
+ 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
102
 
37
103
 
38
104
  ## License
39
105
 
40
106
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -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,31 @@ module YamlEnumeration
89
90
  end
90
91
  alias_method :find_by_id, :find
91
92
 
93
+ def find_by(column, value)
94
+ all.find {|item| item.send(column) == value }
95
+ end
96
+
92
97
  def find_by_type(type)
93
98
  all.detect {|a| a.type == type.to_s}
94
99
  end
95
100
 
101
+ def where(matches)
102
+ raise "Only the hash form of where is supported, not #{matches}" unless matches.is_a?(Hash)
103
+
104
+ all.select do |item|
105
+ matches.all? {|k,v| item.send(k) == v }
106
+ end
107
+ end
108
+
109
+ # Define singleton methods .BLAH for each type
110
+ def with_named_items(column = :type)
111
+ all.each do |item|
112
+ define_singleton_method "#{item.send(column).upcase}" do
113
+ find_by(column, item.send(column).downcase)
114
+ end
115
+ end
116
+ end
117
+
96
118
  protected :new
97
119
  end # class << self
98
120
 
@@ -1,3 +1,3 @@
1
1
  module YamlEnumeration
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.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
 
@@ -21,9 +22,9 @@ Gem::Specification.new do |spec|
21
22
 
22
23
  spec.add_development_dependency "bundler", "~> 1.12"
23
24
  spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "minitest"
25
+ spec.add_development_dependency "maxitest", '~> 1'
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", '>= 4'
29
+ spec.add_dependency "railties", '>= 4'
29
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
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.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thorsten Boettger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-05 00:00:00.000000000 Z
11
+ date: 2018-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,19 +39,19 @@ 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
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '1'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: shoulda-context
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -72,29 +72,31 @@ dependencies:
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '4'
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: '4'
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: '4'
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: '4'
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
@@ -136,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
139
  version: '0'
137
140
  requirements: []
138
141
  rubyforge_project:
139
- rubygems_version: 2.5.1
142
+ rubygems_version: 2.7.6
140
143
  signing_key:
141
144
  specification_version: 4
142
145
  summary: Create ActiveRecord enumerations based on YAML files.