trailblazer-declarative 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 243dc1410b80f1a84c5d6a85b7e933d64cb9fee257cf2b5fd63d76c791246e53
4
+ data.tar.gz: 6186c3c33419a8692bc91dbe3a29fef6b343cee22ccf65a0c429a18e62a48d9d
5
+ SHA512:
6
+ metadata.gz: 1f0f638638dbd13ef1c7fa8517bba2e6b6d469f5b56df83d93b162ccbcf3efd0d5d6cbb4c8637d535c411a71128a5082ed3da01b80b227c06d15a7ddaab3da1b
7
+ data.tar.gz: b29bc27efa77142065687093bea0105f0a1c96cc27f576cf8c4ad83a4811348627d780b8a4407be3ecbf0941d59aeb452c51b7cc370e3175ad01f0dd44336ab3
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.11
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.0.1]
2
+
3
+ - Initial release into an unsuspecting world.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in trailblazer-declarative.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # Trailblazer::Declarative
2
+
3
+
4
+ ## State
5
+
6
+ `State` is a minimalistic state handling object, similar to a hash with
7
+ additional "inheritance" logic via `State#copy`. Every field in `state`
8
+ can have a specific copying strategy, ranging from simply referencing the
9
+ original object to subclassing.
10
+
11
+ ```ruby
12
+ state = Declarative.State(key: ["value", ...])
13
+ state.add!
14
+ state.update!
15
+ state.get
16
+ state.copy # inheritance
17
+ ```
18
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,69 @@
1
+ module Trailblazer
2
+ module Declarative
3
+ def self.Schema(&block)
4
+ Class.new do
5
+ extend Trailblazer::Declarative::Schema
6
+ instance_exec(&block)
7
+ end
8
+ end
9
+ # Include this to maintain inheritable, nested schemas with ::defaults and
10
+ # ::feature the way we have it in Representable, Reform, and Disposable.
11
+ #
12
+ # The schema with its defnitions will be kept in ::definitions.
13
+ #
14
+ # Requirements to includer: ::default_nested_class, override building with ::nested_builder.
15
+ module Schema
16
+ def self.extended(extender)
17
+ extender.extend DSL # ::property
18
+ # extender.extend Feature # ::feature
19
+ # extender.extend Heritage::DSL # ::heritage
20
+ # extender.extend Heritage::Inherited # ::included
21
+
22
+ extender.initialize_state!() # replaces {@definitions ||= Definitions.new(definition_class)}
23
+ end
24
+
25
+
26
+ # class << self
27
+ # end
28
+
29
+ module DSL
30
+ # def initialize_state!()
31
+ # @state = State.new
32
+ # end
33
+
34
+ # # @return State
35
+ # def update_state!(key, value)
36
+ # @state = @state.merge(key => value)
37
+ # end
38
+
39
+ def property(name, options={}, &block)
40
+ # heritage.record(:property, name, options, &block)
41
+
42
+ # build_definition(name, options, &block)
43
+ end
44
+ end
45
+
46
+ module State
47
+ def initialize_state!(tuples)
48
+ @state = Declarative.State(tuples)
49
+ end
50
+
51
+ def state
52
+ @state
53
+ end
54
+
55
+ module Inherited
56
+ # DISCUSS: this is *not* a class method and will not be executed when extended the first time.
57
+ def inherited(subclass)
58
+ super
59
+
60
+ inherited_fields = state.copy_fields(subclass: subclass)
61
+
62
+ subclass.initialize_state!(inherited_fields) # TODO: discuss, should this be done "in" the subclass rather than here?
63
+ end
64
+ end
65
+ end # Schema::State
66
+ end # Schema
67
+
68
+ end
69
+ end
@@ -0,0 +1,65 @@
1
+ module Trailblazer
2
+ module Declarative
3
+ # Class-wide configuration data
4
+ def self.State(tuples={})
5
+ state = State.new
6
+ tuples.each { |path, (value, options)| state.add!(path, value, **options) }
7
+ state
8
+ end
9
+
10
+ class State # FIXME: who is providing the immutable API?
11
+ def self.dup(value, **) # DISCUSS: should that be here?
12
+ value.dup
13
+ end
14
+
15
+ def self.subclass(value, **)
16
+ Class.new(value)
17
+ end
18
+
19
+ def initialize
20
+ @fields = {}
21
+ @field_options = {}
22
+ end
23
+
24
+ def add!(path, value, copy: State.method(:dup))
25
+ @fields[path] = value
26
+ @field_options[path] = {copy: copy}
27
+ self
28
+ end
29
+
30
+ # Tries to retrieve {path}, if it exists {block} is called
31
+ # and receives the old value.
32
+ # The return value of the block will be the new value.
33
+ def update!(path, &block)
34
+ value = get(path)
35
+ new_value = yield(value, **{})
36
+ set!(path, new_value)
37
+ end
38
+
39
+ def get(path)
40
+ @fields.fetch(path)
41
+ end
42
+
43
+ # @private
44
+ def set!(path, value)
45
+ @fields[path] = value
46
+ end
47
+
48
+ def copy_fields(**options)
49
+ inherited_fields = @fields.collect do |path, value|
50
+ path_options = @field_options.fetch(path)
51
+ inherited_value = path_options.fetch(:copy).(value, **options)
52
+
53
+ [path, [inherited_value, path_options]]
54
+ end.to_h
55
+ end
56
+
57
+ # DISCUSS: do we need it?
58
+ def copy(**options) # DISCUSS: make class method?
59
+ inherited_fields = copy_fields(**options)
60
+
61
+ Declarative.State(inherited_fields)
62
+ end
63
+ end # State
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ module Trailblazer
2
+ module Declarative
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ require_relative "declarative/version"
2
+
3
+ module Trailblazer
4
+ module Declarative
5
+ class Error < StandardError; end
6
+ # Your code goes here...
7
+ end
8
+ end
9
+
10
+ require_relative "declarative/state"
11
+ require_relative "declarative/schema"
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/trailblazer/declarative/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "trailblazer-declarative"
7
+ spec.version = Trailblazer::Declarative::VERSION
8
+ spec.authors = ["Nick Sutterer"]
9
+ spec.email = ["apotonick@gmail.com"]
10
+
11
+ spec.summary = "Generic DSL providing schemas and inheritance."
12
+ spec.homepage = "https://github.com/trailblazer/trailblazer-declarative"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/trailblazer/trailblazer-declarative"
17
+ spec.metadata["changelog_uri"] = "https://github.com/trailblazer/trailblazer-declarative/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ # Uncomment to register a new dependency of your gem
29
+ # spec.add_dependency "example-gem", "~> 1.0"
30
+ spec.add_development_dependency "minitest-line"
31
+
32
+ # For more information and examples about making a new gem, checkout our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trailblazer-declarative
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nick Sutterer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest-line
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - apotonick@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".github/workflows/main.yml"
35
+ - ".gitignore"
36
+ - CHANGELOG.md
37
+ - Gemfile
38
+ - README.md
39
+ - Rakefile
40
+ - lib/trailblazer/declarative.rb
41
+ - lib/trailblazer/declarative/schema.rb
42
+ - lib/trailblazer/declarative/state.rb
43
+ - lib/trailblazer/declarative/version.rb
44
+ - trailblazer-declarative.gemspec
45
+ homepage: https://github.com/trailblazer/trailblazer-declarative
46
+ licenses: []
47
+ metadata:
48
+ homepage_uri: https://github.com/trailblazer/trailblazer-declarative
49
+ source_code_uri: https://github.com/trailblazer/trailblazer-declarative
50
+ changelog_uri: https://github.com/trailblazer/trailblazer-declarative/CHANGELOG.md
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.4.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.2.3
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Generic DSL providing schemas and inheritance.
70
+ test_files: []