frakzio 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in frakzio.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,29 @@
1
+ Frakzio
2
+ ==========
3
+
4
+ This gem helps to write a fraction like attributes of a ActiveRecord models.
5
+
6
+ Usage
7
+ -----
8
+
9
+ ### Add fraction capability to your ActiveRecord model
10
+
11
+ class Window < ActiveRecord::Base
12
+ act_as_frakzio :width, :height
13
+ end
14
+
15
+ Make sure that the frakzio attributes are string type.
16
+
17
+ ### Fraction normalizer
18
+
19
+ w = Window.create(:width => 10.5, :height => "10 8/4")
20
+ w.width
21
+ > "10 1/2"
22
+ w.height
23
+ > "12"
24
+
25
+
26
+ Copyright
27
+ ---------
28
+
29
+ Copyright © 2011 Piotr Dębosz, Adam Lipka.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ Frakzio
2
+ ==========
3
+
4
+ This gem helps to write a fraction like attributes of a ActiveRecord models.
5
+
6
+ Usage
7
+ -----
8
+
9
+ ### Add fraction capability to your ActiveRecord model
10
+
11
+ class Window < ActiveRecord::Base
12
+ act_as_frakzio :width, :height
13
+ end
14
+
15
+ Make sure that the frakzio attributes are string type.
16
+
17
+ ### Fraction normalizer
18
+
19
+ w = Window.create(:width => 10.5, :height => "10 8/4")
20
+ w.width
21
+ > "10 1/2"
22
+ w.height
23
+ > "12"
24
+
25
+
26
+ Copyright
27
+ ---------
28
+
29
+ Copyright © 2011 Piotr Dębosz, Adam Lipka.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/frakzio.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "frakzio/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "frakzio"
7
+ s.version = Frakzio::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Adam Lipka, Piotr Dębosz"]
10
+ s.email = ["adam.lipka@gmail.com, piotr.debosz@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = "%Write a gem summary"
13
+ s.description = "%Write a gem description"
14
+
15
+ s.rubyforge_project = "frakzio"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
data/lib/frakzio.rb ADDED
@@ -0,0 +1,107 @@
1
+ module Frakzio
2
+ require File.dirname(__FILE__)+'/frakzio/railtie.rb' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
3
+
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def act_as_frakzio(*attributes)
10
+ attributes.each do |attribute|
11
+ validates attribute, :frakzio => true
12
+
13
+ #setter
14
+ define_method((attribute.to_s + "=").to_sym) do |value|
15
+ fraction_valid?(value) ? write_attribute(attribute, frakzionize(value)) : write_attribute(attribute, value)
16
+ end
17
+
18
+ #getter
19
+ define_method(attribute) do
20
+ read_attribute(attribute) || ""
21
+ end
22
+ end
23
+
24
+ include InstanceMethods
25
+ end
26
+ end
27
+
28
+ module InstanceMethods
29
+
30
+ def fraction_valid?(value)
31
+ if value
32
+ value = value.to_s
33
+ (value.match(/\A\d* *\d*\/?\d*\z/).to_s == value || value.match(/\A\d*\.?\d*\z/).to_s == value) ? true : false
34
+ end
35
+ end
36
+
37
+ def frakzionize(s = "")
38
+ s = "" unless s
39
+ s = s.to_s
40
+ if s.include?('.')
41
+ frac = fraction_to_s(decimal_to_array(s))
42
+ else if s.include?('/')
43
+ frac = fraction_to_s(fraction_to_array(s))
44
+ else
45
+ s == "" ? s = nil : s = s
46
+ frac = s
47
+ end
48
+ end
49
+ frac
50
+ end
51
+
52
+ def nwd(a,b)
53
+ while b!=0 do
54
+ a,b = b,a%b
55
+ end
56
+
57
+ return a
58
+ end
59
+
60
+ def simplify(array)
61
+ index = array.size - 2
62
+ entires = index == 0 ? 0 : array[index-1] ? array[index-1] : 0
63
+
64
+ rest = array[index]%array[index+1]
65
+ entires += array[index]/array[index+1]
66
+ n = nwd(rest, array[index+1])
67
+ numerator = rest/n
68
+ denominator = array[index+1]/n
69
+
70
+ numerator = denominator = nil if numerator == 0
71
+
72
+ [entires, numerator, denominator]
73
+ end
74
+
75
+ def decimal_to_array(d)
76
+ precision = 10**6
77
+ numerator = (d.to_f*precision).to_i
78
+
79
+ simplify([numerator, precision])
80
+ end
81
+
82
+ def fraction_to_array(d)
83
+ result = []
84
+ if d.include?(" ")
85
+ result << d.split(" ").first.to_i
86
+ result.concat get_fraction(d.split(" ").last)
87
+ else
88
+ result << nil
89
+ result.concat get_fraction(d)
90
+ end
91
+
92
+ simplify(result)
93
+ end
94
+
95
+ def get_fraction(s)
96
+ s.split("/").map {|x| x.to_i}
97
+ end
98
+
99
+ def fraction_to_s(frac)
100
+ result = ""
101
+ result += frac[0].to_s if frac[0] && frac[0] != 0
102
+ result += " " if frac[0] && frac[0] != 0 && frac[1]
103
+ result += "#{frac[1]}/#{frac[2]}" if frac[1] && frac[2]
104
+ result
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,15 @@
1
+ module Frakzio
2
+
3
+ require 'frakzio'
4
+ require 'rails'
5
+ require 'frakzio_validator'
6
+
7
+ class Railtie < Rails::Railtie
8
+
9
+ initializer 'frakzio.initialize' do
10
+ ActiveSupport.on_load(:active_record) do
11
+ ActiveRecord::Base.send :include, Frakzio
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Frakzio
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ class FrakzioValidator < ActiveModel::EachValidator
2
+ # implement the method called during validation
3
+ def validate_each(record, attribute, value)
4
+ if value
5
+ value = value.to_s
6
+ record.errors[attribute] << 'invalid format' unless value.match(/\A\d* *\d*\/?\d*\z/).to_s == value || value.match(/\A\d*\.?\d*\z/).to_s == value
7
+ end
8
+ end
9
+ end
data/spec.db ADDED
Binary file
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ describe Frakzio do
4
+ it "should be valid with assigned standard fraction" do
5
+ item = Item.new(:width => "5 5/7")
6
+ item.valid?.should == true
7
+ end
8
+
9
+ it "should be valid with assigned string/decimal value" do
10
+ item = Item.new(:width => "10.7")
11
+ item.valid?.should == true
12
+ end
13
+
14
+ it "should be valid with assigned decimal value" do
15
+ item = Item.new(:width => 0.7)
16
+ item.valid?.should == true
17
+ end
18
+
19
+ it "should be valid with assigned integer value" do
20
+ item = Item.new(:width => 5)
21
+ item.valid?.should == true
22
+ end
23
+
24
+ it "shouldn't be valid with assigned alpha type string" do
25
+ item = Item.new(:width => "dsadsadas")
26
+ item.valid?.should == false
27
+ end
28
+
29
+ it "shouldn't be valid with assigned alpha type string" do
30
+ item = Item.new(:width => "..........")
31
+ item.valid?.should == false
32
+ end
33
+
34
+ it "shouldn't be valid with numeric/dot type string" do
35
+ item = Item.new(:width => "10.01.01")
36
+ item.valid?.should == false
37
+ end
38
+
39
+ it "shouldn't be valid with numeric/slash like string" do
40
+ item = Item.new(:width => "10 3/6/8")
41
+ item.valid?.should == false
42
+ end
43
+
44
+ it "should assign a float and return fraction string representation" do
45
+ item = Item.create(:width => 10.8)
46
+ item.width.should == "10 4/5"
47
+ end
48
+
49
+ it "should assign a float like string and return the fraction string representation" do
50
+ item = Item.create(:width => "10.6")
51
+ item.width.should == "10 3/5"
52
+ end
53
+
54
+ it "should assign a regular fraction string and return the fraction string representation" do
55
+ item = Item.create(:width => "10 24/12")
56
+ item.width.should == "12"
57
+ end
58
+
59
+ it "should assign a regular fraction string and return the fraction string representation" do
60
+ item = Item.create(:width => "100/12")
61
+ item.width.should == "8 1/3"
62
+ end
63
+ end
@@ -0,0 +1,33 @@
1
+ require "rspec"
2
+ require 'sqlite3'
3
+ require 'active_record'
4
+
5
+ SPEC_DIR = File.dirname(__FILE__)
6
+ lib_path = File.expand_path("#{SPEC_DIR}/../lib")
7
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
8
+ require "frakzio"
9
+ require "frakzio_validator"
10
+
11
+ ActiveRecord::Base.send :include, Frakzio
12
+
13
+ ActiveRecord::Base.establish_connection(
14
+ :adapter => "sqlite3",
15
+ :database => "spec.db"
16
+ )
17
+
18
+ class Item < ActiveRecord::Base
19
+ act_as_frakzio :width
20
+ end
21
+
22
+ if Item.table_exists?
23
+ ActiveRecord::Base.connection.drop_table(:items)
24
+ end
25
+
26
+ if !Item.table_exists?
27
+ ActiveRecord::Base.connection.create_table(:items) do |i|
28
+ i.column :id, :integer
29
+ i.column :width, :string
30
+ i.column :created_at, :datetime
31
+ i.column :updated_at, :datetime
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: frakzio
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - "Adam Lipka, Piotr D\xC4\x99bosz"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-27 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: "%Write a gem description"
23
+ email:
24
+ - adam.lipka@gmail.com, piotr.debosz@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README
35
+ - README.md
36
+ - Rakefile
37
+ - frakzio.gemspec
38
+ - lib/frakzio.rb
39
+ - lib/frakzio/railtie.rb
40
+ - lib/frakzio/version.rb
41
+ - lib/frakzio_validator.rb
42
+ - spec.db
43
+ - spec/frakzio_spec.rb
44
+ - spec/spec_helper.rb
45
+ has_rdoc: true
46
+ homepage: ""
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project: frakzio
75
+ rubygems_version: 1.6.2
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: "%Write a gem summary"
79
+ test_files:
80
+ - spec/frakzio_spec.rb
81
+ - spec/spec_helper.rb