lazy_model_const 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem 'lazy_const'
6
+
7
+ # Add dependencies to develop your gem here.
8
+ # Include everything needed to run rake, tests, features, etc.
9
+ group :development do
10
+ gem "minitest", ">= 0"
11
+ gem "rdoc", "~> 3.12"
12
+ gem "bundler"
13
+ gem "jeweler", "~> 1.8.4"
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Ryan Graham
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ = lazy_model_const
2
+
3
+ A convenient variant of +lazy_const+ with an eye towards ActiveRecord models.
4
+
5
+ == Installation
6
+
7
+ gem 'lazy_model_const'
8
+
9
+ == Example
10
+
11
+ Here's an example using Rails. Really, any class that has a class method +find_or_create_by_name+ can be used.
12
+ If you're not using ActiveRecord, you probably want to use the +lazy_const+ gem.
13
+
14
+ app/model/group.rb:
15
+ class Group < ActiveRecord::Base
16
+ extend LazyConst
17
+ lazy_const :EVERYONE, name: 'Everyone'
18
+ end
19
+
20
+ app/model/user.rb:
21
+ class User < ActiveRecord::Base
22
+ has_many :groups
23
+ after_create :add_to_everyone
24
+ def add_to_everyone
25
+ groups << Group.EVERYONE
26
+ end
27
+ end
28
+
29
+ == Contributing to lazy_model_const
30
+
31
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
32
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
33
+ * Fork the project.
34
+ * Start a feature/bugfix branch.
35
+ * Commit and push until you are happy with your contribution.
36
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
37
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
38
+
39
+ == Copyright
40
+
41
+ Copyright (c) 2013 Ryan Graham. See LICENSE.txt for further details. (TL;DR: MIT license)
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "lazy_model_const"
18
+ gem.homepage = "http://github.com/rmg/lazy_model_const"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A version of lazy_const specifically for ActiveRecord models}
21
+ gem.description = %Q{Allows you to define lazy evaluated, constant-like, instances of your models}
22
+ gem.email = "r.m.graham@gmail.com"
23
+ gem.authors = ["Ryan Graham"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "lazy_model_const #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,62 @@
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 = "lazy_model_const"
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Graham"]
12
+ s.date = "2013-03-13"
13
+ s.description = "Allows you to define lazy evaluated, constant-like, instances of your models"
14
+ s.email = "r.m.graham@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lazy_model_const.gemspec",
27
+ "lib/lazy_model_const.rb",
28
+ "lib/lazy_model_const/spec.rb",
29
+ "test/helper.rb",
30
+ "test/test_lazy_model_const.rb"
31
+ ]
32
+ s.homepage = "http://github.com/rmg/lazy_model_const"
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = "1.8.23"
36
+ s.summary = "A version of lazy_const specifically for ActiveRecord models"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<lazy_const>, [">= 0"])
43
+ s.add_development_dependency(%q<minitest>, [">= 0"])
44
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
45
+ s.add_development_dependency(%q<bundler>, [">= 0"])
46
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
47
+ else
48
+ s.add_dependency(%q<lazy_const>, [">= 0"])
49
+ s.add_dependency(%q<minitest>, [">= 0"])
50
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
51
+ s.add_dependency(%q<bundler>, [">= 0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<lazy_const>, [">= 0"])
56
+ s.add_dependency(%q<minitest>, [">= 0"])
57
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
58
+ s.add_dependency(%q<bundler>, [">= 0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
60
+ end
61
+ end
62
+
@@ -0,0 +1,10 @@
1
+ require 'lazy_const'
2
+
3
+ module LazyModelConst
4
+ include LazyConst
5
+ def lazy_model_const(name, attrs = {})
6
+ lazy_const name do
7
+ find_or_create_by_name(attrs)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ require 'lazy_const/spec'
2
+
3
+ module LazyModelConst::ExtendedModelMixins
4
+ def defines_model_consts(*consts)
5
+ defines_consts(*consts)
6
+ consts.each do |const|
7
+ it "defined constant #{const} is a valid #{described_class}" do
8
+ described_class.public_send(const.to_sym).should be_valid
9
+ end
10
+ end
11
+ end
12
+ alias_method :defines_model_const, :defines_model_consts
13
+ end
14
+
15
+ RSpec.configure do |config|
16
+ config.extend LazyModelConst::ExtendedModelMixins, type: :model
17
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'minitest/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'lazy_model_const'
15
+
16
+ class MiniTest::Unit::TestCase
17
+ end
18
+
19
+ MiniTest::Unit.autorun
@@ -0,0 +1,23 @@
1
+ require 'helper'
2
+
3
+ $called = 0
4
+ class SomeModel < OpenStruct
5
+ extend LazyModelConst
6
+ lazy_model_const :IMPORTANT_INSTANCE, name: "imp", desc: "Really really important"
7
+ def self.find_or_create_by_name(attrs = {})
8
+ $called += 1
9
+ new(attrs)
10
+ end
11
+ end
12
+
13
+ class TestLazyModelConst < MiniTest::Unit::TestCase
14
+ def test_lazy_model_const
15
+ assert_equal $called, 0
16
+ instance = SomeModel.IMPORTANT_INSTANCE
17
+ assert_equal $called, 1
18
+ assert_equal instance, SomeModel.IMPORTANT_INSTANCE
19
+ assert_equal $called, 1
20
+ assert_equal instance, SomeModel.IMPORTANT_INSTANCE
21
+ assert_equal $called, 1
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_model_const
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Graham
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ none: false
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ name: lazy_const
23
+ requirement: !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ type: :development
38
+ name: minitest
39
+ requirement: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ version: '3.12'
53
+ type: :development
54
+ name: rdoc
55
+ requirement: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '3.12'
61
+ prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ type: :development
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ prerelease: false
78
+ - !ruby/object:Gem::Dependency
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ~>
83
+ - !ruby/object:Gem::Version
84
+ version: 1.8.4
85
+ type: :development
86
+ name: jeweler
87
+ requirement: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ version: 1.8.4
93
+ prerelease: false
94
+ description: Allows you to define lazy evaluated, constant-like, instances of your
95
+ models
96
+ email: r.m.graham@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.rdoc
102
+ files:
103
+ - .document
104
+ - Gemfile
105
+ - LICENSE.txt
106
+ - README.rdoc
107
+ - Rakefile
108
+ - VERSION
109
+ - lazy_model_const.gemspec
110
+ - lib/lazy_model_const.rb
111
+ - lib/lazy_model_const/spec.rb
112
+ - test/helper.rb
113
+ - test/test_lazy_model_const.rb
114
+ homepage: http://github.com/rmg/lazy_model_const
115
+ licenses:
116
+ - MIT
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ segments:
127
+ - 0
128
+ hash: -2054573700381018630
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 1.8.23
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: A version of lazy_const specifically for ActiveRecord models
142
+ test_files: []