blobject 0.1.9 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +145 -0
- data/lib/blobject.rb +48 -13
- data/lib/blobject/version.rb +1 -1
- metadata +6 -7
- data/README.markdown +0 -127
- data/test.rb +0 -6
data/README.md
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
![](https://github.com/sjltaylor/blobject/raw/master/blobject.png)
|
2
|
+
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
|
6
|
+
This is how you could use blobject to present a model for a JSON api call
|
7
|
+
|
8
|
+
def present(model)
|
9
|
+
blobject do
|
10
|
+
name.first model.first_name
|
11
|
+
name.last model.last_name
|
12
|
+
url 'http://www.youtube.com/watch?v=dQw4w9WgXcQ'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
If the were more calls in the 'name' namespace it might be more convenient to do
|
17
|
+
|
18
|
+
def present(model)
|
19
|
+
blobject do
|
20
|
+
name do
|
21
|
+
first model.first_name
|
22
|
+
middle model.middle_name if model.has_middle_name?
|
23
|
+
last model.last_name
|
24
|
+
end
|
25
|
+
url 'http://www.youtube.com/watch?v=dQw4w9WgXcQ'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
In each case, a blobject is returned that can be used like a normal object, or a hash
|
30
|
+
|
31
|
+
b = present(model)
|
32
|
+
b.name.first
|
33
|
+
=> 'Barry'
|
34
|
+
b.name.has_middle?
|
35
|
+
=> true
|
36
|
+
b[:url] # could also pass a string, blobject isn't a key fascist
|
37
|
+
=> "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
38
|
+
|
39
|
+
blobjects can be modified as follows
|
40
|
+
|
41
|
+
b.modify do
|
42
|
+
number 172367
|
43
|
+
end
|
44
|
+
|
45
|
+
They can be as complex/deep as required
|
46
|
+
|
47
|
+
b = blobject do
|
48
|
+
deep.nested.member.ofa.very.complex.thing 'HELLO WORLD'
|
49
|
+
end
|
50
|
+
|
51
|
+
b.deep.nested.member.ofa.very.complex.thing
|
52
|
+
=> 'HELLO WORLD'
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
## Creation options
|
58
|
+
|
59
|
+
You can call blobject or Blobject.new with a hash to prefill it with data
|
60
|
+
|
61
|
+
b = blobject :name => {:first => 'Barry'}
|
62
|
+
b.name.first
|
63
|
+
=> 'Barry'
|
64
|
+
|
65
|
+
b.modify { name.last 'McDoogle' }
|
66
|
+
b.name.last
|
67
|
+
=> 'McDoogle'
|
68
|
+
|
69
|
+
`blobject *params` is simply an alias for `Blobject.new *params`
|
70
|
+
|
71
|
+
## Checking for members
|
72
|
+
|
73
|
+
to find out if a blobject already has a member:
|
74
|
+
|
75
|
+
b = blobject
|
76
|
+
|
77
|
+
b.has_name?
|
78
|
+
=> false
|
79
|
+
|
80
|
+
b.modify do
|
81
|
+
has_name?
|
82
|
+
=> false
|
83
|
+
name 'Jim'
|
84
|
+
has_name?
|
85
|
+
=> true
|
86
|
+
end
|
87
|
+
|
88
|
+
b.has_name?
|
89
|
+
=> true
|
90
|
+
|
91
|
+
## Usage Gists
|
92
|
+
|
93
|
+
* [in the controller](https://gist.github.com/1254399)
|
94
|
+
|
95
|
+
* [for configuration files](https://gist.github.com/1254403)
|
96
|
+
|
97
|
+
|
98
|
+
## Behaves like a hash
|
99
|
+
|
100
|
+
* `#keys` returns all of the available keys
|
101
|
+
* `#values` returns all of the available keys
|
102
|
+
* `#each` enumerator of key, value pairs
|
103
|
+
* `#[]` can be used for access to members, similarly `#[]=` can be used for assignment inside or outside of the modify block
|
104
|
+
* `#dup` performs `Marshal.load(Marshal.dump(self))` for a deep copy
|
105
|
+
* `#empty?` returns true if there are no members, false otherwise
|
106
|
+
* `#to_yaml` and `#to_json`
|
107
|
+
|
108
|
+
## Load JSON and YAML from a file
|
109
|
+
|
110
|
+
`blobject_file = Blobject.read('path/to/file.json')`
|
111
|
+
|
112
|
+
|
113
|
+
Supported extensions: `yml`, `yaml`, `json`, `js`
|
114
|
+
|
115
|
+
|
116
|
+
## Limitations
|
117
|
+
|
118
|
+
Blobjects are intended for de/serialization; cyclic object graphs will cause havoc
|
119
|
+
|
120
|
+
|
121
|
+
## Contributing to blobject
|
122
|
+
|
123
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
124
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
125
|
+
* Fork the project
|
126
|
+
* Start a feature/bugfix branch
|
127
|
+
* Commit and push until you are happy with your contribution
|
128
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
129
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
130
|
+
|
131
|
+
## License
|
132
|
+
|
133
|
+
(The MIT License)
|
134
|
+
|
135
|
+
Copyright © 2011 [Sam Taylor](http://sjltaylor.com/)
|
136
|
+
|
137
|
+
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:
|
138
|
+
|
139
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
140
|
+
|
141
|
+
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.
|
142
|
+
|
143
|
+
Copyright (c) 2011 Sam Taylor. See LICENSE.txt for
|
144
|
+
further details.
|
145
|
+
|
data/lib/blobject.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'blobject/version'
|
2
|
+
require 'json'
|
2
3
|
|
3
4
|
def blobject *parameters, &block
|
4
5
|
Blobject.new *parameters, &block
|
@@ -34,13 +35,10 @@ class Blobject
|
|
34
35
|
|
35
36
|
def method_missing sym, *params, &block
|
36
37
|
|
37
|
-
if match = /^has_(
|
38
|
-
return @hash.has_key? match[
|
38
|
+
if match = /^has_(.+)\?/.match(sym)
|
39
|
+
return @hash.has_key? match[1].to_sym
|
39
40
|
end
|
40
41
|
|
41
|
-
modify_getter = -> { params.length == 0 && @modifying }
|
42
|
-
modify_assign = -> { params.length == 1 && @modifying }
|
43
|
-
|
44
42
|
if @modifying
|
45
43
|
|
46
44
|
case params.length
|
@@ -95,15 +93,43 @@ class Blobject
|
|
95
93
|
end
|
96
94
|
|
97
95
|
def [] key
|
98
|
-
@hash[key]
|
96
|
+
@hash[key.to_s.to_sym]
|
97
|
+
end
|
98
|
+
|
99
|
+
def keys
|
100
|
+
@hash.keys
|
101
|
+
end
|
102
|
+
|
103
|
+
def values
|
104
|
+
@hash.values
|
105
|
+
end
|
106
|
+
|
107
|
+
def each &block
|
108
|
+
return @hash.each &block
|
99
109
|
end
|
100
110
|
|
101
111
|
def []= key, value
|
102
112
|
send key, value
|
103
113
|
end
|
104
114
|
|
115
|
+
def dup
|
116
|
+
Marshal.load(Marshal.dump(self))
|
117
|
+
end
|
118
|
+
|
105
119
|
def to_hash
|
106
|
-
|
120
|
+
hash = @hash
|
121
|
+
|
122
|
+
hash.each do |key, value|
|
123
|
+
hash[key] = value.to_hash if (value.instance_of? Blobject)
|
124
|
+
|
125
|
+
if value.instance_of? Array
|
126
|
+
hash[key] = value.map do |v|
|
127
|
+
v.instance_of?(Blobject) ? v.to_hash : v
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
hash
|
107
133
|
end
|
108
134
|
|
109
135
|
def from_hash hash
|
@@ -111,7 +137,7 @@ class Blobject
|
|
111
137
|
end
|
112
138
|
|
113
139
|
def to_yaml *params
|
114
|
-
|
140
|
+
to_hash.to_yaml *params
|
115
141
|
end
|
116
142
|
|
117
143
|
def self.from_yaml yaml
|
@@ -126,6 +152,17 @@ class Blobject
|
|
126
152
|
__blobjectify__ JSON.load(json)
|
127
153
|
end
|
128
154
|
|
155
|
+
def self.read path
|
156
|
+
case File.extname(path).downcase
|
157
|
+
when /\.y(a)?ml$/
|
158
|
+
from_yaml File.read(path)
|
159
|
+
when /\.js(on)?$/
|
160
|
+
from_json File.read(path)
|
161
|
+
else
|
162
|
+
raise "Cannot handle file format of #{path}"
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
129
166
|
def dup
|
130
167
|
Blobject.new to_hash
|
131
168
|
end
|
@@ -134,8 +171,6 @@ class Blobject
|
|
134
171
|
@hash.inspect
|
135
172
|
end
|
136
173
|
|
137
|
-
|
138
|
-
|
139
174
|
protected
|
140
175
|
|
141
176
|
def self.__blobjectify__ obj
|
@@ -158,11 +193,11 @@ protected
|
|
158
193
|
obj
|
159
194
|
end
|
160
195
|
|
161
|
-
def __r_modify_set__
|
162
|
-
@modifying =
|
196
|
+
def __r_modify_set__ v
|
197
|
+
@modifying = v
|
163
198
|
@hash.values.each do |child|
|
164
199
|
if child.class <= Blobject
|
165
|
-
child.__r_modify_set__
|
200
|
+
child.__r_modify_set__ v
|
166
201
|
end
|
167
202
|
end
|
168
203
|
end
|
data/lib/blobject/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blobject
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-09-30 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152696260 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152696260
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: ruby-debug19
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152693880 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152693880
|
36
36
|
description: Blobject provides a free flowing syntax for creating blobs of data.
|
37
37
|
email:
|
38
38
|
- sjltaylor@gmail.com
|
@@ -44,7 +44,7 @@ files:
|
|
44
44
|
- .pryrc
|
45
45
|
- .rspec
|
46
46
|
- Gemfile
|
47
|
-
- README.
|
47
|
+
- README.md
|
48
48
|
- Rakefile
|
49
49
|
- TODO.txt
|
50
50
|
- blobject.gemspec
|
@@ -54,7 +54,6 @@ files:
|
|
54
54
|
- lib/blobject/version.rb
|
55
55
|
- spec/blobject_spec.rb
|
56
56
|
- spec/spec_helper.rb
|
57
|
-
- test.rb
|
58
57
|
homepage: https://github.com/sjltaylor/blobject
|
59
58
|
licenses: []
|
60
59
|
post_install_message:
|
data/README.markdown
DELETED
@@ -1,127 +0,0 @@
|
|
1
|
-
![](https://github.com/sjltaylor/blobject/raw/master/blobject.png)
|
2
|
-
|
3
|
-
Provides a fluent to create blobs of data for views, JSON etc.
|
4
|
-
|
5
|
-
## Examples
|
6
|
-
|
7
|
-
### Creating a Blobject
|
8
|
-
|
9
|
-
#### Basic usage
|
10
|
-
b = Blobject.new
|
11
|
-
|
12
|
-
b.name.first = 'Rick'
|
13
|
-
b.name.last = 'Astley'
|
14
|
-
|
15
|
-
b.url = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ'
|
16
|
-
|
17
|
-
#### With a block
|
18
|
-
b = Blobject.new do |b|
|
19
|
-
|
20
|
-
b.name.first = 'Rick'
|
21
|
-
b.name.last = 'Astley'
|
22
|
-
|
23
|
-
b.url = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ'
|
24
|
-
end
|
25
|
-
|
26
|
-
#### With a hash
|
27
|
-
b = Blobject.new :name => {:first => 'Rick', :last => 'Astley'}, :url => 'http://www.youtube.com/watch?v=dQw4w9WgXcQ'
|
28
|
-
|
29
|
-
#### With selected members of an existing object
|
30
|
-
b = Blobject.new existing_object, [:name, :url]
|
31
|
-
|
32
|
-
#### With the global shortcut method
|
33
|
-
b = blobject do |b|
|
34
|
-
b.name.first = 'Rick'
|
35
|
-
b.name.last = 'Astley'
|
36
|
-
|
37
|
-
b.url = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ'
|
38
|
-
end
|
39
|
-
|
40
|
-
all of the above initialisation methods can be used in this way too.
|
41
|
-
|
42
|
-
### Object graphs can be as complex as necessary
|
43
|
-
|
44
|
-
b = blobject
|
45
|
-
b.very.deep.nesting.of.objects = 2
|
46
|
-
|
47
|
-
The intermediary blobjects are created automagically
|
48
|
-
|
49
|
-
### Check for the existence of a member
|
50
|
-
b = blobject
|
51
|
-
b.name = 'Rick'
|
52
|
-
b.url?
|
53
|
-
=> false
|
54
|
-
b.name?
|
55
|
-
=> true
|
56
|
-
|
57
|
-
### Overrides ruby freeze and unfreeze
|
58
|
-
The freeze method prevents the blobject from being extended or changed.
|
59
|
-
|
60
|
-
b = blobject do |b|
|
61
|
-
b.name.first = 'Rick'
|
62
|
-
b.name.last = 'Astley'
|
63
|
-
|
64
|
-
b.url = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ'
|
65
|
-
end.freeze
|
66
|
-
b.frozen?
|
67
|
-
=> true
|
68
|
-
b.am_i_here
|
69
|
-
=> nil
|
70
|
-
b.name?
|
71
|
-
=> true
|
72
|
-
b.name = 'hello'
|
73
|
-
=> Exception!
|
74
|
-
|
75
|
-
### empty? and blank?
|
76
|
-
|
77
|
-
b = blobject
|
78
|
-
b.empty? && b.blank?
|
79
|
-
=> true
|
80
|
-
b.number = 123
|
81
|
-
b.empty? || b.blank?
|
82
|
-
=> false
|
83
|
-
|
84
|
-
## Feature summary
|
85
|
-
|
86
|
-
* call a chain of methods and build an object graph automagically
|
87
|
-
* check the existence of a member with 'method?'
|
88
|
-
* overrides ruby freeze
|
89
|
-
* recursively parses blobjects from json strings
|
90
|
-
* can be initialized with...
|
91
|
-
** an object and list of accessors to copy
|
92
|
-
** a hash
|
93
|
-
** a block
|
94
|
-
|
95
|
-
## Intended usage and limitations
|
96
|
-
|
97
|
-
Intended for use to create view models which can be encoded to JSON with ActiveSupport::JSON.encode
|
98
|
-
JSON can be unmarshalled into a blobject (recursively) with `Blobject.from_json json_string`
|
99
|
-
|
100
|
-
Blobjects are intended for de/serialization, therefore cycles in the object graph will lead to a non terminating thread.
|
101
|
-
|
102
|
-
|
103
|
-
## Contributing to blobject
|
104
|
-
|
105
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
106
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
107
|
-
* Fork the project
|
108
|
-
* Start a feature/bugfix branch
|
109
|
-
* Commit and push until you are happy with your contribution
|
110
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
111
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
112
|
-
|
113
|
-
## License
|
114
|
-
|
115
|
-
(The MIT License)
|
116
|
-
|
117
|
-
Copyright © 2011 [Sam Taylor](http://sjltaylor.com/)
|
118
|
-
|
119
|
-
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:
|
120
|
-
|
121
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
122
|
-
|
123
|
-
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.
|
124
|
-
|
125
|
-
Copyright (c) 2011 Sam Taylor. See LICENSE.txt for
|
126
|
-
further details.
|
127
|
-
|