tableless_model 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,9 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tableless_model (0.0.1)
5
- activerecord
6
- hashie
4
+ vitobotta/tableless_model (0.0.1)
7
5
  validatable
8
6
 
9
7
  GEM
@@ -21,20 +19,8 @@ GEM
21
19
  activesupport (3.0.3)
22
20
  arel (2.0.6)
23
21
  builder (2.1.2)
24
- diff-lcs (1.1.2)
25
- hashie (0.4.0)
26
22
  i18n (0.5.0)
27
- mocha (0.9.10)
28
- rake
29
- rake (0.8.7)
30
- rspec (2.3.0)
31
- rspec-core (~> 2.3.0)
32
- rspec-expectations (~> 2.3.0)
33
- rspec-mocks (~> 2.3.0)
34
- rspec-core (2.3.1)
35
- rspec-expectations (2.3.0)
36
- diff-lcs (~> 1.1.2)
37
- rspec-mocks (2.3.0)
23
+ minitest (2.0.2)
38
24
  tzinfo (0.3.23)
39
25
  validatable (1.6.7)
40
26
 
@@ -43,8 +29,6 @@ PLATFORMS
43
29
 
44
30
  DEPENDENCIES
45
31
  activerecord
46
- hashie
47
- mocha
48
- rspec
49
- tableless_model!
32
+ minitest
50
33
  validatable
34
+ vitobotta/tableless_model!
data/README.rdoc CHANGED
@@ -64,7 +64,7 @@ Using Tableless Model, we could remove the association and the table seo_options
64
64
 
65
65
  class SeoOptions < ActiveRecord::TablelessModel
66
66
 
67
- attribute :title_tag, :type => :string, :default => ""
67
+ attribute :title_tag, :type => :string, :default => "default title tag"
68
68
  attribute :meta_description, :type => :string, :default => ""
69
69
  attribute :meta_keywords, :type => :string, :default => ""
70
70
  attribute :noindex, :type => :boolean, :default => false
@@ -74,14 +74,48 @@ Using Tableless Model, we could remove the association and the table seo_options
74
74
  end
75
75
 
76
76
 
77
- That's it. Each instance of Page will now store directly its YAML-serialized SEO settings in the column named "seo". This is how the content of that column would look like in the database:
77
+ That's it.
78
+
79
+ When you now create an instance of SeoOptions, you can get and set its attributes as you would do with a normal model:
80
+
81
+ seo_options = SeoOptions.new
82
+ => <#SeoOptions meta_description="" meta_keywords="" noarchive=false nofollow=false noindex=false
83
+ title_tag="default title tag">
84
+
85
+ seo_options.title_tag
86
+ => "default title tag"
87
+
88
+ seo_options.title_tag = "new title tag"
89
+ => "new title tag"
90
+
91
+ Note that inspect shows the properties of the Tableless Model in the same way it does for ActiveRecord models.
92
+ Of course, you can also override the default values for the attributes when creating a new instance:
93
+
94
+ seo_options = SeoOptions.new( :title_tag => "a different title tag" )
95
+ => <#SeoOptions meta_description="" meta_keywords="" noarchive=false nofollow=false noindex=false
96
+ title_tag="a different title tag">
97
+
98
+ Now, if you have used the has_tabless macro in the parent class, Page, each instance of Page will store directly its YAML-serialized SEO settings in the column named "seo".
99
+
100
+ page = Page.new
101
+
102
+ page.seo
103
+ => <#SeoOptions meta_description="" meta_keywords="" noarchive=false nofollow=false noindex=false
104
+ title_tag="default title tag">
105
+
106
+ page.seo.title_tag = "changed title tag"
107
+ => <#SeoOptions meta_description="" meta_keywords="" noarchive=false nofollow=false noindex=false
108
+ title_tag="changed title tag">
109
+
110
+
111
+ And this is how the content of the serialized column would look like in the database if you saved the changes as in the example
78
112
 
79
113
  --- !map:SeoOptions
80
114
  noarchive: false
81
115
  meta_description:
82
116
  meta_keywords:
83
117
  nofollow: false
84
- title_tag:
118
+ title_tag: "changed title tag"
85
119
  noindex: false
86
120
 
87
121
 
@@ -62,20 +62,21 @@ module Tableless
62
62
  begin
63
63
  case type
64
64
  when :string then (value.is_a?(String) ? value : String(value))
65
- when :integer then (value.is_a?(Integer) ? value : Integer(value))
66
- when :float then (value.is_a?(Float) ? value : Float(value))
67
- when :decimal then (value.is_a?(Float) ? value : Float(value))
65
+ when :integer then (value.is_a?(Integer) ? value : value.to_s.to_i)
66
+ when :float then (value.is_a?(Float) ? value : value.to_s.to_f)
67
+ when :decimal then (value.is_a?(BigDecimal) ? value : BigDecimal(value.to_s))
68
68
  when :time then (value.is_a?(Time) ? value : Time.parse(value))
69
69
  when :date then (value.is_a?(Date) ? value : Date.parse(value))
70
70
  when :datetime then (value.is_a?(DateTime) ? value : DateTime.parse(value))
71
- when :boolean then (value == true || value == 1 || value.to_s =~ /^(true|1)$/i)
71
+ when :boolean then (["true", "1"].include?(value.to_s))
72
72
  else value
73
73
  end
74
74
  rescue Exception => e
75
- raise StandardError, "Invalid value '#{value.inspect}' for attribute #{attribute_name} - expected data type is #{type} but value is a #{value.class} (Exception details: #{e})"
75
+ raise StandardError, "Invalid value #{value.inspect} for attribute #{attribute_name} - expected data type is #{type.to_s.capitalize} but value is a #{value.class} (Exception details: #{e})"
76
76
  value
77
77
  end
78
78
  end
79
79
 
80
80
  end
81
- end
81
+ end
82
+
@@ -1,3 +1,3 @@
1
1
  module TablelessModel
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,8 +1,93 @@
1
+ require "rubygems"
2
+ require "active_record"
1
3
  require "lib/tableless_model"
4
+ require 'minitest/autorun'
2
5
 
3
- describe ActiveRecord::TablelessModel do
4
- it "should say bla" do
5
- true
6
+ class TestTablelessModel < ActiveRecord::TablelessModel
7
+ end
8
+
9
+
10
+ describe TestTablelessModel do
11
+ it "has the accessor 'attributes', originally an empty hash" do
12
+ TestTablelessModel.must_respond_to("attributes")
13
+ TestTablelessModel.attributes.must_be_kind_of Hash
14
+ TestTablelessModel.attributes.must_be_empty
6
15
  end
7
16
 
17
+ it "responds to 'attribute'" do
18
+ TestTablelessModel.must_respond_to "attribute"
19
+ end
20
+
21
+ it "responds to 'attribute', 'cast'" do
22
+ TestTablelessModel.must_respond_to "attribute"
23
+ TestTablelessModel.must_respond_to "cast"
24
+ end
25
+
26
+ describe "'attribute' macro" do
27
+ before do
28
+ TestTablelessModel.class_eval do
29
+ attribute :test_attribute
30
+ end
31
+ end
32
+
33
+ it "adds a key-value pair to the attributes accessor" do
34
+ TestTablelessModel.attributes.wont_be_empty
35
+ TestTablelessModel.attributes.key?("test_attribute").must_equal true, "An attribute named 'test_attribute' should have been defined"
36
+ end
37
+ end
38
+
39
+ describe "An instance of TablelessModel" do
40
+ before do
41
+ TestTablelessModel.class_eval do
42
+ attribute :test_attribute
43
+ end
44
+ end
45
+
46
+ it "tries to enforce type casting if a type has been specified for an attribute" do
47
+
48
+ test_values = [ "test", 1234, true, "1234.12", "2011-01-02 15:23" ]
49
+
50
+ [ :string, :integer, :float, :decimal, :time, :date, :datetime, :boolean ].each do |type|
51
+
52
+ # temporarily changing type
53
+ TestTablelessModel.attributes["test_attribute"][:type] = type
54
+
55
+ instance = TestTablelessModel.new
56
+
57
+ # instance.must_respond_to "test_attribute", "Getter for test_attribute should have been defined"
58
+ # instance.must_respond_to "test_attribute=", "Setter for test_attribute should have been defined"
59
+
60
+ type_name = case type
61
+ when :datetime then :date_time
62
+ when :decimal then :big_decimal
63
+ else type
64
+ end
65
+
66
+
67
+ # excluding some test values that would always faild depending on the type
68
+ exclude_test_values = case type
69
+ when :decimal then [ "test", true ]
70
+ when :time then [ 1234, true ]
71
+ when :date then [ "test", 1234, true, "1234.12" ]
72
+ when :datetime then [ "test", 1234, true ]
73
+ else []
74
+ end
75
+
76
+
77
+ (test_values - exclude_test_values).each do |value|
78
+
79
+ instance.test_attribute = value
80
+
81
+ if type == :boolean
82
+ [true, false].include?(instance.test_attribute).must_equal true, "Expected #{instance.test_attribute.inspect} to be boolean, not #{type.class}"
83
+ else
84
+ instance.test_attribute.must_be_kind_of type_name.to_s.classify.constantize
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+ end
91
+ end
92
+
8
93
  end
@@ -1,5 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
+
3
4
  require "tableless_model/version"
4
5
 
5
6
  Gem::Specification.new do |s|
@@ -12,11 +13,10 @@ Gem::Specification.new do |s|
12
13
  s.summary = %q{A serialisable and validatable table-less model with support for associations, useful to store settings, options, etc in a serialized form in a parent object}
13
14
  s.description = %q{A serialisable and validatable table-less model with support for associations, useful to store settings, options, etc in a serialized form in a parent object}
14
15
 
15
- s.add_dependency "hashie"
16
16
  s.add_dependency "validatable"
17
17
 
18
- s.add_development_dependency "rspec"
19
- s.add_development_dependency "mocha"
18
+ s.add_development_dependency "activerecord"
19
+ s.add_development_dependency "minitest"
20
20
 
21
21
  s.rubyforge_project = "tableless_model"
22
22
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tableless_model
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Vito Botta
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-02 00:00:00 +00:00
18
+ date: 2011-01-03 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: hashie
22
+ name: validatable
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
@@ -33,7 +33,7 @@ dependencies:
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
- name: validatable
36
+ name: activerecord
37
37
  prerelease: false
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
@@ -44,10 +44,10 @@ dependencies:
44
44
  segments:
45
45
  - 0
46
46
  version: "0"
47
- type: :runtime
47
+ type: :development
48
48
  version_requirements: *id002
49
49
  - !ruby/object:Gem::Dependency
50
- name: rspec
50
+ name: minitest
51
51
  prerelease: false
52
52
  requirement: &id003 !ruby/object:Gem::Requirement
53
53
  none: false
@@ -60,20 +60,6 @@ dependencies:
60
60
  version: "0"
61
61
  type: :development
62
62
  version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- name: mocha
65
- prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
67
- none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
75
- type: :development
76
- version_requirements: *id004
77
63
  description: A serialisable and validatable table-less model with support for associations, useful to store settings, options, etc in a serialized form in a parent object
78
64
  email:
79
65
  - vito@botta.name