Skip to content

Commit

Permalink
specs
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Sep 7, 2020
0 parents commit d6ee1e4
Show file tree
Hide file tree
Showing 18 changed files with 786 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Test
on:
pull_request:
push: { branches: master }

jobs:
test:
name: Ruby ${{ matrix.ruby }}

runs-on: ubuntu-latest

strategy:
matrix: { ruby: ['2.4', '2.5', '2.6', '2.7'] }

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Prepare bundle cache
uses: actions/cache@v1
with:
path: vendor/bundle
key: ${{ runner.os }}-${{ matrix.ruby }}-bundle

- name: Set up Ruby
uses: actions/setup-ruby@v1
with: { ruby-version: '${{ matrix.ruby }}' }

- name: Bundle install
run: |
gem install bundler
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3
- name: Run tests
run: bundle exec rspec
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.gem
/.yardoc
/coverage
/db
/debug.rb
/dev
/doc
/Gemfile.lock
/gems
/spec/tmp
/tmp
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--format documentation
--fail-fast
15 changes: 15 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
source "https://rubygems.org"

gem 'rdoc', require: false
gem 'runfile', require: false
gem 'runfile-tasks', require: false
gem 'yard', require: false

gem 'byebug'
gem 'lp'
gem 'requires'
gem 'rspec'
gem 'rspec_approvals'
gem 'simplecov'

gemspec
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Danny Ben Shitrit

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.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ActiveCabinet
==================================================

[![Gem Version](https://badge.fury.io/rb/active_cabinet.svg)](https://badge.fury.io/rb/active_cabinet)
[![Build Status](https://travis-ci.com/DannyBen/active_cabinet.svg?branch=master)](https://travis-ci.com/DannyBen/active_cabinet)
[![Maintainability](https://api.codeclimate.com/v1/badges/.../maintainability)](https://codeclimate.com/github/DannyBen/active_cabinet/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/.../test_coverage)](https://codeclimate.com/github/DannyBen/active_cabinet/test_coverage)

---

Database-like interface for HashCabinet

---

Installation
--------------------------------------------------

$ gem install active_cabinet



Usage
--------------------------------------------------

TODO
24 changes: 24 additions & 0 deletions Runfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "runfile-tasks"
require "byebug"
require "lp"
require_relative 'lib/active_cabinet'

title "ActiveCabinet Developer Toolbelt"
summary "Runfile tasks for building the ActiveCabinet gem"
version ActiveCabinet::VERSION

RunfileTasks::RubyGems.all 'active_cabinet'
RunfileTasks::Testing.rspec
RunfileTasks::Docs.rdoc

help "Run YARD server"
action :yard do
run "yard server -p3000 -B0.0.0.0 -r"
end

help "Run interactive console"
action :console, :c do
run "bundle exec bin/console"
end

require_relative 'debug.rb' if File.exist? 'debug.rb'
20 changes: 20 additions & 0 deletions active_cabinet.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'date'
require 'active_cabinet/version'

Gem::Specification.new do |s|
s.name = 'active_cabinet'
s.version = ActiveCabinet::VERSION
s.date = Date.today.to_s
s.summary = "Database-like interface for HashCabinet"
s.description = "Database-like interface for HashCabinet"
s.authors = ["Danny Ben Shitrit"]
s.email = '[email protected]'
s.files = Dir['README.md', 'lib/**/*.*']
s.homepage = 'https://github.com/dannyben/active_cabinet'
s.license = 'MIT'
s.required_ruby_version = ">= 2.4.0"

s.add_runtime_dependency 'hash_cabinet', '~> 0.1'
end
9 changes: 9 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby

require "irb"
require "irb/completion"
require "active_cabinet"

include ActiveCabinet

IRB.start
109 changes: 109 additions & 0 deletions lib/active_cabinet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
require 'hash_cabinet'
require 'active_cabinet/metaclass'

class ActiveCabinet
attr_reader :attributes, :error

def initialize(attributes = {})
@attributes = attributes.transform_keys(&:to_sym)
end

def all_attributes
self.class.all_attributes
end

def method_missing(method_name, *args, &blk)
name = method_name
return attributes[name] if attributes.has_key? name

suffix = nil

if name.end_with?('=', '?')
suffix = name[-1]
name = name[0..-2].to_sym
end

case suffix
when "="
attributes[name] = args.first

when "?"
!!attributes[name]

else
super

end
end

def optional_attributes
self.class.optional_attributes
end

def reload
return nil unless saved?
update cabinet[id]
self
end

def required_attributes
self.class.required_attributes
end

def respond_to_missing?(method_name, include_private = false)
name = method_name
name = name[0..-2].to_sym if name.end_with?('=', '?')
attributes.has_key?(name) || super
end

def save
if valid?
cabinet[id] = attributes
self
else
false
end
end

def saved?
cabinet.key? id
end

def to_json
attributes.to_json
end

def update(new_attributes)
@attributes = attributes.merge new_attributes
end

def update!(new_attributes)
update new_attributes
save
end

def valid?
missing_keys = required_attributes - attributes.keys
if missing_keys.any?
@error = "missing required attributes: #{missing_keys}"
return false
end

if optional_attributes.any?
invalid_keys = attributes.keys - all_attributes
if invalid_keys.any?
@error = "invalid attributes: #{invalid_keys}"
return false
end
end

true
end

protected

def cabinet
self.class.cabinet
end

end
11 changes: 11 additions & 0 deletions lib/active_cabinet/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ActiveCabinet
class Config
class << self
attr_writer :dir

def dir
@dir ||= 'db'
end
end
end
end
82 changes: 82 additions & 0 deletions lib/active_cabinet/metaclass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'hash_cabinet'
require 'forwardable'
require 'active_cabinet/config'

class ActiveCabinet
class << self
extend Forwardable
def_delegators :cabinet, :count, :delete, :empty?, :keys, :size

def []=(id, attributes)
create attributes.merge(id: id)
end

def all
cabinet.values.map do |attributes|
new(attributes)
end
end

def all_attributes
optional_attributes + required_attributes
end

def cabinet
@cabinet ||= HashCabinet.new "#{Config.dir}/#{cabinet_name}"
end

def create(attributes)
record = new attributes
record.save
end

def delete(id)
!!cabinet.delete(id)
end

def drop
cabinet.clear
end

def find(id)
attributes = cabinet[id]
attributes ? new(attributes) : nil
end
alias [] find

def optional_attributes(*args)
args = args.first if args.first.is_a? Array
if args.any?
@optional_attributes = *args
else
@optional_attributes ||= []
end
end

def required_attributes(*args)
args = args.first if args.first.is_a? Array
if args.any?
@required_attributes = args
@required_attributes.push :id unless @required_attributes.include? :id
@required_attributes
else
@required_attributes ||= [:id]
end
end

def to_h
cabinet.to_h.map { |id, attributes| [id, new(attributes)] }.to_h
end

def where
all.select { |asset| yield asset }
end

private

def cabinet_name
@cabinet_name ||= self.to_s.downcase.gsub('::', '_')
end

end
end
3 changes: 3 additions & 0 deletions lib/active_cabinet/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ActiveCabinet
VERSION = "0.0.1"
end
Loading

0 comments on commit d6ee1e4

Please sign in to comment.