Skip to content

Commit

Permalink
main feature (#2)
Browse files Browse the repository at this point in the history
main feature (#2)
  • Loading branch information
jinghua000 committed Nov 11, 2019
1 parent 775cc3a commit 878e206
Show file tree
Hide file tree
Showing 26 changed files with 738 additions and 4 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service_name: travis-pro
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.vscode
.idea
.DS_Store
coverage
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: ruby
before_install:
- gem install bundler
rvm:
- 2.6.3
script:
- bundle exec rspec
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source 'https://gems.ruby-china.com/'

group :development, :test do
gem 'rspec'
gem 'simplecov'
gem 'coveralls'
end
51 changes: 51 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
GEM
remote: https://gems.ruby-china.com/
specs:
coveralls (0.7.2)
multi_json (~> 1.3)
rest-client (= 1.6.7)
simplecov (>= 0.7)
term-ansicolor (= 1.2.2)
thor (= 0.18.1)
diff-lcs (1.3)
docile (1.3.2)
json (2.2.0)
mime-types (3.3)
mime-types-data (~> 3.2015)
mime-types-data (3.2019.1009)
multi_json (1.14.1)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.0)
rspec-support (~> 3.9.0)
rspec-expectations (3.9.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.0)
simplecov (0.17.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
term-ansicolor (1.2.2)
tins (~> 0.8)
thor (0.18.1)
tins (0.13.2)

PLATFORMS
ruby

DEPENDENCIES
coveralls
rspec
simplecov

BUNDLED WITH
2.0.2
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# client-data-adapter

## TODO
[![Build Status](https://travis-ci.org/jinghua000/client-data-adapter.svg?branch=master)](https://travis-ci.org/jinghua000/client-data-adapter)
[![Gem Version](https://badge.fury.io/rb/client-data-adapter.svg)](https://rubygems.org/gems/client-data-adapter)
[![Coverage Status](https://coveralls.io/repos/github/jinghua000/client-data-adapter/badge.svg?branch=master)](https://coveralls.io/github/jinghua000/client-data-adapter?branch=master)

## Introduction

TODO
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
task :test do
p 123
end
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gem build client-data-adapter.gemspec
gem install client-data-adapter-0.0.1.gem
3 changes: 3 additions & 0 deletions client-data-adapter.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |s|

s.name = 'client-data-adapter'
Expand Down
84 changes: 84 additions & 0 deletions demo/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require_relative '../lib/client-data-adapter'
require_relative 'book_shelf'
require_relative 'category'

class Book

include ClientDataAdapter

define_adapter do

link_one :book_shelf
link_many :categories

adapter do
{
id: id,
title: title,
}
end

with :full_title do
"<#{title}>"
end

with :my_awesome_title do
"my_awesome_title"
end

with :pass1 do |*args|
args << 'pass1'
end

with :pass2 do |*args|
args << 'pass2'
end

with :id do
return id if true
# :nocov:
raise 'CODE WILL NOT REACH HERE.'
# :nocov:
end

end

attr_accessor :id, :title, :book_shelf_id

def initialize(**opt)

self.id = opt[:id]
self.title = opt[:title]
self.book_shelf_id = opt[:book_shelf_id]

end

def self.demo
new(
id: 1,
title: 'My Book',
book_shelf_id: 1,
)
end

def my_title
'my title'
end

def my_awesome_title
'no no no'
end

def sub_title(sub)
"#{title}-#{sub}"
end

def book_shelf
BookShelf.demo
end

def categories
[Category.new(id: 1, cat: 'one'), Category.new(id: 2, cat: 'two')]
end

end
46 changes: 46 additions & 0 deletions demo/book_shelf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require_relative 'library'

class BookShelf

include ClientDataAdapter

define_adapter do

link_one :library

adapter do
{
id: id,
desc: desc,
}
end

with :other_desc do
'other desc'
end

end

attr_accessor :id, :desc, :library_id

def initialize(**opt)

self.id = opt[:id]
self.desc = opt[:desc]
self.library_id = opt[:library_id]

end

def self.demo
new(
id: 1,
desc: 'my book shelf',
library_id: 1,
)
end

def library
Library.demo
end

end
29 changes: 29 additions & 0 deletions demo/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Category

include ClientDataAdapter

define_adapter do

adapter do
{
id: id,
cat: cat,
}
end

end

attr_accessor :id, :cat

def initialize(**opt)

self.id = opt[:id]
self.cat = opt[:cat]

end

def summary
'summary'
end

end
36 changes: 36 additions & 0 deletions demo/library.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Library

include ClientDataAdapter

define_adapter do

adapter do
{
id: id,
size: size,
}
end

with :welcome do
'welcome'
end

end

attr_accessor :id, :size, :city_id

def initialize(**opt)

self.id = opt[:id]
self.size = opt[:size]

end

def self.demo
new(
id: 1,
size: 'small'
)
end

end
8 changes: 6 additions & 2 deletions lib/client-data-adapter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
require_relative 'client-data-adapter/class_methods'
require_relative 'client-data-adapter/instance_methods'

module ClientDataAdapter

def self.demo
'hello'
def self.included(base)
base.include(ClientDataAdapter::InstanceMethods)
base.extend(ClientDataAdapter::ClassMethods)
end

end
18 changes: 18 additions & 0 deletions lib/client-data-adapter/class_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require_relative 'config'
require_relative 'wrapper'

module ClientDataAdapter
module ClassMethods

def define_adapter(&block)

const_set(ADAPTER_WRAPPER, Class.new(Wrapper))

define_method :adapter_wrapper do
@__adapter_wrapper__ ||= self.class.const_get(ADAPTER_WRAPPER).new(self, &block)
end

end

end
end
6 changes: 6 additions & 0 deletions lib/client-data-adapter/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ClientDataAdapter

ADAPTER_WRAPPER = :AdapterWrapper
ADAPTER = :adapter

end
41 changes: 41 additions & 0 deletions lib/client-data-adapter/instance_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative 'util'

module ClientDataAdapter
module InstanceMethods

def adapter(*args)

length = args.length

if length == 0
adapter_wrapper.__adapter__
else
adapter_wrapper.__adapter__.merge(

*args.map do |arg|
if [String, Symbol].include?(arg.class)
__merge_to_adapter__(arg.to_sym, nil)
elsif arg.is_a?(Hash)
arg.map(&method(:__merge_to_adapter__))
else
raise '[ERROR] Not available arguments type.'
end
end.flatten,

)
end
end

private

def __merge_to_adapter__(key, params)
{}.tap do |hah|
hah["#{key}".to_sym] =
adapter_wrapper.respond_to?(key) ?
adapter_wrapper.public_send(key, *params) :
public_send(key, *params)
end
end

end
end

0 comments on commit 878e206

Please sign in to comment.