dm-enum 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.6)
5
+ coderay (0.9.8)
6
+ data_objects (0.10.6)
7
+ addressable (~> 2.1)
8
+ diff-lcs (1.1.3)
9
+ dm-core (1.1.0)
10
+ addressable (~> 2.2.4)
11
+ dm-do-adapter (1.1.0)
12
+ data_objects (~> 0.10.2)
13
+ dm-core (~> 1.1.0)
14
+ dm-migrations (1.1.0)
15
+ dm-core (~> 1.1.0)
16
+ dm-sqlite-adapter (1.1.0)
17
+ dm-do-adapter (~> 1.1.0)
18
+ do_sqlite3 (~> 0.10.2)
19
+ do_sqlite3 (0.10.6)
20
+ data_objects (= 0.10.6)
21
+ git (1.2.5)
22
+ jeweler (1.6.4)
23
+ bundler (~> 1.0)
24
+ git (>= 1.2.5)
25
+ rake
26
+ method_source (0.6.6)
27
+ ruby_parser (~> 2.0.5)
28
+ pry (0.9.6.2)
29
+ coderay (~> 0.9.8)
30
+ method_source (~> 0.6.5)
31
+ ruby_parser (~> 2.0.5)
32
+ slop (~> 2.1.0)
33
+ rake (0.9.2.2)
34
+ rspec (2.3.0)
35
+ rspec-core (~> 2.3.0)
36
+ rspec-expectations (~> 2.3.0)
37
+ rspec-mocks (~> 2.3.0)
38
+ rspec-core (2.3.1)
39
+ rspec-expectations (2.3.0)
40
+ diff-lcs (~> 1.1.2)
41
+ rspec-mocks (2.3.0)
42
+ ruby_parser (2.0.6)
43
+ sexp_processor (~> 3.0)
44
+ sexp_processor (3.0.9)
45
+ slop (2.1.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler (~> 1.0.0)
52
+ dm-core
53
+ dm-migrations
54
+ dm-sqlite-adapter
55
+ jeweler (~> 1.6.4)
56
+ pry
57
+ rspec (~> 2.3.0)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # dm-enums (Enumrated models for DataMapper)
1
+ # dm-enum (Enumrated models for DataMapper)
2
2
  by Potapov Sergey (aka Blake)
3
3
 
4
4
  Inspired by [power\_enum](https://github.com/albertosaurus/enumerations_mixin)
@@ -7,10 +7,18 @@ Inspired by [power\_enum](https://github.com/albertosaurus/enumerations_mixin)
7
7
 
8
8
  Allows you to cache enumeration models.
9
9
 
10
+
11
+ ## Installation
12
+
13
+ gem install dm-enum
14
+
10
15
  ## Usage
11
16
 
12
17
  ```ruby
13
18
 
19
+ require 'dm-core'
20
+ require 'dm-enum'
21
+
14
22
  class Language
15
23
  include DataMapper::Resource
16
24
  include DataMapper::Enum
@@ -33,7 +41,7 @@ Language["en"] # => #<Language @code="en" @name="English">
33
41
  ```
34
42
 
35
43
 
36
- ## Contributing to dm-enums
44
+ ## Contributing to dm-enum
37
45
 
38
46
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
39
47
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -0,0 +1,70 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dm-enum}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Potapov Sergey"]
12
+ s.date = %q{2012-03-02}
13
+ s.description = %q{Enumerated models for DataMapper to cache enumerated values in memory}
14
+ s.email = %q{blake131313@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "dm-enum.gemspec",
29
+ "lib/dm-enum.rb",
30
+ "lib/dm/enum.rb",
31
+ "spec/dm/enum_spec.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/greyblake/dm-enum}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.6.2}
38
+ s.summary = %q{Enumerated models for DataMapper}
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<dm-core>, [">= 0"])
45
+ s.add_development_dependency(%q<dm-migrations>, [">= 0"])
46
+ s.add_development_dependency(%q<dm-sqlite-adapter>, [">= 0"])
47
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
50
+ s.add_development_dependency(%q<pry>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<dm-core>, [">= 0"])
53
+ s.add_dependency(%q<dm-migrations>, [">= 0"])
54
+ s.add_dependency(%q<dm-sqlite-adapter>, [">= 0"])
55
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
56
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
58
+ s.add_dependency(%q<pry>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<dm-core>, [">= 0"])
62
+ s.add_dependency(%q<dm-migrations>, [">= 0"])
63
+ s.add_dependency(%q<dm-sqlite-adapter>, [">= 0"])
64
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
65
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
66
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
67
+ s.add_dependency(%q<pry>, [">= 0"])
68
+ end
69
+ end
70
+
@@ -0,0 +1 @@
1
+ require File.expand_path('enum', 'lib/dm')
@@ -0,0 +1,35 @@
1
+ module DataMapper
2
+ module Enum
3
+ def self.included(receiver)
4
+ receiver.extend ClassMethods
5
+ attr_reader :name_property
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_enumerated(options = {})
10
+ @name_property = options.delete(:name_property) || :name
11
+ @enums_cache = {}
12
+ end
13
+
14
+ def update_enums_cache!
15
+ @enums_cache = {}
16
+ @cache_update_flag = true
17
+ all.each do |model|
18
+ name = model.send(@name_property).to_sym
19
+ @enums_cache[name] = model
20
+ end
21
+ end
22
+
23
+ def [](name)
24
+ if model = @enums_cache[name.to_sym]
25
+ model
26
+ elsif !@cache_update_flag
27
+ raise RuntimeError.new("Looks like you forgot to call `#{self}.update_enums_cache!` before using `#{self}[#{name.inspect}]`")
28
+ else
29
+ nil
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMapper::Enum do
4
+ before :all do
5
+ Language.create(:code => "ru", :name => "Russian")
6
+ Country.create(:name => "Ukraine")
7
+ City.create(:name => "Kharkov")
8
+
9
+ Language.update_enums_cache!
10
+ Country.update_enums_cache!
11
+ end
12
+
13
+ describe '.[]' do
14
+ context 'name_property is other than default' do
15
+ context 'symbol passed' do
16
+ subject { Language[:ru] }
17
+
18
+ it { should be_an_instance_of Language }
19
+ its(:code) { should == 'ru' }
20
+ its(:name) { should == 'Russian' }
21
+ end
22
+
23
+ context 'string passed' do
24
+ subject { Language[:ru] }
25
+
26
+ it { should be_an_instance_of Language }
27
+ its(:code) { should == 'ru' }
28
+ its(:name) { should == 'Russian' }
29
+ end
30
+
31
+ context 'non-existing value' do
32
+ subject { Language[:wtf] }
33
+
34
+ it { should be_nil}
35
+ end
36
+ end
37
+
38
+ context 'default name_property' do
39
+ subject { Country[:Ukraine] }
40
+
41
+ it { should be_an_instance_of Country }
42
+ its(:name) { should == "Ukraine" }
43
+ end
44
+
45
+ context "forgot to call update_enums_cache!" do
46
+ it "raises an error with meaningful message" do
47
+ error_message = "Looks like you forgot to call `City.update_enums_cache!` before using `City[:Kharkov]`"
48
+ expect { City[:Kharkov] }.to raise_error(RuntimeError, error_message)
49
+ end
50
+ end
51
+ end
52
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: dm-core
17
- requirement: &17199560 !ruby/object:Gem::Requirement
17
+ requirement: &8845360 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *17199560
25
+ version_requirements: *8845360
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: dm-migrations
28
- requirement: &17198960 !ruby/object:Gem::Requirement
28
+ requirement: &8844680 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *17198960
36
+ version_requirements: *8844680
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: dm-sqlite-adapter
39
- requirement: &17198300 !ruby/object:Gem::Requirement
39
+ requirement: &8844140 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *17198300
47
+ version_requirements: *8844140
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rspec
50
- requirement: &17197640 !ruby/object:Gem::Requirement
50
+ requirement: &8843540 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 2.3.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *17197640
58
+ version_requirements: *8843540
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: bundler
61
- requirement: &17196920 !ruby/object:Gem::Requirement
61
+ requirement: &8842700 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.0.0
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *17196920
69
+ version_requirements: *8842700
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: jeweler
72
- requirement: &17196080 !ruby/object:Gem::Requirement
72
+ requirement: &8842040 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ~>
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 1.6.4
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *17196080
80
+ version_requirements: *8842040
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: pry
83
- requirement: &17195360 !ruby/object:Gem::Requirement
83
+ requirement: &8841380 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,7 +88,7 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *17195360
91
+ version_requirements: *8841380
92
92
  description: Enumerated models for DataMapper to cache enumerated values in memory
93
93
  email: blake131313@gmail.com
94
94
  executables: []
@@ -100,11 +100,16 @@ files:
100
100
  - .document
101
101
  - .rspec
102
102
  - Gemfile
103
+ - Gemfile.lock
103
104
  - LICENSE.txt
105
+ - README.md
104
106
  - Rakefile
105
107
  - VERSION
108
+ - dm-enum.gemspec
109
+ - lib/dm-enum.rb
110
+ - lib/dm/enum.rb
111
+ - spec/dm/enum_spec.rb
106
112
  - spec/spec_helper.rb
107
- - README.md
108
113
  has_rdoc: true
109
114
  homepage: http://github.com/greyblake/dm-enum
110
115
  licenses: