hashie-lazy_trash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.simplecov +1 -0
- data/Gemfile +8 -0
- data/Guardfile +9 -0
- data/LICENSE +22 -0
- data/README.md +30 -0
- data/Rakefile +2 -0
- data/hashie-lazy_trash.gemspec +17 -0
- data/lib/hashie-lazy_trash.rb +43 -0
- data/lib/hashie-lazy_trash/version.rb +1 -0
- data/spec/lazy_trash_spec.rb +48 -0
- data/spec/spec_helper.rb +6 -0
- metadata +60 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.simplecov
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
SimpleCov.start
|
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Christoph Olszowka
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Hashie::LazyTrash
|
2
|
+
|
3
|
+
An extended `Hashie::Trash` with the following 2 additional features:
|
4
|
+
|
5
|
+
1. When trying to set a value that is not defined as a property, the LazyTrash just sllently ignores this value
|
6
|
+
(as opposed to Hashie::Trash, which raises an Exception)
|
7
|
+
|
8
|
+
2. You can define properties that are to be fetched lazily (on first access), i.e.
|
9
|
+
|
10
|
+
property :user_id
|
11
|
+
property :user, :lazy => lambda { User.find(user_id) }
|
12
|
+
|
13
|
+
As you can see in this example, the Proc is executed in the context of the Hashie::LazyTrash instance
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
This gem relies on the yet-unreleased version 2 of the `hashie` gem. Therefore, you must get that dependency using bundler
|
18
|
+
from github manually for now:
|
19
|
+
|
20
|
+
# Gemfile
|
21
|
+
gem 'hashie', :github => 'intridea/hashie'
|
22
|
+
gem 'hashie-lazy_trash'
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/hashie-lazy_trash/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Christoph Olszowka"]
|
6
|
+
gem.email = ["christoph at olszowka.de"]
|
7
|
+
gem.description = %q{An extended version of Hashie::Trash}
|
8
|
+
gem.summary = %q{An extended version of Hashie::Trash}
|
9
|
+
gem.homepage = "https://github.com/colszowka/hashie-lazy_trash"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "hashie-lazy_trash"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = HashieLazyTrashVersion
|
17
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "hashie-lazy_trash/version"
|
2
|
+
require 'hashie'
|
3
|
+
|
4
|
+
#
|
5
|
+
# A Hashie::Trash with 2 main differences:
|
6
|
+
#
|
7
|
+
# 1. When trying to set a value that is not defined as a property, the LazyTrash just sllently ignores this value
|
8
|
+
# (as opposed to Hashie::Trash, which raises an Exception)
|
9
|
+
# 2. You can define properties that are to be fetched lazily (on first access), i.e.
|
10
|
+
#
|
11
|
+
# property :user_id
|
12
|
+
# property :user, lazy: ->{ User.find(user_id) }
|
13
|
+
#
|
14
|
+
# As you can see in this example, the Proc is executed in the context of the Hashie::LazyTrash instance.
|
15
|
+
#
|
16
|
+
class Hashie::LazyTrash < Hashie::Trash
|
17
|
+
def self.property(property_name, options = {})
|
18
|
+
super
|
19
|
+
|
20
|
+
if options[:lazy]
|
21
|
+
lazy_properties[property_name.to_sym] = options[:lazy]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def [](property)
|
26
|
+
if !super and self.class.lazy_properties.include?(property.to_sym)
|
27
|
+
self[property.to_sym] = instance_exec(&self.class.lazy_properties[property.to_sym])
|
28
|
+
else
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def self.lazy_properties
|
36
|
+
@lazy_properties ||= {}
|
37
|
+
end
|
38
|
+
|
39
|
+
# If the property does not exist, just returns false and therefore denies setting a value to it
|
40
|
+
def property_exists?(property)
|
41
|
+
self.class.property?(property.to_sym)
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
HashieLazyTrashVersion = '0.0.1'
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hashie::LazyTrash do
|
4
|
+
class ExampleTrash < Hashie::LazyTrash
|
5
|
+
User = ""
|
6
|
+
property :name
|
7
|
+
property :email, :transform_with => lambda { |val| val.downcase }
|
8
|
+
property :created_at, :from => :time, :with => lambda { |t| Time.parse(t) }
|
9
|
+
property :user, :lazy => lambda { User.find_by_name(name) }
|
10
|
+
end
|
11
|
+
|
12
|
+
it "accepts a name" do
|
13
|
+
ExampleTrash.new(:name => 'Christoph').name.should == 'Christoph'
|
14
|
+
ExampleTrash.new("name" => 'Christoph').name.should == 'Christoph'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "accepts an email and downcases it" do
|
18
|
+
ExampleTrash.new(:email => 'Christoph@Example.com').email.should == 'christoph@example.com'
|
19
|
+
end
|
20
|
+
|
21
|
+
it "accepts a Time string and parses it" do
|
22
|
+
time = Time.now
|
23
|
+
ExampleTrash.new(:time => time.iso8601).created_at.to_i.should == time.to_i
|
24
|
+
end
|
25
|
+
|
26
|
+
it "allows to set created_at directly" do
|
27
|
+
time = Time.now
|
28
|
+
ExampleTrash.new(:created_at => time).created_at.should == time
|
29
|
+
end
|
30
|
+
|
31
|
+
it "silently ignores undefined properties" do
|
32
|
+
lazy_trash = ExampleTrash.new(:undefined => 'foo')
|
33
|
+
expect { lazy_trash.undefined }.to raise_error(NoMethodError)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "evaluates lazy attributes on first access and caches them" do
|
37
|
+
ExampleTrash::User.should_receive(:find_by_name).with('Christoph').once.and_return('A User object')
|
38
|
+
lazy_trash = ExampleTrash.new(:name => 'Christoph')
|
39
|
+
lazy_trash.user.should == 'A User object'
|
40
|
+
lazy_trash.user.should == 'A User object'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "allows to set lazy attributes explicitly" do
|
44
|
+
ExampleTrash::User.should_not_receive(:find_by_name)
|
45
|
+
lazy_trash = ExampleTrash.new(:user => 'A user')
|
46
|
+
lazy_trash.user.should == 'A user'
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashie-lazy_trash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Christoph Olszowka
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-14 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: An extended version of Hashie::Trash
|
15
|
+
email:
|
16
|
+
- christoph at olszowka.de
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- .simplecov
|
24
|
+
- Gemfile
|
25
|
+
- Guardfile
|
26
|
+
- LICENSE
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- hashie-lazy_trash.gemspec
|
30
|
+
- lib/hashie-lazy_trash.rb
|
31
|
+
- lib/hashie-lazy_trash/version.rb
|
32
|
+
- spec/lazy_trash_spec.rb
|
33
|
+
- spec/spec_helper.rb
|
34
|
+
homepage: https://github.com/colszowka/hashie-lazy_trash
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.24
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: An extended version of Hashie::Trash
|
58
|
+
test_files:
|
59
|
+
- spec/lazy_trash_spec.rb
|
60
|
+
- spec/spec_helper.rb
|