staticmodel 1.0.0

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
+ SHA1:
3
+ metadata.gz: 89e840ef250a7502da3f788a2c211db15cd2d914
4
+ data.tar.gz: a06c94ca8374d4ef8cd5b08aeba68f0b8e429a11
5
+ SHA512:
6
+ metadata.gz: abd7fd8933701a9edb5c67f602311e449afc623adc7e768ca5784fa5fc63df9a8218d35836ddbd0b1f227eddd43194887d15267d133e11051d83dbaa794bfeac
7
+ data.tar.gz: 08e25d28cb2262d02ced4b8966ac8cd9b920bdc8fe844eca3f0fac3a6f8e3581ae68ee5f6d60b5982a930dee9a24bd122ea655b0fa3236c42455e76331f5c669
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Fernando Tapia Rico
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # StaticModel
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/staticmodel`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "staticmodel"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install staticmodel
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/staticmodel. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,11 @@
1
+ require "active_support/inflector"
2
+ require "virtus"
3
+ require "yaml"
4
+
5
+ require "static_model/base"
6
+ require "static_model/configuration"
7
+ require "static_model/errors"
8
+
9
+ module StaticModel
10
+ autoload :Shortcut, "static_model/shortcut"
11
+ end
@@ -0,0 +1,68 @@
1
+ module StaticModel
2
+ class Base
3
+ include Virtus.model
4
+
5
+ def id
6
+ defined?(super) ? super : __send__(primary_key)
7
+ end
8
+
9
+ def self.primary_key
10
+ @primary_key ||= superclass == Base ? "id".freeze : superclass.primary_key
11
+ end
12
+
13
+ def self.primary_key=(primary_key)
14
+ @primary_key = primary_key
15
+ end
16
+
17
+ def self.data_path
18
+ return if self == Base
19
+
20
+ @data_path ||= superclass.data_path || default_data_path
21
+ end
22
+
23
+ def self.data_path=(data_path)
24
+ @data_path = data_path
25
+ end
26
+
27
+ def self.default_data_path
28
+ File.join(StaticModel.base_data_path, "#{name.demodulize.tableize}.yml")
29
+ end
30
+
31
+ def self.all
32
+ @records ||= load_records.freeze
33
+ end
34
+
35
+ def self.where(criteria = {})
36
+ all.select do |record|
37
+ criteria.all? do |attribute, value|
38
+ record.respond_to?(attribute) && record.__send__(attribute) == value
39
+ end
40
+ end
41
+ end
42
+
43
+ def self.find(id)
44
+ find_by!(primary_key => id)
45
+ end
46
+
47
+ def self.find_by!(*args)
48
+ find_by(*args) || raise(NotFound)
49
+ end
50
+
51
+ def self.find_by(criteria = {})
52
+ where(criteria).first
53
+ end
54
+
55
+ def self.exist?(criteria = {})
56
+ if criteria.is_a?(Hash)
57
+ !!find_by(criteria)
58
+ else
59
+ !!find_by(primary_key => criteria)
60
+ end
61
+ end
62
+
63
+ def self.load_records
64
+ data = YAML.load_file(data_path)
65
+ data.map { |record_attributes| new(record_attributes).freeze }
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,25 @@
1
+ module StaticModel
2
+ class Configuration
3
+ attr_accessor :base_data_path
4
+ end
5
+
6
+ def self.configure
7
+ yield(configuration)
8
+ end
9
+
10
+ def self.configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def self.method_missing(method_sym, *arguments, &block)
15
+ if configuration.respond_to?(method_sym)
16
+ configuration.__send__(method_sym)
17
+ else
18
+ super
19
+ end
20
+ end
21
+
22
+ def self.respond_to?(method_sym, include_private = false)
23
+ configuration.respond_to?(method_sym, include_private) || super
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ module StaticModel
2
+ class Error < StandardError; end
3
+ class NotFound < Error; end
4
+ end
@@ -0,0 +1,21 @@
1
+ module StaticModel
2
+ module Shortcut
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def method_missing(method_sym, *arguments, &block)
9
+ if exist?(method_sym.to_s)
10
+ find(method_sym.to_s)
11
+ else
12
+ super
13
+ end
14
+ end
15
+
16
+ def respond_to?(method_sym, include_private = false)
17
+ exist?(method_sym.to_s) || super
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ module StaticModel
2
+ VERSION = [
3
+ VERSION_MAJOR = 1,
4
+ VERSION_MINOR = 0,
5
+ VERSION_TINY = 0,
6
+ VERSION_PRE = nil
7
+ ].compact.join(".")
8
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: staticmodel
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Tapia Rico
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: virtus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Read-only ActiveRecord-like interface to query static YAML files.
84
+ email:
85
+ - fertapric@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.md
92
+ - lib/static_model.rb
93
+ - lib/static_model/base.rb
94
+ - lib/static_model/configuration.rb
95
+ - lib/static_model/errors.rb
96
+ - lib/static_model/shortcut.rb
97
+ - lib/static_model/version.rb
98
+ homepage: https://github.com/fertapric/staticmodel
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.4.5
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Static Model
122
+ test_files: []