risa 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.
data/lib/risa.rb ADDED
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'json'
5
+ require 'date'
6
+ require_relative 'risa/context'
7
+ require_relative 'risa/instance'
8
+ require_relative 'risa/page'
9
+ require_relative 'risa/query'
10
+ require_relative 'risa/relations'
11
+
12
+ module Risa
13
+ class DataFileError < StandardError; end
14
+ class ScopeError < StandardError; end
15
+ class ItemMethodError < StandardError; end
16
+
17
+ class << self
18
+ def configure(data_path: 'data')
19
+ @data_path = data_path
20
+ end
21
+
22
+ def load_from(data_dir)
23
+ absolute_dir = File.expand_path(data_dir, Dir.pwd)
24
+
25
+ Dir[File.join(absolute_dir, '**', '*.rb')].sort.each do |file|
26
+ load file # Use load, not require - allows reloading
27
+ end
28
+ end
29
+
30
+ def reload_from(data_dir)
31
+ Risa.reload
32
+ load_from(data_dir)
33
+ end
34
+
35
+ def define(model_name, &block)
36
+ context = DefinitionContext.new(model_name, @data_path || 'data')
37
+ context.instance_eval(&block)
38
+
39
+ @collections ||= {}
40
+ @collections[model_name.to_sym] = {
41
+ data: context.loaded_data,
42
+ scopes: context.scopes,
43
+ relations: context.relations # Make sure this line exists
44
+ }
45
+ end
46
+
47
+ def present(model_name, &block)
48
+ presenter_module = Module.new
49
+ presenter_module.module_eval(&block)
50
+ (@presenter_modules ||= {})[model_name.to_sym] = presenter_module
51
+ end
52
+
53
+ def presenter_for(model_name)
54
+ (@presenter_modules || {})[model_name.to_sym]
55
+ end
56
+
57
+ def query(model_name)
58
+ model_name = model_name.to_sym
59
+ collection = @collections[model_name]
60
+
61
+ unless collection
62
+ raise "Collection #{model_name} not defined. Use Risa.define :#{model_name} to define it."
63
+ end
64
+
65
+ Query.new(
66
+ model_name,
67
+ collection[:data],
68
+ collection[:scopes],
69
+ collection[:relations]
70
+ )
71
+ end
72
+
73
+ def reload
74
+ @collections = {}
75
+ @presenter_modules = {}
76
+ end
77
+
78
+ def defined_models
79
+ (@collections || {}).keys
80
+ end
81
+
82
+ def relations_for(model_name)
83
+ (@collections || {}).dig(model_name.to_sym, :relations) || {}
84
+ end
85
+ end
86
+ end
87
+
88
+ def rs(model_name)
89
+ Risa.query(model_name)
90
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: risa
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Steven Garcia
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Turn associative arrays into rich, queryable data for ruby applications
13
+ email:
14
+ - stevendgarcia@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - CHANGELOG.md
20
+ - CODE_OF_CONDUCT.md
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
24
+ - lib/risa.rb
25
+ - lib/risa/context.rb
26
+ - lib/risa/instance.rb
27
+ - lib/risa/page.rb
28
+ - lib/risa/query.rb
29
+ - lib/risa/relations.rb
30
+ - lib/risa/version.rb
31
+ homepage: https://github.com/activestylus/risa.git
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ allowed_push_host: https://rubygems.org
36
+ homepage_uri: https://github.com/activestylus/risa.git
37
+ source_code_uri: https://github.com/activestylus/risa.git
38
+ changelog_uri: https://github.com/activestylus/risa/CHANGELOG.md
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.0.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.6.9
54
+ specification_version: 4
55
+ summary: Read-Only ORM for Ruby Hashes
56
+ test_files: []