Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit ed9c609c9c270ed75be8b9f29c5d20b877f6b2b0 0 parents
@kachick authored
1  .gitignore
@@ -0,0 +1 @@
+*~
5 History.rdoc
@@ -0,0 +1,5 @@
+=== 0.0.1 2012-04-13
+
+* 1 major enhancement:
+ * Initial release
+
22 LICENSE
@@ -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.
9 Manifest.txt
@@ -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
55 README.rdoc
@@ -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)
15 Rakefile
@@ -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 }
24 example/example.rb
@@ -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
60 lib/flyweight.rb
@@ -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
34 test/test_flyweight.rb
@@ -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
2  test/test_helper.rb
@@ -0,0 +1,2 @@
+require 'test/unit'
+require_relative '../lib/flyweight'
Please sign in to comment.
Something went wrong with that request. Please try again.