structure 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  Structure
2
2
  =========
3
3
 
4
- Structure is a better Struct and does wonders when modeling ephemeral
5
- data fed in from an API.
4
+ Structure is a better Struct.
5
+
6
+ It does wonders when modeling ephemeral data fed in from an API.
6
7
 
7
8
  #_ d
8
9
  ##_ d#
@@ -42,9 +43,12 @@ Define a model:
42
43
  key :friends, :type => Array, :default => []
43
44
  end
44
45
 
45
- Typecast values:
46
+ Conjure an object:
46
47
 
47
48
  p1 = Person.new :name => 'John'
49
+
50
+ Typecast values:
51
+
48
52
  p1.age = '28'
49
53
  p1.age
50
54
  => 28
@@ -68,10 +72,35 @@ models set up:
68
72
  person.friends.first
69
73
  => #<Person:0x0000010107d030 @attributes={:name=>"Jane", :age=>nil, :friends=>[#<Person:0x0000010107d030 ...>]}, @modifiable=true>
70
74
 
75
+ Throw in some Active Model modules...
76
+
77
+ require 'active_model'
78
+
79
+ class Book < Structure
80
+ include ActiveModel::Validations
81
+
82
+ key :title
83
+ key :authors, :type => Array, :default => []
84
+
85
+ validates_presence_of :title
86
+ end
87
+
88
+ ... and make your model quack like ActiveRecord:
89
+
90
+ book = Book.new
91
+ book.valid?
92
+ => false
93
+ book.errors
94
+ => {:title=>["can't be blank"]}
95
+ book.title = "Society of the Spectacle"
96
+ book.valid?
97
+ => true
98
+
71
99
  Types
72
100
  -----
73
101
 
74
102
  Structure supports the following types:
103
+
75
104
  * Array
76
105
  * Boolean
77
106
  * Float
@@ -1,3 +1,3 @@
1
1
  class Sucker
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
data/lib/structure.rb CHANGED
@@ -50,7 +50,9 @@ class Structure
50
50
  lambda do |value|
51
51
  case value
52
52
  when String
53
- !(value =~ /false/i)
53
+ value =~ /1|true/i
54
+ when Integer
55
+ value != 0
54
56
  else
55
57
  !!value
56
58
  end
@@ -7,18 +7,6 @@ describe Structure do
7
7
  person.should respond_to :map
8
8
  end
9
9
 
10
- context "when frozen" do
11
- before do
12
- person.freeze
13
- end
14
-
15
- it "raises an error" do
16
- expect do
17
- person.name = 'Joe'
18
- end.to raise_error TypeError
19
- end
20
- end
21
-
22
10
  describe ".key" do
23
11
  it "defines accessors" do
24
12
  %w{name name=}.each { |method| person.should respond_to method }
@@ -112,6 +100,42 @@ describe Structure do
112
100
  end.not_to raise_error
113
101
  end
114
102
  end
103
+
104
+ context "when typecasting a set value" do
105
+ before(:all) do
106
+ Person.key :vegetarian, :type => Boolean
107
+ end
108
+
109
+ it "typecasts 'true' to true" do
110
+ person.vegetarian = 'true'
111
+ person.vegetarian.should be_true
112
+ end
113
+
114
+ it "typecasts 'TRUE' to true" do
115
+ person.vegetarian = 'TRUE'
116
+ person.vegetarian.should be_true
117
+ end
118
+
119
+ it "typecasts '1' to true" do
120
+ person.vegetarian = '1'
121
+ person.vegetarian.should be_true
122
+ end
123
+
124
+ it "typecasts all other strings to false" do
125
+ person.vegetarian = 'foo'
126
+ person.vegetarian.should be_false
127
+ end
128
+
129
+ it "typecasts 0 to false" do
130
+ person.vegetarian = 0
131
+ person.vegetarian.should be_false
132
+ end
133
+
134
+ it "typecasts all other integers to true" do
135
+ person.vegetarian = 1
136
+ person.vegetarian.should be_true
137
+ end
138
+ end
115
139
  end
116
140
 
117
141
  context "when type is Hash" do
@@ -141,6 +165,18 @@ describe Structure do
141
165
  end
142
166
  end
143
167
 
168
+ context "when frozen" do
169
+ before do
170
+ person.freeze
171
+ end
172
+
173
+ it "raises an error" do
174
+ expect do
175
+ person.name = 'Joe'
176
+ end.to raise_error TypeError
177
+ end
178
+ end
179
+
144
180
  context "when setting the value of an attribute to nil" do
145
181
  it "does not typecast the value" do
146
182
  person.age = nil
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 1
9
- version: 0.3.1
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Paper Cavalier
@@ -63,6 +63,7 @@ extra_rdoc_files: []
63
63
  files:
64
64
  - .gitignore
65
65
  - Gemfile
66
+ - LICENSE
66
67
  - README.md
67
68
  - Rakefile
68
69
  - lib/structure.rb