Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Apr 13, 2012
0 parents commit ed9c609
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*~
5 changes: 5 additions & 0 deletions History.rdoc
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

22 changes: 22 additions & 0 deletions LICENSE
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.
9 changes: 9 additions & 0 deletions Manifest.txt
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
55 changes: 55 additions & 0 deletions README.rdoc
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)
15 changes: 15 additions & 0 deletions Rakefile
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 }
24 changes: 24 additions & 0 deletions example/example.rb
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
60 changes: 60 additions & 0 deletions lib/flyweight.rb
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
34 changes: 34 additions & 0 deletions test/test_flyweight.rb
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
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'test/unit'
require_relative '../lib/flyweight'

0 comments on commit ed9c609

Please sign in to comment.