Skip to content

Commit

Permalink
#72 copy
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 16, 2024
1 parent 8ba3664 commit 6c9d890
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,33 @@ It's a collection of tools for
You are not supposed to use it directly, but only in a combination
with other tools of Zerocracy.

The following tools runs a block:

* `Fbe.regularly` runs a block of code every X days.
* `Fbe.conclude` runs a block on every fact from a query.
* `Fbe.iterate` runs a block on each repository, until it's time to stop.
* `Fbe.repeatedly` runs a block of code every X hours, leaving
a fact-marker in the factbase.

These tools help manage facts:

* `Fbe.fb` makes an entry point to the factbase.
* `Fbe.overwrite` changes a property in a fact to another value by deleting
the fact first, and then creating a new similar fact with all previous
properties but one changed.
* `Fbe.pmp` takes a PMP-related property by the area.

They help with formatting:

* `Fbe.who` formats user name.
* `Fbe.issue` formats issue number.
* `Fbe.award` calculates award by the policy.
* `Fbe.sec` formats seconds.

They help with external connections:

* `Fbe.octo` connects to GitHub API.

## How to contribute

Read
Expand Down
41 changes: 41 additions & 0 deletions lib/fbe/copy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# MIT License
#
# Copyright (c) 2024 Zerocracy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require_relative '../fbe'
require_relative 'fb'

# Make a copy of a fact, moving all properties to a new fact.
#
# @param [Factbase::Fact] source The source
# @param [Factbase::Fact] target The targer
def Fbe.copy(source, target)
raise 'The source is nil' if source.nil?
raise 'The target is nil' if target.nil?
source.all_properties.each do |k|
next unless target[k].nil?
source[k].each do |v|
target.send(:"#{k}=", v)
end
end
end
2 changes: 2 additions & 0 deletions lib/fbe/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require_relative '../fbe'

# The module.
module Fbe::Middleware
# empty
Expand Down
46 changes: 46 additions & 0 deletions test/fbe/test_copy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

# MIT License
#
# Copyright (c) 2024 Zerocracy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'minitest/autorun'
require 'factbase'
require_relative '../test__helper'
require_relative '../../lib/fbe/copy'

# Test.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2024 Zerocracy
# License:: MIT
class TestCopy < Minitest::Test
def test_simple_copy
fb = Factbase.new
f1 = fb.insert
f1._id = 1
f1.foo = 42
f2 = fb.insert
f2._id = 2
Fbe.copy(f1, f2)
assert_equal(2, f2._id)
assert_equal(42, f2.foo)
end
end

0 comments on commit 6c9d890

Please sign in to comment.