gimlet-model 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3a6225098749abd2827e4f227d326ced0517f64
4
+ data.tar.gz: ba7c7827c677c61714876982ba588017551cd67b
5
+ SHA512:
6
+ metadata.gz: 21f4df9a49af2efb5ee5e6b7e065a0df027b6168f376a4b1673fb594919352f14df2763cf4248dd0ec011bd0806bdc115236ac53752041d393993c3ca38a5acf
7
+ data.tar.gz: 3ca800da43c908b1da6fdaf4e06653640cd59100387eb3568ead5575169271edd43e9c0e02c59e6f6d684ef88c04b4eb2825879fb2177bac6e1dc9becb41993e
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gimlet-model.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Yoji SHIDARA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Gimlet::Model
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gimlet-model'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gimlet-model
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+
6
+ task :default => :spec
7
+
8
+ desc "Run all specs in spec directory"
9
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,5 @@
1
+ ---
2
+ id: 1
3
+ name: one
4
+ even: no
5
+ odd: yes
@@ -0,0 +1,5 @@
1
+ ---
2
+ id: 2
3
+ name: two
4
+ even: yes
5
+ odd: no
@@ -0,0 +1,5 @@
1
+ ---
2
+ id: 3
3
+ name: three
4
+ even: no
5
+ odd: yes
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gimlet/model/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gimlet-model"
8
+ spec.version = Gimlet::Model::VERSION
9
+ spec.authors = ["Yoji SHIDARA"]
10
+ spec.email = ["dara@shidara.net"]
11
+ spec.description = %q{Gimlet: Document-oriented, Text-based and Read-only data storage, acts as Model}
12
+ spec.summary = %q{Gimlet: Document-oriented, Text-based and Read-only data storage, acts as Model}
13
+ spec.homepage = "https://github.com/darashi/gimlet-model"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "gimlet", "~> 0.0.2"
25
+ end
@@ -0,0 +1,47 @@
1
+ require 'forwardable'
2
+ require 'gimlet'
3
+ require 'gimlet/queryable'
4
+
5
+ module Gimlet
6
+ module Model
7
+ extend Forwardable
8
+
9
+ def self.included(base)
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+ include Queryable::API
15
+
16
+ def find(id)
17
+ @instances[id.to_s]
18
+ end
19
+
20
+ def source(source)
21
+ @instances = {}
22
+ source.each do |id, data|
23
+ @instances[id] = self.new(data)
24
+ end
25
+ end
26
+
27
+ def scope(name, body)
28
+ singleton_class.send(:define_method, name) do |*args|
29
+ scope = body.call(*args)
30
+ scope || all
31
+ end
32
+ end
33
+
34
+ attr_accessor :current_scope
35
+ end
36
+
37
+ def initialize(data)
38
+ @data = data
39
+ end
40
+
41
+ def to_param
42
+ id
43
+ end
44
+
45
+ def_delegators :@data, :[], :method_missing, :to_h
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module Gimlet
2
+ module Model
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,70 @@
1
+ require 'forwardable'
2
+
3
+ module Gimlet
4
+ module Queryable
5
+ module API
6
+ extend Forwardable
7
+
8
+ def select(options = {})
9
+ selecting = @instances
10
+ options[:where].each do |attribute, operator, argument|
11
+ selecting = selecting.select do |id, instance|
12
+ instance[attribute].send(operator, argument)
13
+ end
14
+ end
15
+ selecting.values
16
+ end
17
+
18
+ def new_query
19
+ current_scope || Query.new(self)
20
+ end
21
+
22
+ def_delegators :new_query, :all, :where, :first, :last, :count
23
+ end
24
+
25
+ class Query
26
+ include Enumerable
27
+ extend Forwardable
28
+
29
+ def initialize(model)
30
+ @model = model
31
+ @where = []
32
+ end
33
+
34
+ def where(hash)
35
+ hash.each do |attribute, value|
36
+ case value
37
+ when Array
38
+ @where.push([attribute, :in?, value]) # should be :== ?
39
+ when Regexp
40
+ @where.push([attribute, :=~, value])
41
+ else
42
+ @where.push([attribute, :==, value])
43
+ end
44
+ end
45
+ self
46
+ end
47
+
48
+ def all
49
+ @model.select(:where => @where)
50
+ end
51
+
52
+ def method_missing(method, *args, &block)
53
+ if @model.respond_to?(method)
54
+ scoping { @model.send(method, *args, &block) }
55
+ else
56
+ super
57
+ end
58
+ end
59
+
60
+ def scoping
61
+ previous, @model.current_scope = @model.current_scope, self
62
+ yield
63
+ ensure
64
+ @model.current_scope = previous
65
+ end
66
+
67
+ def_delegators :all, :each, :first, :last
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,87 @@
1
+ require_relative '../spec_helper'
2
+
3
+ class TestModel
4
+ include Gimlet::Model
5
+ source Gimlet::DataStore.new(fixture_path('model'))
6
+
7
+ scope :even, -> { where(even: true) }
8
+ scope :odd, -> { where(odd: true) }
9
+ end
10
+
11
+ describe Gimlet::Model do
12
+ describe '.find' do
13
+ subject { TestModel.find(1) }
14
+
15
+ it do
16
+ expect(subject.id).to eq(1) # NOTE or should be "1" ?
17
+ end
18
+
19
+ it do
20
+ expect(subject.name).to eq('one')
21
+ end
22
+ end
23
+
24
+ describe '.all' do
25
+ subject { TestModel.all }
26
+
27
+ it do
28
+ expect(subject.size).to eq(3)
29
+ end
30
+ end
31
+
32
+ describe '.count' do
33
+ subject { TestModel.count }
34
+
35
+ it do
36
+ expect(subject).to eq(3)
37
+ end
38
+ end
39
+
40
+ describe '.where' do
41
+ subject { TestModel.where(even: true) }
42
+
43
+ it do
44
+ expect(subject.first.name).to eq('two')
45
+ end
46
+ end
47
+
48
+ describe '.scope' do
49
+ subject { TestModel.even }
50
+
51
+ it do
52
+ expect(subject.first.name).to eq('two')
53
+ end
54
+ end
55
+
56
+ describe 'where chain' do
57
+ subject { TestModel.where(id: 1).where(odd: true) }
58
+
59
+ it do
60
+ expect(subject.first.name).to eq('one')
61
+ end
62
+ end
63
+
64
+ describe 'where then scope' do
65
+ subject { TestModel.where(id: 1).odd }
66
+
67
+ it do
68
+ expect(subject.first.name).to eq('one')
69
+ end
70
+ end
71
+
72
+ describe 'scope then where' do
73
+ subject { TestModel.odd.where(id: 1) }
74
+
75
+ it do
76
+ expect(subject.first.name).to eq('one')
77
+ end
78
+ end
79
+
80
+ describe 'disjoint scopes' do
81
+ subject { TestModel.odd.even }
82
+
83
+ it do
84
+ expect(subject.all).to eq([])
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,5 @@
1
+ require_relative '../lib/gimlet/model'
2
+
3
+ def fixture_path(name)
4
+ File.join(__dir__, '..', 'fixtures', name)
5
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gimlet-model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yoji SHIDARA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gimlet
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.2
55
+ description: 'Gimlet: Document-oriented, Text-based and Read-only data storage, acts
56
+ as Model'
57
+ email:
58
+ - dara@shidara.net
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - .rspec
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - fixtures/model/1.yaml
70
+ - fixtures/model/2.yaml
71
+ - fixtures/model/3.yaml
72
+ - gimlet-model.gemspec
73
+ - lib/gimlet/model.rb
74
+ - lib/gimlet/model/version.rb
75
+ - lib/gimlet/queryable.rb
76
+ - spec/gimlet/model_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: https://github.com/darashi/gimlet-model
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.3
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: 'Gimlet: Document-oriented, Text-based and Read-only data storage, acts as
102
+ Model'
103
+ test_files:
104
+ - spec/gimlet/model_spec.rb
105
+ - spec/spec_helper.rb