typecaster 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Rakefile +3 -0
- data/lib/typecaster.rb +83 -0
- data/lib/typecaster/version.rb +3 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/typecaster_spec.rb +65 -0
- data/typecaster.gemspec +24 -0
- metadata +91 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/lib/typecaster.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "typecaster/version"
|
3
|
+
|
4
|
+
module Typecaster
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def attribute(name, options={})
|
9
|
+
attribute_name = name.to_sym
|
10
|
+
|
11
|
+
raw_attributes[attribute_name] = options
|
12
|
+
attributes[attribute_name] = nil
|
13
|
+
|
14
|
+
define_method(name) do
|
15
|
+
if instance_variable_defined?("@#{name}")
|
16
|
+
instance_variable_get("@#{name}")
|
17
|
+
elsif raw_attributes[attribute_name].has_key?(:default)
|
18
|
+
define_instance_variable(name, raw_attributes[attribute_name][:default])
|
19
|
+
else
|
20
|
+
define_instance_variable(name, nil)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
define_method("#{name}=") do |val|
|
25
|
+
define_instance_variable(name, val)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def attributes
|
30
|
+
@attributes ||= Hash.new
|
31
|
+
end
|
32
|
+
|
33
|
+
def raw_attributes
|
34
|
+
@raw_attributes ||= Hash.new
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def initialize(attributes={})
|
39
|
+
raw_attributes.each do |name, attributes|
|
40
|
+
if attributes.has_key?(:default)
|
41
|
+
define_instance_variable(name, attributes[:default])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
attributes.each do |key, value|
|
46
|
+
send "#{key}=", value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def attributes
|
51
|
+
@attributes ||= self.class.attributes
|
52
|
+
end
|
53
|
+
|
54
|
+
def raw_attributes
|
55
|
+
@raw_attributes ||= self.class.raw_attributes
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_row
|
59
|
+
attributes.values.join("")
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def typecaster
|
65
|
+
{}
|
66
|
+
end
|
67
|
+
|
68
|
+
def typecasted_attribute(attribute_name, options)
|
69
|
+
type = options[:type]
|
70
|
+
typecast_attribute(type).call(options[:value], options)
|
71
|
+
end
|
72
|
+
|
73
|
+
def typecast_attribute(type)
|
74
|
+
typecaster[type].new if typecaster
|
75
|
+
end
|
76
|
+
|
77
|
+
def define_instance_variable(name, val)
|
78
|
+
raw_attributes[name][:value] = val
|
79
|
+
val = typecasted_attribute(name, raw_attributes[name])
|
80
|
+
attributes[name] = val
|
81
|
+
instance_variable_set("@#{name}", val)
|
82
|
+
end
|
83
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class StringTypecaster
|
4
|
+
def call(value, options)
|
5
|
+
value.to_s.ljust(options[:size], " ")
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class IntegerTypecaster
|
10
|
+
def call(value, options)
|
11
|
+
value.to_s.rjust(options[:size], "0")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ObjectFormatter
|
16
|
+
include Typecaster
|
17
|
+
|
18
|
+
attribute :name, :type => "string", :size => 10
|
19
|
+
attribute :age, :type => "integer", :size => 3
|
20
|
+
attribute :identification, :type => "string", :size => 5, :default => "*"
|
21
|
+
|
22
|
+
def typecaster
|
23
|
+
{
|
24
|
+
"string" => StringTypecaster,
|
25
|
+
"integer" => IntegerTypecaster
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe Typecaster do
|
31
|
+
context "without values" do
|
32
|
+
subject do
|
33
|
+
ObjectFormatter.new
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return row" do
|
37
|
+
subject.to_row.should eq "* "
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with values" do
|
42
|
+
subject do
|
43
|
+
ObjectFormatter.new(:name => "Ricardo", :age => 23, :identification => "R")
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return formatted name" do
|
47
|
+
subject.name.should eq "Ricardo "
|
48
|
+
subject.attributes[:name].should eq "Ricardo "
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return formatted age" do
|
52
|
+
subject.age.should eq "023"
|
53
|
+
subject.attributes[:age].should eq "023"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return identification with default value" do
|
57
|
+
subject.identification.should eq "R "
|
58
|
+
subject.attributes[:identification].should eq "R "
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should return row" do
|
62
|
+
subject.to_row.should eq "Ricardo 023R "
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/typecaster.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "typecaster/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "typecaster"
|
7
|
+
s.version = Typecaster::VERSION
|
8
|
+
s.authors = ["Ricardo H."]
|
9
|
+
s.email = ["ricardohsd@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Ease create plain old ruby object with formatted values}
|
12
|
+
s.description = %q{Typecaster make easy the job of create plain old ruby object with formatted values}
|
13
|
+
|
14
|
+
s.rubyforge_project = "typecaster"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "activesupport", ">= 3.2.1"
|
22
|
+
s.add_development_dependency "rspec", ">= 2.8.0"
|
23
|
+
s.add_development_dependency 'rake', '>= 0.8.7'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: typecaster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ricardo H.
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &2157487960 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2157487960
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &2157486980 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.8.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2157486980
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &2157486420 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.8.7
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2157486420
|
47
|
+
description: Typecaster make easy the job of create plain old ruby object with formatted
|
48
|
+
values
|
49
|
+
email:
|
50
|
+
- ricardohsd@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .rspec
|
57
|
+
- Gemfile
|
58
|
+
- Rakefile
|
59
|
+
- lib/typecaster.rb
|
60
|
+
- lib/typecaster/version.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
- spec/typecaster_spec.rb
|
63
|
+
- typecaster.gemspec
|
64
|
+
homepage: ''
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project: typecaster
|
84
|
+
rubygems_version: 1.8.15
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Ease create plain old ruby object with formatted values
|
88
|
+
test_files:
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- spec/typecaster_spec.rb
|
91
|
+
has_rdoc:
|