fixed_record 0.1.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 517a804c21cbd7ebe17493341ae42ddef70193953f5d3a40026907f470088eeb
4
+ data.tar.gz: 8d7f1d298b25419ef38b457b0a6284dcf6c3b385dfa20b272d98a09f3281b607
5
+ SHA512:
6
+ metadata.gz: ac9c2c0ba7632aa0edf97a49f5a4e95377f99f5aa46ed28ec4de7a91560f0aa4e96e1edbbbaf203f9623711c5baaf57ce3ac4d7a99f8d55876a2435954dd091f
7
+ data.tar.gz: 4e76dc6a252b5e71c6932d423ab2c274bdb9d975004879d97409a421ea5baeabee15dc101cd321c754576bbef126eaa70f6017cd262d125a892d3c4735d31208
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # Compiled gems
14
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.4
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fixed_record.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fixed_record (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.2)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.4)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 2.0)
30
+ fixed_record!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 2.0.2
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # FixedRecord
2
+
3
+ FixedRecord provides ActiveRecord-like read-only access to a set of records
4
+ described in a YAML file.
5
+
6
+ Why is this useful? Occasionally you have tabular data which hardly ever
7
+ changes and can easily be edited by hand. Although this code could be placed in a database, it's not worth the overheads (loading a database, maintaining database code).
8
+
9
+ It's quicker and simpler to implement this as an array of objects in a YAML file.
10
+
11
+ See the Usage section below
12
+
13
+
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'fixed_record'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install fixed_record
30
+
31
+ ## Usage
32
+
33
+ Create a YAML file defining an array of records like this:
34
+
35
+ ```yaml
36
+ -
37
+ name: Risky Thinking
38
+ url: https://www.riskythinking.com/
39
+ -
40
+ name: Krebs on Security
41
+ url: https://krebsonsecurity.com/
42
+
43
+ ```
44
+
45
+ Then to load these, create a class
46
+
47
+ ```ruby
48
+ require 'FixedRecord'
49
+
50
+ class MyFavoriteWebsite < FixedRecord
51
+ data "#{Rails.root}/data/my_favorite_websites.yml"
52
+
53
+ # Return hostname of url for company
54
+ def hostname
55
+ URI.parse(url).host
56
+ end
57
+
58
+ end
59
+ ```
60
+ The collection can then be enumerated as required:
61
+
62
+ ```ruby
63
+ MyFavoriteWebsite.each do |w|
64
+ puts w.name
65
+ puts w.hostname
66
+ end
67
+ ```
68
+ Or can be accessed as an array:
69
+
70
+ ```ruby
71
+ MyFavoriteWebiste.all
72
+ ```
73
+ A count of the number of records is available:
74
+
75
+ ```ruby
76
+ puts MyFavoriteWebiste.count
77
+ ```
78
+
79
+ The class also includes the `Enumerable` module.
80
+
81
+ Some basic sanity checks are performed on the YAML file to catch common errors:
82
+
83
+ * It must define a non-empty array of records
84
+ * All records must have the same set of attributes
85
+
86
+ An `ArgumentError` exception will be thrown if any errors are detected.
87
+
88
+ Additional validations can be performed by overriding the `validate_yaml` and
89
+ `validate_item` class functions.
90
+
91
+
92
+
93
+
94
+
95
+ ## Development
96
+
97
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
98
+
99
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `fixed_record.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fixed_record.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fixed_record"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "fixed_record"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "fixed_record"
7
+ spec.version = FixedRecord::VERSION
8
+ spec.authors = ["Mike Bell"]
9
+ spec.email = ["mbell@albionresearch.com"]
10
+
11
+ spec.summary = %q{Provide ActiveRecord like read-only access to a small dataset stored in a YAML file}
12
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
13
+ spec.homepage = "https://github.com/m-z-b/fixed_record/blob/master/README.md"
14
+ spec.license = "MIT"
15
+
16
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/m-z-b/fixed_record"
20
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
@@ -0,0 +1,74 @@
1
+
2
+ require 'yaml'
3
+
4
+ class FixedRecord
5
+ VERSION = "0.1.2"
6
+
7
+ # Lazy load data from given filename
8
+ # creating accessors for top level attributes
9
+ def self.data( filename )
10
+ class_eval %Q{
11
+ class << self
12
+ include Enumerable
13
+ end
14
+
15
+ @@items = nil
16
+
17
+ def self.filename
18
+ %Q{#{filename}}
19
+ end
20
+
21
+ def self.all
22
+ load!
23
+ @@items
24
+ end
25
+
26
+ def self.each( &block )
27
+ all.each(&block)
28
+ end
29
+
30
+ def self.count
31
+ all.count
32
+ end
33
+
34
+ def self.load!
35
+ if @@items.nil?
36
+ y = YAML.load_file( filename )
37
+ validate_yaml( y )
38
+ valid_keys = y.first.keys
39
+ valid_keys.each do |k|
40
+ define_method( k.to_sym) { @values[k] }
41
+ end
42
+
43
+ @@items = y.map.with_index do |values,i|
44
+ validate_item( valid_keys, values, i )
45
+ r = new
46
+ r.instance_variable_set( :@values, values )
47
+ r
48
+ end
49
+ end
50
+ end
51
+ }
52
+ end
53
+
54
+ # Validate the top level of the data structure returned
55
+ def self.validate_yaml( y )
56
+ unless y.is_a?(Array) && y.length > 0
57
+ throw ArgumentError.new "#{filename} does not contain an array of items"
58
+ end
59
+ end
60
+
61
+ # Validate a hash of name -> value
62
+ def self.validate_item( keys, hash, index )
63
+ raise ArgumentError, "#{filename} item #{index+1} should be name value pairs" unless hash.is_a?(Hash)
64
+ raise ArgumentError, "#{filename} item #{index+1} has wrong number of values" if keys.count != hash.count
65
+ keys.each do |name|
66
+ unless hash.has_key? name
67
+ raise ArgumentError, "#{filename} item #{index+1} is missing value for '#{name}'"
68
+ end
69
+ end
70
+ end
71
+
72
+
73
+
74
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fixed_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Mike Bell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - mbell@albionresearch.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - fixed_record.gemspec
72
+ - lib/fixed_record.rb
73
+ homepage: https://github.com/m-z-b/fixed_record/blob/master/README.md
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ homepage_uri: https://github.com/m-z-b/fixed_record/blob/master/README.md
78
+ source_code_uri: https://github.com/m-z-b/fixed_record
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.0.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Provide ActiveRecord like read-only access to a small dataset stored in a
98
+ YAML file
99
+ test_files: []