This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ed9c609
Showing
10 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
=== 0.0.1 2012-04-13 | ||
|
||
* 1 major enhancement: | ||
* Initial release | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(The MIT/X11 License) | ||
|
||
Copyright (c) 2012 Kenichi Kamiya | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
README.rdoc | ||
LICENSE | ||
History.rdoc | ||
Manifest.txt | ||
Rakefile | ||
lib/flyweight.rb | ||
test/test_helper.rb | ||
test/test_flyweight.rb | ||
example/example.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
= Flyweight | ||
|
||
* http://github.com/kachick/flyweight | ||
* http://rubygems.org/gems/flyweight | ||
* http://rubyforge.org/projects/flyweight | ||
|
||
== Description | ||
|
||
A tiny template for the "Flyweight Pattern" | ||
|
||
== Usage | ||
|
||
* setup | ||
|
||
require 'flyweight' | ||
|
||
* overview | ||
|
||
class Material | ||
include Flyweight | ||
|
||
def initialize(looks, price) | ||
@looks, @price = looks, price | ||
end | ||
end | ||
|
||
a = Material.value :metal, 2000 | ||
b = Material.value :metal, 2000 | ||
c = Material.new :metal, 2000 | ||
d = Material.value :paper, 50 | ||
a == b #=> true | ||
a.equal? b #=> true | ||
a == c #=> true | ||
a.equal? c #=> false | ||
a == d #=> false | ||
a.equal? d #=> false | ||
|
||
== Requirements | ||
|
||
* Ruby 1.9.2 or later | ||
|
||
target release versions | ||
|
||
* 1.9.3-p125 | ||
* 1.9.2-p290 | ||
|
||
== Installation | ||
|
||
* gem install flyweight | ||
|
||
== License | ||
|
||
Copyright (C) 2012 Kenichi Kamiya | ||
|
||
The MIT/X11 License (See the file LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
gem 'hoe', '~> 3.0.1' | ||
require 'hoe' | ||
require 'fileutils' | ||
|
||
Hoe.plugin :newgem | ||
|
||
$hoe = Hoe.spec 'flyweight' do | ||
developer 'Kenichi Kamiya', 'kachick1+ruby@gmail.com' | ||
self.rubyforge_name = name | ||
require_ruby_version '>= 1.9.2' | ||
dependency 'yard', '~> 0.7.5', :development | ||
end | ||
|
||
require 'newgem/tasks' | ||
Dir['tasks/**/*.rake'].each { |t| load t } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/local/bin/ruby -w | ||
|
||
$VERBOSE = true | ||
|
||
require_relative '../lib/flyweight' | ||
|
||
class Material | ||
include Flyweight | ||
|
||
def initialize(looks, price) | ||
@looks, @price = looks, price | ||
end | ||
end | ||
|
||
a = Material.value :metal, 2000 | ||
b = Material.value :metal, 2000 | ||
c = Material.new :metal, 2000 | ||
d = Material.value :paper, 50 | ||
p a == b #=> true | ||
p a.equal? b #=> true | ||
p a == c #=> true | ||
p a.equal? c #=> false | ||
p a == d #=> false | ||
p a.equal? d #=> false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# flyweight | ||
# A tiny template for the "Flyweight Pattern" | ||
|
||
# Copyright (C) 2012 Kenichi Kamiya | ||
|
||
# @example | ||
# class Material | ||
# include Flyweight | ||
# | ||
# def initialize(looks, price) | ||
# @looks, @price = looks, price | ||
# end | ||
# end | ||
# | ||
# a = Material.value :metal, 2000 | ||
# b = Material.value :metal, 2000 | ||
# c = Material.new :metal, 2000 | ||
# d = Material.value :paper, 50 | ||
# a == b #=> true | ||
# a.equal? b #=> true | ||
# a == c #=> true | ||
# a.equal? c #=> false | ||
# a == d #=> false | ||
# a.equal? d #=> false | ||
module Flyweight | ||
VERSION = '0.0.1'.freeze | ||
|
||
module Eigen | ||
def value(*values) | ||
pool = if singleton_class.const_defined? :FLYWEIGHT_POOL, false | ||
singleton_class::FLYWEIGHT_POOL | ||
else | ||
singleton_class.const_set :FLYWEIGHT_POOL, {} | ||
end | ||
|
||
if pool.has_key? values | ||
pool[values] | ||
else | ||
pool[values] = new(*values) | ||
end | ||
end | ||
end | ||
|
||
class << self | ||
def included(mod) | ||
raise ArgumentError unless mod.kind_of? ::Module | ||
|
||
mod.extend Eigen | ||
# mod.singleton_class.const_set :FLYWEIGHT_POOL, {} | ||
end | ||
end | ||
|
||
def ==(other) | ||
sources_for = ->obj{ | ||
obj.instance_variables.map{|var|obj.instance_variable_get var} | ||
} | ||
|
||
sources_for[self] == sources_for[other] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
$VERBOSE = true | ||
require_relative 'test_helper' | ||
|
||
class TestBasicCase < Test::Unit::TestCase | ||
class Material | ||
include Flyweight | ||
|
||
def initialize(looks, price) | ||
@looks, @price = looks, price | ||
end | ||
end | ||
|
||
class SubMaterial < Material | ||
end | ||
|
||
def test_normaly | ||
a = Material.value :metal, 2000 | ||
b = Material.value :metal, 2000 | ||
c = Material.new :metal, 2000 | ||
d = Material.value :paper, 50 | ||
e = SubMaterial.value :metal, 2000 | ||
assert_equal b, a | ||
assert_same b, a | ||
assert_equal c, a | ||
assert_same false, c.equal?(a) | ||
assert_same false, c.__id__.equal?(a.__id__) | ||
assert_same false, (d == a) | ||
assert_same false, d.equal?(a) | ||
assert_same false, d.__id__.equal?(a.__id__) | ||
assert_equal e, a | ||
assert_same false, e.equal?(a) | ||
assert_same false, e.__id__.equal?(a.__id__) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'test/unit' | ||
require_relative '../lib/flyweight' |