yeti 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
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
18
+ *.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yeti.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Joseph HALTER
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,31 @@
1
+ # Yeti
2
+
3
+ Yeti: Context, Editor and Search patterns
4
+
5
+ Editor pattern simplifies edition of multiple objects at once using ActiveModel
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'yeti'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install yeti
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ require "active_model"
2
+ require "string_cleaner"
3
+ require "yeti/context"
4
+ require "yeti/errors"
5
+ require "yeti/editor"
6
+ require "yeti/search"
7
+ require "yeti/version"
@@ -0,0 +1,28 @@
1
+ module Yeti
2
+ class Context
3
+ class NoAccount
4
+ attr_reader :id
5
+ end
6
+
7
+ def initialize(hash)
8
+ @given_account_id = hash.fetch(:account_id)
9
+ end
10
+
11
+ def account
12
+ @account ||= find_account_by_id(given_id) || no_account
13
+ end
14
+
15
+ def find_account_by_id(id)
16
+ raise NotImplementedError
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :given_account_id
22
+
23
+ def no_account
24
+ NoAccount.new
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,109 @@
1
+ module Yeti
2
+ class Editor
3
+ include ActiveModel::Validations
4
+ include ActiveModel::Dirty
5
+
6
+ attr_reader :context
7
+ delegate :id, to: :edited
8
+
9
+ def initialize(context, given_id)
10
+ @context = context
11
+ @given_id = given_id
12
+ end
13
+
14
+ def errors
15
+ @errors ||= ::Yeti::Errors.new(
16
+ self,
17
+ untranslated: self.class.untranslated?
18
+ )
19
+ end
20
+
21
+ def edited
22
+ @edited ||= if given_id
23
+ find_by_id given_id.to_i
24
+ else
25
+ new_object
26
+ end
27
+ end
28
+
29
+ def find_by_id(id)
30
+ raise NotImplementedError
31
+ end
32
+
33
+ def new_object
34
+ raise NotImplementedError
35
+ end
36
+
37
+ def persisted?
38
+ !!given_id
39
+ end
40
+
41
+ def update_attributes(attrs)
42
+ self.attributes = attrs
43
+ save
44
+ end
45
+
46
+ def attributes
47
+ attributes = {}
48
+ self.class.attributes.each do |key|
49
+ attributes[key] = send key
50
+ end
51
+ attributes
52
+ end
53
+
54
+ def attributes=(attrs)
55
+ self.class.attributes.each do |key|
56
+ self.send "#{key}=", attrs[key] if attrs.has_key? key
57
+ end
58
+ end
59
+
60
+ def save
61
+ return false unless valid?
62
+ persist!
63
+ @previously_changed = changes
64
+ changed_attributes.clear
65
+ true
66
+ end
67
+
68
+ def persist!
69
+ raise NotImplementedError
70
+ end
71
+
72
+ private
73
+
74
+ attr_reader :given_id
75
+
76
+ def self.attribute(name, opts={})
77
+ self.attributes << name
78
+ define_attribute_methods attributes
79
+ from = opts[:from] || :edited
80
+ class_eval """
81
+ def #{name}
82
+ @#{name} = #{from}.#{name} unless defined? @#{name}
83
+ @#{name}
84
+ end
85
+ def #{name}=(value)
86
+ value = (value.to_s.clean.strip if value)
87
+ return if value==#{name}
88
+ #{name}_will_change!
89
+ @#{name} = value
90
+ original_value = changed_attributes[\"#{name}\"]
91
+ changed_attributes.delete \"#{name}\" if value==original_value
92
+ end
93
+ """
94
+ end
95
+
96
+ def self.attributes
97
+ @attributes ||= []
98
+ end
99
+
100
+ def self.dont_translate_error_messages
101
+ @untranslated = true
102
+ end
103
+
104
+ def self.untranslated?
105
+ !!@untranslated
106
+ end
107
+
108
+ end
109
+ end
@@ -0,0 +1,25 @@
1
+ module Yeti
2
+ class Errors < ::ActiveModel::Errors
3
+
4
+ def initialize(base, opts={})
5
+ super base
6
+ @untranslated = opts.fetch :untranslated, false
7
+ end
8
+
9
+ def untranslated?
10
+ @untranslated
11
+ end
12
+
13
+ private
14
+
15
+ def normalize_message(attribute, message, options)
16
+ message ||= :invalid
17
+ if untranslated? && !message.is_a?(Proc)
18
+ message
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ module Yeti
2
+ class Search
3
+
4
+ attr_reader :search, :page
5
+ delegate :to_ary, :empty?, :each, :group_by, :size, to: :results
6
+
7
+ def initialize(context, hash)
8
+ @context = context
9
+ @search = hash[:search] || {}.with_indifferent_access
10
+ @page = hash[:page] || 1
11
+ @per_page = hash[:per_page] || 20
12
+ end
13
+
14
+ def page_count
15
+ paginated_results.page_count
16
+ end
17
+
18
+ def count
19
+ paginated_results.pagination_record_count
20
+ end
21
+
22
+ def results
23
+ paginated_results.all
24
+ end
25
+
26
+ def respond_to?(method)
27
+ super || method.to_s=~delegate_to_search_pattern
28
+ end
29
+
30
+ def method_missing(method, *args, &block)
31
+ case method.to_s
32
+ when /_id_equals\z/
33
+ search[method].to_i
34
+ when delegate_to_search_pattern
35
+ search[method]
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ attr_reader :context, :per_page
44
+
45
+ # ~~~ private instance methods ~~~
46
+ def delegate_to_search_pattern
47
+ /(?:_equals|_contains|_gte|_lte)\z/
48
+ end
49
+
50
+ def paginated_results
51
+ raise NotImplementedError
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module Yeti
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "bundler/setup"
2
+ require "logger"
3
+ require "yeti"
4
+
5
+ RSpec.configure do |config|
6
+ config.before(:all) do
7
+ logger = Logger.new "test.log"
8
+ end
9
+ end
@@ -0,0 +1,151 @@
1
+ require "spec_helper"
2
+
3
+ describe ::Yeti::Editor do
4
+ let(:context){ mock :context }
5
+ subject{ ::Yeti::Editor.new context, nil }
6
+ it "should keep given context" do
7
+ subject.context.should be context
8
+ end
9
+ it "#find_by_id should be virtual" do
10
+ lambda{ subject.find_by_id 1 }.should raise_error NotImplementedError
11
+ end
12
+ it "#new_object should be virtual" do
13
+ lambda{ subject.new_object }.should raise_error NotImplementedError
14
+ end
15
+ it "#persist! should be virtual" do
16
+ lambda{ subject.persist! }.should raise_error NotImplementedError
17
+ end
18
+ context "with a given id" do
19
+ subject{ ::Yeti::Editor.new context, 1 }
20
+ it{ should be_persisted }
21
+ it "#edited should use #find_by_id" do
22
+ subject.stub(:find_by_id).with(1).and_return(expected = mock)
23
+ subject.edited.should be expected
24
+ end
25
+ end
26
+ context "without id" do
27
+ it{ should_not be_persisted }
28
+ it "#edited should use #new_object" do
29
+ subject.stub(:new_object).and_return(expected = mock)
30
+ subject.edited.should be expected
31
+ end
32
+ end
33
+ context "when not valid" do
34
+ before{ subject.stub(:valid?).and_return false }
35
+ it "#save should return false" do
36
+ subject.save.should be false
37
+ end
38
+ end
39
+ context "when valid" do
40
+ it "#save should call persist! then return true" do
41
+ subject.should_receive :persist!
42
+ subject.save.should be true
43
+ end
44
+ end
45
+ context "editor of one record" do
46
+ let :object_editor do
47
+ Class.new ::Yeti::Editor do
48
+ attribute :name
49
+ validates_presence_of :name
50
+ def self.name
51
+ "ObjectEditor"
52
+ end
53
+ end
54
+ end
55
+ context "new record" do
56
+ subject{ object_editor.new context, nil }
57
+ let(:new_record){ mock :new_record, name: nil, id: nil }
58
+ before{ subject.stub(:new_object).and_return new_record }
59
+ its(:id){ should be_nil }
60
+ its(:name){ should be_nil }
61
+ it "#name= should convert input to string" do
62
+ subject.name = ["test"]
63
+ subject.name.should == "[\"test\"]"
64
+ end
65
+ it "#name= should clean the value" do
66
+ subject.name = "\tInfected\210\004"
67
+ subject.name.should == "Infected"
68
+ end
69
+ it "#name= should accept nil" do
70
+ subject.name = "Valid"
71
+ subject.name = nil
72
+ subject.name.should be_nil
73
+ end
74
+ it "#attributes should return a hash" do
75
+ subject.attributes.should == {name: nil}
76
+ end
77
+ it "#attributes= should assign each attribute" do
78
+ subject.should_receive(:name=).with "Anthony"
79
+ subject.attributes = {name: "Anthony"}
80
+ end
81
+ it "#attributes= should skip unknown attributes" do
82
+ subject.attributes = {unknown: "Anthony"}
83
+ end
84
+ context "before validation" do
85
+ its(:errors){ should be_empty }
86
+ end
87
+ context "after validation" do
88
+ it "should have an error on name" do
89
+ subject.valid?
90
+ subject.errors[:name].should have(1).item
91
+ subject.errors[:name].should == ["can't be blank"]
92
+ end
93
+ it "should be able to return untranslated error messages" do
94
+ object_editor.class_eval do
95
+ dont_translate_error_messages
96
+ end
97
+ subject.valid?
98
+ subject.errors[:name].should == [:blank]
99
+ end
100
+ end
101
+ context "when name is empty" do
102
+ it{ should_not be_valid }
103
+ end
104
+ context "when name is changed" do
105
+ before{ subject.name = "Anthony" }
106
+ it{ should be_valid }
107
+ it "#attributes should be updated" do
108
+ subject.attributes.should == {name: "Anthony"}
109
+ end
110
+ it("name should be updated"){ subject.name.should == "Anthony" }
111
+ it("name should be dirty"){ subject.name_changed?.should be true }
112
+ it "name should not be dirty anymore if original value is reset" do
113
+ subject.name = nil
114
+ subject.name_changed?.should be false
115
+ end
116
+ end
117
+ context "after save" do
118
+ before do
119
+ subject.name = "Anthony"
120
+ subject.stub :persist!
121
+ subject.save
122
+ end
123
+ it "should reset dirty attributes" do
124
+ subject.name_changed?.should be false
125
+ end
126
+ it "should still know previous changes" do
127
+ subject.previous_changes.should == {"name"=>[nil, "Anthony"]}
128
+ end
129
+ end
130
+ end
131
+ context "existing record" do
132
+ subject{ object_editor.new context, 1 }
133
+ let(:existing_record){ mock :existing_record, name: "Anthony", id: 1 }
134
+ before{ subject.stub(:find_by_id).with(1).and_return existing_record }
135
+ it("should get id from record"){ subject.id.should be 1 }
136
+ it "should get name from record" do
137
+ subject.name.should == "Anthony"
138
+ end
139
+ it{ should be_valid }
140
+ context "when name is changed" do
141
+ before{ subject.name = nil }
142
+ it("name should be updated"){ subject.name.should be_nil }
143
+ it("name should be dirty"){ subject.name_changed?.should be true }
144
+ it "name should not be dirty anymore if original value is reset" do
145
+ subject.name = "Anthony"
146
+ subject.name_changed?.should be false
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+
3
+ describe Yeti do
4
+ it "version should be defined" do
5
+ lambda{ Yeti::VERSION }.should_not raise_error NameError
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yeti/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "yeti"
8
+ gem.version = Yeti::VERSION
9
+ gem.authors = ["Joseph HALTER"]
10
+ gem.email = ["joseph@openhood.com"]
11
+ gem.description = %q{Yeti: Context, Editor and Search patterns}
12
+ gem.summary = %q{Editor pattern simplifies edition of multiple objects
13
+ at once using ActiveModel}
14
+ gem.homepage = ""
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_runtime_dependency "activemodel"
22
+ gem.add_runtime_dependency "string_cleaner"
23
+
24
+ gem.add_development_dependency "rake"
25
+ gem.add_development_dependency "rspec"
26
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yeti
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joseph HALTER
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: string_cleaner
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: ! 'Yeti: Context, Editor and Search patterns'
79
+ email:
80
+ - joseph@openhood.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - lib/yeti.rb
91
+ - lib/yeti/context.rb
92
+ - lib/yeti/editor.rb
93
+ - lib/yeti/errors.rb
94
+ - lib/yeti/search.rb
95
+ - lib/yeti/version.rb
96
+ - spec/spec_helper.rb
97
+ - spec/yeti/editor_spec.rb
98
+ - spec/yeti_spec.rb
99
+ - yeti.gemspec
100
+ homepage: ''
101
+ licenses: []
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ segments:
113
+ - 0
114
+ hash: 955757267782825945
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ segments:
122
+ - 0
123
+ hash: 955757267782825945
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.24
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Editor pattern simplifies edition of multiple objects at once using ActiveModel
130
+ test_files:
131
+ - spec/spec_helper.rb
132
+ - spec/yeti/editor_spec.rb
133
+ - spec/yeti_spec.rb