structure 0.27.4 → 0.27.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/structure.rb +9 -1
- metadata +6 -36
- data/LICENSE +0 -22
- data/README.md +0 -46
- data/test.rb +0 -68
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bfa61be65533087a6a80a31bfac5e2ad42e4265
|
4
|
+
data.tar.gz: 95de6560a9bfc44d96996316f4882013f81cc063
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0620ac53aa5f5fc3b0251c9fe146df6e8c18506e85adc53a0f4c86db4342f533bfbae716c007f541bb12de955e6b7f3bdac4de2bf4ddd17552995defd2b038bc
|
7
|
+
data.tar.gz: e35884dfea3921545a678245fdad78513f0bce1be1727adedbf85da8d5d30b843b4ae12eaa7b8ac3fe653313c68425a3fd012684ac342bffd82a0fecac4c3016
|
data/structure.rb
CHANGED
@@ -51,7 +51,15 @@ module Structure
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def attribute(name, &blk)
|
54
|
-
define_method(name,
|
54
|
+
define_method("_#{name}", blk)
|
55
|
+
private "_#{name}"
|
56
|
+
|
57
|
+
module_eval <<-END
|
58
|
+
def #{name}
|
59
|
+
@#{name} ||= _#{name}
|
60
|
+
end
|
61
|
+
END
|
62
|
+
|
55
63
|
@attribute_names << name
|
56
64
|
end
|
57
65
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: structure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.27.
|
4
|
+
version: 0.27.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hakan Ensari
|
@@ -9,47 +9,17 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2014-07-10 00:00:00.000000000 Z
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: minitest
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
12
|
+
dependencies: []
|
41
13
|
description:
|
42
|
-
email:
|
14
|
+
email: me@hakanensari.com
|
43
15
|
executables: []
|
44
16
|
extensions: []
|
45
17
|
extra_rdoc_files: []
|
46
18
|
files:
|
47
|
-
- LICENSE
|
48
|
-
- README.md
|
49
19
|
- structure.rb
|
50
|
-
- test.rb
|
51
20
|
homepage:
|
52
|
-
licenses:
|
21
|
+
licenses:
|
22
|
+
- MIT
|
53
23
|
metadata: {}
|
54
24
|
post_install_message:
|
55
25
|
rdoc_options: []
|
@@ -59,7 +29,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
29
|
requirements:
|
60
30
|
- - ">="
|
61
31
|
- !ruby/object:Gem::Version
|
62
|
-
version: '
|
32
|
+
version: '0'
|
63
33
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
34
|
requirements:
|
65
35
|
- - ">="
|
data/LICENSE
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
(The MIT License)
|
2
|
-
|
3
|
-
Copyright (c) 2013-14 Hakan Ensari
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
# Structure
|
2
|
-
|
3
|
-
Structure is minimal glue that helps you parse data—for instance, API responses—into immutable value objects.
|
4
|
-
|
5
|
-
## Usage
|
6
|
-
|
7
|
-
Usage is straightforward. Mix in the module and define the parser methods with `.attribute`, a naming convention I decided to stick to.
|
8
|
-
|
9
|
-
```ruby
|
10
|
-
class Location
|
11
|
-
include Structure
|
12
|
-
|
13
|
-
def initialize(data)
|
14
|
-
@data = data
|
15
|
-
end
|
16
|
-
|
17
|
-
attribute :latitude do
|
18
|
-
parse(:latitude)
|
19
|
-
end
|
20
|
-
|
21
|
-
attribute :longitude do
|
22
|
-
parse(:longitude)
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def parse(key)
|
28
|
-
# Heavy-duty parsing action on @data
|
29
|
-
end
|
30
|
-
end
|
31
|
-
```
|
32
|
-
|
33
|
-
Once you have your parser defined, initialise it with some data and take it to a drive.
|
34
|
-
|
35
|
-
```ruby
|
36
|
-
location = Location.new(data)
|
37
|
-
puts location.latitude # => Some latitude
|
38
|
-
puts location.to_h # => All attributes as a Ruby Hash
|
39
|
-
puts location # => Bonus: This will pretty-inspect the instance
|
40
|
-
```
|
41
|
-
|
42
|
-
When testing objects the parser collaborates in, you may benefit from a double to stand in for the real parser.
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
double = Location.to_struct.new(location: 10, latitude: 10)
|
46
|
-
```
|
data/test.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'minitest/pride'
|
3
|
-
require './structure'
|
4
|
-
|
5
|
-
Location = Struct.new(:res) do
|
6
|
-
include Structure
|
7
|
-
[:latitude, :longitude].each { |key| attribute(key) { res.fetch(key) } }
|
8
|
-
end
|
9
|
-
|
10
|
-
class StructureTest < MiniTest::Test
|
11
|
-
def setup
|
12
|
-
@location = Location.new(latitude: 10, longitude: 20)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_class_returns_attribute_names
|
16
|
-
assert_equal [:latitude, :longitude], Location.attribute_names
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_casts_itself_to_struct
|
20
|
-
struct = Location.to_struct
|
21
|
-
assert_equal 'Struct::Location', struct.name
|
22
|
-
assert_equal 1, struct.new(latitude: 1).latitude
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_cast_to_struct_only_once
|
26
|
-
out, err = capture_io do
|
27
|
-
2.times { Location.to_struct }
|
28
|
-
end
|
29
|
-
assert_empty err
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_subclassing_does_not_have_side_effects
|
33
|
-
subclass = Class.new(Location) do
|
34
|
-
attribute(:name) { 'foo' }
|
35
|
-
end
|
36
|
-
obj = subclass.new(latitude: 10, longitude: 20)
|
37
|
-
assert_equal({ latitude: 10, longitude: 20, name: 'foo' }, obj.attributes)
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_attributes
|
41
|
-
assert_equal 10, @location.latitude
|
42
|
-
assert_equal 20, @location.longitude
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_returns_attributes
|
46
|
-
assert_equal({ latitude: 10, longitude: 20 }, @location.attributes)
|
47
|
-
assert_equal @location.to_h, @location.attributes
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_compares
|
51
|
-
@other = Location.new(longitude: 20, latitude: 10)
|
52
|
-
assert @location == @other
|
53
|
-
assert @location.eql?(@other)
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_pretty_inspects
|
57
|
-
assert_equal '#<Location latitude=10, longitude=20>', @location.inspect
|
58
|
-
assert_equal @location.to_s, @location.inspect
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_truncates_long_arrays_when_pretty_inspecting
|
62
|
-
klass = Class.new { include Structure }
|
63
|
-
klass.attribute(:ary) { ['a'] }
|
64
|
-
assert_includes klass.new.inspect, 'ary=["a"]'
|
65
|
-
klass.attribute(:ary) { ('a'..'z').to_a }
|
66
|
-
assert_includes klass.new.inspect, 'ary=["a", "b", "c"...]'
|
67
|
-
end
|
68
|
-
end
|