artisan-memory-repository 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ require 'artisan-memory-repository/story_repository'
2
+ require 'artisan-memory-repository/project_repository'
3
+ require 'artisan-memory-repository/iteration_repository'
4
+ require 'artisan-memory-repository/user_repository'
5
+ require 'artisan-memory-repository/future_user_repository'
6
+ require 'artisan-memory-repository/project_configuration_repository'
7
+ require 'artisan-memory-repository/change_repository'
8
+ require 'artisan-memory-repository/member_repository'
9
+
10
+ module ArtisanMemoryRepository
11
+ def self.story
12
+ @story_repo ||= StoryRepository.new
13
+ end
14
+
15
+ def self.project
16
+ @project_repo ||= ProjectRepository.new
17
+ end
18
+
19
+ def self.iteration
20
+ @iteration_repo ||= IterationRepository.new
21
+ end
22
+
23
+ def self.user
24
+ @user_repo ||= UserRepository.new
25
+ end
26
+
27
+ def self.future_user
28
+ @future_user_repo ||= FutureUserRepository.new
29
+ end
30
+
31
+ def self.project_configuration
32
+ @project_config_repo ||= ProjectConfigurationRepository.new
33
+ end
34
+
35
+ def self.member
36
+ @member_repo ||= MemberRepository.new
37
+ end
38
+
39
+ def self.change
40
+ @change ||= ChangeRepository.new
41
+ end
42
+ end
@@ -0,0 +1,57 @@
1
+ module ArtisanMemoryRepository
2
+
3
+ module BaseRepository
4
+ def save(object)
5
+ clone = object.dup
6
+
7
+ if clone.id
8
+ records[clone.id] = clone
9
+ else
10
+ clone.id = records.size + 1
11
+ records[clone.id] = clone
12
+ end
13
+
14
+ return clone
15
+ end
16
+
17
+ #def create!(attrs = {})
18
+ # project = model_class.new(attrs)
19
+ # self.save(project)
20
+ #end
21
+
22
+ def create(attrs = {})
23
+ project = model_class.new(attrs)
24
+ self.save(project)
25
+ end
26
+
27
+ def find(id)
28
+ record = records[id.to_i]
29
+ raise Artisan::RecordNotFound unless record
30
+ record
31
+ end
32
+
33
+ def find_each(&block)
34
+ records.each do |id, model|
35
+ block.call(model)
36
+ end
37
+ end
38
+
39
+ def delete(id)
40
+ records.delete(id)
41
+ end
42
+
43
+ def destroy_all
44
+ @records = {}
45
+ end
46
+
47
+ def records
48
+ @records ||= {}
49
+ end
50
+
51
+ def where(attributes)
52
+ records.values.select do |record|
53
+ attributes.all? { |key, value| record.send(key) == value }
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,31 @@
1
+ require 'artisan-memory-repository/base_repository'
2
+ require 'artisan-memory-repository/models/change'
3
+
4
+ module ArtisanMemoryRepository
5
+ class ChangeRepository
6
+ include BaseRepository
7
+
8
+ def model_class
9
+ ArtisanMemoryRepository::Change
10
+ end
11
+
12
+ def create_project_change(attributes = {})
13
+ create(attributes.merge({:type => "ProjectChange"}))
14
+ end
15
+
16
+ def create_iteration_change(attributes = {})
17
+ create(attributes.merge({:type => "IterationChange"}))
18
+ end
19
+
20
+
21
+ def create_story_change(attributes = {})
22
+ create(attributes.merge({:type => "StoryChange"}))
23
+ end
24
+
25
+
26
+ def find_all_by_project_id(project_id)
27
+ records.values.find_all {|r| r.project_id == project_id}.sort_by(&:created_at).reverse
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,39 @@
1
+ require 'artisan-memory-repository/models/future_user'
2
+ require 'artisan-memory-repository/base_repository'
3
+
4
+ module ArtisanMemoryRepository
5
+ class FutureUserRepository
6
+ include BaseRepository
7
+
8
+ def new(attrs = {})
9
+ ArtisanMemoryRepository::FutureUser.new(attrs)
10
+ end
11
+
12
+ def create(attrs = {})
13
+ future_user = new(attrs)
14
+ unique_email = validate_unique_email(attrs[:email])
15
+ if unique_email
16
+ save(future_user)
17
+ else
18
+ future_user.add_errors(:email => "This email has already been taken")
19
+ raise ArgumentError.new('The record has errors.')
20
+ end
21
+ future_user
22
+ end
23
+
24
+ def find_by_email(email)
25
+ records.values.find { |r| r.email == email }
26
+ end
27
+
28
+ def find_by_project_id(project_id)
29
+ records.values.find { |r| r.project_id == project_id }
30
+ end
31
+
32
+ private
33
+
34
+ def validate_unique_email(email)
35
+ return false if email.nil?
36
+ !records.values.any? { |r| r.email == email }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,36 @@
1
+ require 'artisan-memory-repository/base_repository'
2
+ require 'artisan-memory-repository/models/iteration'
3
+
4
+ module ArtisanMemoryRepository
5
+ class IterationRepository
6
+ include BaseRepository
7
+
8
+ def model_class
9
+ ArtisanMemoryRepository::Iteration
10
+ end
11
+
12
+ def new(attributes = {})
13
+ model_class.new(attributes)
14
+ end
15
+
16
+ def create_for_project(project, attrs)
17
+ iteration = model_class.new(attrs)
18
+ iteration.project = project
19
+ project.iterations << iteration
20
+ save(iteration)
21
+ end
22
+
23
+ def find_by_number(project, number)
24
+ records.values.find {|r| r.project == project && r.number = number}
25
+ end
26
+
27
+ def by_project(project, options = {})
28
+ records.values.find_all {|r| r.project == project}.sort_by(&:number).reverse
29
+ end
30
+
31
+ def by_project_by_finish_date(project)
32
+ self.by_project(project).sort_by!{ |iteration| Date.parse(iteration.finish_date) }.reverse
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ require 'artisan-memory-repository/models/member'
2
+
3
+ module ArtisanMemoryRepository
4
+ class MemberRepository
5
+ def model_name
6
+ Member
7
+ end
8
+
9
+ def new(attributes = {})
10
+ model_name.new(attributes)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,85 @@
1
+ module ArtisanMemoryRepository
2
+ class Base
3
+ def initialize(options = {})
4
+ self.class._collections.each {|field| set(field, [])}
5
+ options.each { |key, value| set(key, value) }
6
+ set(:id, random_int) if saves?
7
+ self.init
8
+ end
9
+
10
+ def init; end
11
+
12
+ def self.data_attributes(*fields)
13
+ @_fields = fields
14
+ (fields + [:id, :saves]).each {|field| attr_accessor field }
15
+ end
16
+
17
+ def self._fields
18
+ @_fields ||= []
19
+ end
20
+
21
+ def self.boolean(*fields)
22
+ (fields).each do |field|
23
+ define_method("#{field}?".to_sym) { !!get(field) }
24
+ end
25
+ end
26
+
27
+ def self.collections(*collections)
28
+ @_collections = collections
29
+ collections.each do |collection|
30
+ attr_accessor collection
31
+ end
32
+ end
33
+
34
+ def self._collections
35
+ @_collections ||= []
36
+ end
37
+
38
+ def saves?
39
+ get(:saves).nil? ? true : !!get(:saves)
40
+ end
41
+
42
+ def get(key)
43
+ instance_variable_get("@#{key}")
44
+ end
45
+
46
+ def set(key, value)
47
+ instance_variable_set("@#{key}", value)
48
+ end
49
+
50
+ def attributes
51
+ self.class._fields.inject({}) do |attributes, key|
52
+ attributes[key] = get(key)
53
+ attributes
54
+ end
55
+ end
56
+
57
+ alias :update_attribute :set
58
+
59
+ def update_attributes(options = {})
60
+ return false unless saves?
61
+ return attributes if options.nil?
62
+ options.each { |key, value| set(key, value) }
63
+ end
64
+
65
+ alias :update_attributes! :update_attributes
66
+
67
+ def ==(other)
68
+ return false if other.nil?
69
+ return false if other.class != self.class
70
+ self.class._fields.each do |attr|
71
+ return false if self.send(attr) != other.send(attr)
72
+ end
73
+ end
74
+
75
+ def to_param
76
+ id.to_s
77
+ end
78
+
79
+ private
80
+
81
+ def random_int
82
+ return (Time.now.to_i + rand(1000))
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,15 @@
1
+ require "artisan-memory-repository/models/base"
2
+
3
+ module ArtisanMemoryRepository
4
+ class Change < Base
5
+ data_attributes :type,
6
+ :source_id,
7
+ :user_id,
8
+ :event,
9
+ :project_id,
10
+ :created_at,
11
+ :original_fields,
12
+ :changed_fields
13
+
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require "artisan-memory-repository/models/base"
2
+
3
+ module ArtisanMemoryRepository
4
+ class FutureUser < Base
5
+ data_attributes :id, :email, :project_id, :login
6
+
7
+ attr_reader :errors
8
+
9
+ def init
10
+ @errors = {}
11
+ end
12
+
13
+ def add_errors(errors_hash)
14
+ errors_hash.each_pair do |key, value|
15
+ @errors[key] = @errors[key] || []
16
+ @errors[key] << value
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ require "artisan-memory-repository/models/base"
2
+
3
+ module ArtisanMemoryRepository
4
+ class Iteration < Base
5
+ data_attributes :completed_total,
6
+ :story_tags,
7
+ :start_date,
8
+ :finish_date,
9
+ :number,
10
+ :complete,
11
+ :completed_at,
12
+ :committed_points_at_completion,
13
+ :errors,
14
+ :total_billed_points,
15
+ :committed_points,
16
+ :total_points,
17
+ :project,
18
+ :days
19
+
20
+ boolean :complete
21
+
22
+ collections :stories, :users
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ module ArtisanMemoryRepository
2
+ class Member < Base
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ require "artisan-memory-repository/models/base"
2
+
3
+ module ArtisanMemoryRepository
4
+ class Project < Base
5
+ data_attributes :id, :name, :life_cycle, :archived, :description,
6
+ :api_key, :project_configuration
7
+
8
+ boolean :archived
9
+
10
+ collections :iterations, :stories, :members, :owners, :users
11
+
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require "artisan-memory-repository/models/base"
2
+
3
+ module ArtisanMemoryRepository
4
+ class ProjectConfiguration < Base
5
+ data_attributes :estimate_mode
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ require "artisan-memory-repository/models/base"
2
+
3
+ module ArtisanMemoryRepository
4
+ class Story < Base
5
+
6
+ data_attributes :assigned_user_id, :billed,
7
+ :complete, :completed_at, :created_at, :creator,
8
+ :creator_id, :deleted, :estimate, :incomplete, :name,
9
+ :number, :nonbillable, :optimistic, :pessimistic,
10
+ :position, :realistic, :standard_deviation, :tags,
11
+ :tag_list, :assigned_user, :iteration, :project
12
+
13
+ boolean :complete, :nonbillable, :deleted
14
+
15
+ def to_json(*args)
16
+ attributes.to_json
17
+ end
18
+
19
+ def valid?
20
+ true
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ require "artisan-memory-repository/models/base"
2
+ require "active_model"
3
+
4
+ module ArtisanMemoryRepository
5
+ class User < ArtisanMemoryRepository::Base
6
+ data_attributes :full_name, :projects, :members,
7
+ :login, :email, :gravatar_url, :password,
8
+ :password_confirmation
9
+
10
+ def initialize(*args)
11
+ @external_errors = []
12
+ super(*args)
13
+ end
14
+
15
+ include ActiveModel::Validations
16
+
17
+ validates :full_name, :presence => true
18
+ validates_length_of :login, :minimum => 3
19
+ validates_format_of :login, :with => /\A\w[\w\.+\-_@ ]+$/, :message => "should use only letters, numbers, spaces, and .-_@ please."
20
+
21
+ validate do
22
+ @external_errors.each { |error| errors.add *error }
23
+ true
24
+ end
25
+
26
+ def add_error(*args)
27
+ @external_errors << args
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ require 'artisan-memory-repository/models/project_configuration'
2
+
3
+ module ArtisanMemoryRepository
4
+ class ProjectConfigurationRepository
5
+ include BaseRepository
6
+
7
+ def model_class
8
+ ArtisanMemoryRepository::ProjectConfiguration
9
+ end
10
+
11
+ def new(attributes = {})
12
+ model_class.new(attributes)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,70 @@
1
+ require 'artisan-memory-repository/base_repository'
2
+ require 'artisan-memory-repository/models/project'
3
+ require "artisan-memory-repository/models/project_configuration"
4
+
5
+ module ArtisanMemoryRepository
6
+ class ProjectRepository
7
+ include BaseRepository
8
+
9
+ def model_class
10
+ ArtisanMemoryRepository::Project
11
+ end
12
+
13
+ def new(attributes = {})
14
+ model_class.new(attributes)
15
+ end
16
+
17
+ def order_by_name
18
+ records.values.sort_by { |r| r.name }
19
+ end
20
+
21
+ def unarchived
22
+ records.values.select { |r| !r.archived }.sort_by(&:name)
23
+ end
24
+
25
+ def archived
26
+ records.values.select { |r| r.archived }.sort_by(&:name)
27
+ end
28
+
29
+ def find_by_api_key(api_key)
30
+ records.values.find { |r| r.api_key == api_key }
31
+ end
32
+
33
+ def find_by_name(name)
34
+ records.values.find{|r| r.name == name}
35
+ end
36
+
37
+ def add_iteration(project, iteration)
38
+ project.iterations << iteration
39
+ project.iterations.sort! { |i| i.number }
40
+ iteration
41
+ end
42
+
43
+ def add_member(project, user, owner)
44
+ project.users << user
45
+ project.owners << user if owner
46
+ end
47
+
48
+ def remove_member(project, user)
49
+ project.users.delete(user)
50
+ project.owners.delete(user)
51
+ end
52
+
53
+ def is_member?(project, user)
54
+ project.users.include?(user)
55
+ end
56
+
57
+ def is_owner?(project, user)
58
+ project.owners.include?(user)
59
+ end
60
+
61
+ def owners(project)
62
+ project.owners
63
+ end
64
+
65
+ def create_project_configuration(project)
66
+ project.project_configuration = ArtisanMemoryRepository::ProjectConfiguration.new
67
+ end
68
+ end
69
+ end
70
+
@@ -0,0 +1,86 @@
1
+ require 'artisan-memory-repository/base_repository'
2
+ require 'artisan-memory-repository/models/story'
3
+
4
+ module ArtisanMemoryRepository
5
+ class StoryRepository
6
+ include BaseRepository
7
+
8
+ def model_class
9
+ ArtisanMemoryRepository::Story
10
+ end
11
+
12
+ def new(attributes = {})
13
+ model_class.new(attributes)
14
+ end
15
+
16
+ def find(number, project)
17
+ result = records.values.find do |record|
18
+ record.project == project && record.number == number
19
+ end
20
+
21
+ raise Artisan::RecordNotFound.new if result.nil?
22
+ return result
23
+ end
24
+
25
+ def create!(attributes = {})
26
+ save model_class.new(attributes)
27
+ end
28
+
29
+ def for_user(user)
30
+ records.values.select{|r| r.assigned_user == user}
31
+ end
32
+
33
+ def by_project(project)
34
+ records.values.select{|r| r.project.nil? ? false : r.project.id == project.id }
35
+ end
36
+
37
+ def create_for_project(project, attrs = {})
38
+ story = create(attrs)
39
+ project.stories << story
40
+ story.project = project
41
+ Artisan::Repository.project.save(project)
42
+ story
43
+ end
44
+
45
+ def add_to_iteration(iteration, story)
46
+ iteration.stories.delete_if { |s| s.id == story.id }
47
+ iteration.stories << story
48
+ story.iteration = iteration
49
+ Artisan::Repository.iteration.save(iteration)
50
+ end
51
+
52
+ def remove_from_iteration(story)
53
+ iteration = story.iteration
54
+ story.iteration = nil
55
+ iteration.stories.delete_if { |s| s.id == story.id }
56
+ end
57
+
58
+ def delete(story)
59
+ deleted = records.delete(story.id)
60
+ deleted.deleted = true
61
+ deleted
62
+ end
63
+
64
+ def all(iteration)
65
+ iteration.stories
66
+ end
67
+
68
+ def ready(iteration)
69
+ iteration.stories.select{|r| r.assigned_user.nil? && !r.complete}
70
+ end
71
+
72
+ def working(iteration)
73
+ iteration.stories.select{|r| !r.assigned_user.nil? &&
74
+ !r.complete}
75
+ end
76
+
77
+ def completed(iteration)
78
+ iteration.stories.select(&:complete)
79
+ end
80
+
81
+ def backlog(project)
82
+ records.values.select { |r| r.iteration.nil? }
83
+ end
84
+ end
85
+ end
86
+
@@ -0,0 +1,44 @@
1
+ require 'artisan-memory-repository/models/user'
2
+ require 'artisan-memory-repository/base_repository'
3
+
4
+ module ArtisanMemoryRepository
5
+ class UserRepository
6
+ include BaseRepository
7
+
8
+ def new(attrs = {})
9
+ ArtisanMemoryRepository::User.new(attrs)
10
+ end
11
+
12
+ def create(attrs = {})
13
+ user = new(attrs)
14
+ if !existing_login?(user.login) && user.valid?
15
+ save(user)
16
+ else
17
+ user.add_error(:login, 'has already been taken')
18
+ raise ArgumentError.new('The record has errors.')
19
+ end
20
+ user
21
+ end
22
+
23
+ def find_by_email(email)
24
+ records.values.find { |r| r.email == email }
25
+ end
26
+
27
+ def find_by_login(login)
28
+ return nil if login.nil?
29
+ records.values.find { |r| r.login.downcase == login.downcase }
30
+ end
31
+
32
+ def active_stories_for(user)
33
+ story_repository = Artisan::Repository.story
34
+ stories = story_repository.for_user(user)
35
+ stories.reject { |story| story.complete }
36
+ end
37
+
38
+ private
39
+
40
+ def existing_login?(login)
41
+ !find_by_login(login).nil?
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artisan-memory-repository
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - 8th Light Craftsmen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-10-10 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: An interface to the Artisan in memory persistance layer
17
+ email: paul@8thlight.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/artisan-memory-repository/artisan_memory_repository.rb
26
+ - lib/artisan-memory-repository/base_repository.rb
27
+ - lib/artisan-memory-repository/change_repository.rb
28
+ - lib/artisan-memory-repository/future_user_repository.rb
29
+ - lib/artisan-memory-repository/iteration_repository.rb
30
+ - lib/artisan-memory-repository/member_repository.rb
31
+ - lib/artisan-memory-repository/models/base.rb
32
+ - lib/artisan-memory-repository/models/change.rb
33
+ - lib/artisan-memory-repository/models/future_user.rb
34
+ - lib/artisan-memory-repository/models/iteration.rb
35
+ - lib/artisan-memory-repository/models/member.rb
36
+ - lib/artisan-memory-repository/models/project.rb
37
+ - lib/artisan-memory-repository/models/project_configuration.rb
38
+ - lib/artisan-memory-repository/models/story.rb
39
+ - lib/artisan-memory-repository/models/user.rb
40
+ - lib/artisan-memory-repository/project_configuration_repository.rb
41
+ - lib/artisan-memory-repository/project_repository.rb
42
+ - lib/artisan-memory-repository/story_repository.rb
43
+ - lib/artisan-memory-repository/user_repository.rb
44
+ homepage: http://8thlight.com
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 4406370865731914395
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.24
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Artisan Memory Repository
74
+ test_files: []
75
+