optional 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -60
- data/optional.gemspec +2 -2
- metadata +2 -2
data/README.md
CHANGED
@@ -1,60 +1,2 @@
|
|
1
|
-
#
|
2
|
-
###
|
3
|
-
|
4
|
-
JSON is a great way to transfer data between systems, and it's easy to parse into a Ruby hash. But sometimes it's nice to have actual methods to call when you want to get attributes from your data, rather than coupling your entire codebase to the hash representation by littering it with calls to `fetch` or `[]`. The same goes for BSON documents stored in Mongo.
|
5
|
-
|
6
|
-
That's where `id` (as in Freud) comes in. You define your model classes using syntax that should look pretty familiar if you've used any popular Ruby ORMs - but `id` is not an ORM. Model objects defined with `id` have a constructor that accepts a hash, and you define the values of this hash that are made readable as fields - but that hash can come from any source.
|
7
|
-
|
8
|
-
#### Defining a model
|
9
|
-
|
10
|
-
Defining a model looks like this:
|
11
|
-
|
12
|
-
class MyModel
|
13
|
-
include Id::Model
|
14
|
-
|
15
|
-
field :foo
|
16
|
-
field :bar, default: 42
|
17
|
-
field :baz, key: 'barry'
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
my_model = MyModel.new(foo: 7, barry: 'hello')
|
22
|
-
my_model.foo # => 7
|
23
|
-
my_model.bar # => 42
|
24
|
-
my_model.baz # => 'hello'
|
25
|
-
|
26
|
-
As you can see, you can specify default values as well as key aliases.
|
27
|
-
|
28
|
-
#### Associations
|
29
|
-
|
30
|
-
You can also specify has_one or has_many "associations" - what would be nested subdocuments in MongoDB for example - like this:
|
31
|
-
|
32
|
-
class Zoo
|
33
|
-
include Id::Model
|
34
|
-
|
35
|
-
has_many :lions
|
36
|
-
has_many :zebras
|
37
|
-
has_one :zookeeper, type: Person
|
38
|
-
end
|
39
|
-
|
40
|
-
zoo = Zoo.new(lions: [{name: 'Hetty'}],
|
41
|
-
zebras: [{name: 'Lisa'}],
|
42
|
-
zookeeper: {name: 'Russell' d})
|
43
|
-
|
44
|
-
zoo.lions.first.class # => Lion
|
45
|
-
zoo.lions.first.name # => "Hetty"
|
46
|
-
zoo.zookeeper.class # => Person
|
47
|
-
zoo.zookeeper.name # => "Russell"
|
48
|
-
|
49
|
-
Types are inferred from the association name unless one is specified.
|
50
|
-
|
51
|
-
#### Designed for immutability
|
52
|
-
|
53
|
-
`id` models provide accessor methods, but no mutator methods, because they are designed for immutability. How do immutable models work? When you need to change some field of a model object, a new copy of the object is created with the field changed as required. This is handled for you by `id`'s `set` method:
|
54
|
-
|
55
|
-
person = Person.new(name: 'Russell', job: 'programmer')
|
56
|
-
person.set(name: 'Radek') # => returns a new Person whose name is Radek and whose job is 'programmer'
|
57
|
-
|
58
|
-
You can even set fields on nested models in this way:
|
59
|
-
|
60
|
-
person.hat.set(color: 'red') # => returns a new person object with a new hat object with its color set to red
|
1
|
+
# optional
|
2
|
+
### option types to make nils say bye bye
|
data/optional.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'optional'
|
3
|
-
s.version = '0.0.
|
3
|
+
s.version = '0.0.2'
|
4
4
|
s.date = '2013-04-19'
|
5
5
|
s.summary = "Optional values with pattern matching"
|
6
6
|
s.description = "Make nils go bye bye with Options!"
|
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.files = `git ls-files`.split($\)
|
10
10
|
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
11
11
|
s.require_paths = ["lib"]
|
12
|
-
s.homepage = 'http://github.com/
|
12
|
+
s.homepage = 'http://github.com/rsslldnphy/optional'
|
13
13
|
|
14
14
|
s.add_development_dependency "rspec"
|
15
15
|
s.add_development_dependency "simplecov"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: optional
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -73,7 +73,7 @@ files:
|
|
73
73
|
- spec/lib/optional/some_spec.rb
|
74
74
|
- spec/spec_helper.rb
|
75
75
|
- spec/support/cat.rb
|
76
|
-
homepage: http://github.com/
|
76
|
+
homepage: http://github.com/rsslldnphy/optional
|
77
77
|
licenses: []
|
78
78
|
post_install_message:
|
79
79
|
rdoc_options: []
|