morph 0.4.1 → 0.5.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.
- checksums.yaml +4 -4
- data/CHANGELOG +2 -0
- data/README.md +72 -40
- data/lib/morph.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da524dc60329ec656ad28aa17e38231481fefa80
|
4
|
+
data.tar.gz: 62c9c6fb10ecff9c410d6fd146c2de2eaa0ccc4b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea4e19f7beeffd219d1f11b5c0979e214fb55997eff9180994e3a633e7b92d87ca81005554b93352020ae86a28e8c9b576ab158e42696206f8f8cc907404694a
|
7
|
+
data.tar.gz: 1d2d29875aa3c306007dd48a807dacb3f8253bca6e84f12e74911e4ac15138fce6614dd4a5b510f543ca76f0e5503486ec567c9fa2bda5c050330db8b3a25e7d
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -4,11 +4,15 @@ Morph allows you to emerge Ruby class definitions from data or by calling assign
|
|
4
4
|
|
5
5
|
## Installing Morph
|
6
6
|
|
7
|
+
```rb
|
7
8
|
gem install morph
|
9
|
+
```
|
8
10
|
|
9
11
|
To use Morph:
|
10
12
|
|
13
|
+
```rb
|
11
14
|
require 'morph'
|
15
|
+
```
|
12
16
|
|
13
17
|
Tested to work with Ruby 1.8 - 2.3, JRuby 9, and Rubinius 3.
|
14
18
|
|
@@ -16,6 +20,7 @@ Tested to work with Ruby 1.8 - 2.3, JRuby 9, and Rubinius 3.
|
|
16
20
|
|
17
21
|
Here's an example showing Morph creating classes and objects from JSON:
|
18
22
|
|
23
|
+
```rb
|
19
24
|
json = '{
|
20
25
|
"id": "3599110793",
|
21
26
|
"type": "PushEvent",
|
@@ -39,40 +44,34 @@ Here's an example showing Morph creating classes and objects from JSON:
|
|
39
44
|
event = Morph.from_json json, type, namespace
|
40
45
|
|
41
46
|
# => <Github::PushEvent @id="3599110793", @type="PushEvent",
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
+
# @actor=#<Github::Actor:0x007faa0c86b790 @id=3447, @login="robmckinnon",
|
48
|
+
# @url="https://api.github.com/users/robmckinnon">,
|
49
|
+
# @repo=#<Github::Repo:0x007faa0c869198 @id=5092, @name="robmckinnon/morph",
|
50
|
+
# @url="https://api.github.com/repos/robmckinnon/morph">
|
51
|
+
# >
|
47
52
|
|
48
|
-
event.class
|
53
|
+
event.class # => Github::PushEvent
|
49
54
|
|
50
|
-
# =>
|
55
|
+
event.class.morph_attributes # => [:id, :type, :actor, :repo]
|
51
56
|
|
52
|
-
event.class
|
57
|
+
event.actor.class # => Github::Actor
|
53
58
|
|
54
|
-
# =>
|
55
|
-
|
56
|
-
event.actor.class
|
57
|
-
|
58
|
-
# => Github::Actor
|
59
|
-
|
60
|
-
event.repo.class
|
61
|
-
|
62
|
-
# => Github::Repo
|
59
|
+
event.repo.class # => Github::Repo
|
60
|
+
```
|
63
61
|
|
64
62
|
If namespace module not provided, new classes are created in Morph module.
|
65
63
|
|
64
|
+
```rb
|
66
65
|
event = Morph.from_json json, type, namespace
|
67
66
|
|
68
|
-
event.class
|
69
|
-
|
70
|
-
# => Morph::PushEvent
|
67
|
+
event.class # => Morph::PushEvent
|
68
|
+
```
|
71
69
|
|
72
70
|
## Morph creating classes `from_csv`
|
73
71
|
|
74
72
|
Here's an example showing Morph playing with CSV (comma-separated values):
|
75
73
|
|
74
|
+
```rb
|
76
75
|
csv = %Q[name,party\nTed Roe,red\nAli Davidson,blue\nSue Smith,green]
|
77
76
|
|
78
77
|
people = Morph.from_csv(csv, 'person')
|
@@ -81,14 +80,14 @@ Here's an example showing Morph playing with CSV (comma-separated values):
|
|
81
80
|
#<Morph::Person @name="Ali Davidson", @party="blue">,
|
82
81
|
#<Morph::Person @name="Sue Smith", @party="green">]
|
83
82
|
|
84
|
-
people.last.party
|
85
|
-
|
86
|
-
# => "green"
|
83
|
+
people.last.party # => "green"
|
84
|
+
```
|
87
85
|
|
88
86
|
## Morph creating classes `from_tsv`
|
89
87
|
|
90
88
|
Here's example code showing Morph playing with TSV (tab-separated values):
|
91
89
|
|
90
|
+
```rb
|
92
91
|
tsv = %Q[name\tparty\nTed Roe\tred\nAli Davidson\tblue\nSue Smith\tgreen]
|
93
92
|
|
94
93
|
people = Morph.from_tsv(tsv, 'person')
|
@@ -97,14 +96,14 @@ Here's example code showing Morph playing with TSV (tab-separated values):
|
|
97
96
|
#<Morph::Person @name="Ali Davidson", @party="blue">,
|
98
97
|
#<Morph::Person @name="Sue Smith", @party="green">]
|
99
98
|
|
100
|
-
people.last.party
|
101
|
-
|
102
|
-
# => "green"
|
99
|
+
people.last.party # => "green"
|
100
|
+
```
|
103
101
|
|
104
102
|
## Morph creating classes `from_xml`
|
105
103
|
|
106
104
|
Here's example code showing Morph playing with XML:
|
107
105
|
|
106
|
+
```rb
|
108
107
|
xml = %Q[<?xml version="1.0" encoding="UTF-8"?>
|
109
108
|
<councils type="array">
|
110
109
|
<council code='1'>
|
@@ -120,9 +119,8 @@ Here's example code showing Morph playing with XML:
|
|
120
119
|
# => [#<Morph::Council @code="1", @name="Aberdeen City Council">,
|
121
120
|
#<Morph::Council @code="2", @name="Allerdale Borough Council">]
|
122
121
|
|
123
|
-
councils.first.name
|
124
|
-
|
125
|
-
# => "Aberdeen City Council"
|
122
|
+
councils.first.name # => "Aberdeen City Council"
|
123
|
+
```
|
126
124
|
|
127
125
|
## Registering a listener to new class / methods via `register_listener`
|
128
126
|
|
@@ -131,27 +129,39 @@ created.
|
|
131
129
|
|
132
130
|
For example given Morph used as a mixin:
|
133
131
|
|
134
|
-
|
132
|
+
```rb
|
133
|
+
class Project
|
134
|
+
include Morph
|
135
|
+
end
|
136
|
+
|
135
137
|
project = Project.new
|
138
|
+
```
|
136
139
|
|
137
140
|
Register listener:
|
138
141
|
|
142
|
+
```rb
|
139
143
|
listener = -> (klass, method) do
|
140
144
|
puts "class: #{klass.to_s} --- method: #{method}"
|
141
145
|
end
|
146
|
+
|
142
147
|
Morph.register_listener listener
|
148
|
+
```
|
143
149
|
|
144
150
|
Callback prints string as new methods are created via assignment calls:
|
145
151
|
|
152
|
+
```rb
|
146
153
|
project.deadline = "11 11 2075"
|
147
154
|
# class: Project --- method: deadline
|
148
155
|
|
149
156
|
project.completed = true
|
150
157
|
# class: Project --- method: completed
|
158
|
+
```
|
151
159
|
|
152
160
|
To unregister a listener use `unregister_listener`:
|
153
161
|
|
162
|
+
```rb
|
154
163
|
Morph.unregister_listener listener
|
164
|
+
```
|
155
165
|
|
156
166
|
For an example of Morph's `register_listener` being used to
|
157
167
|
[create a Github API](https://github.com/robmckinnon/hubbit/blob/master/lib/hubbit.rb)
|
@@ -161,15 +171,19 @@ see the [Hubbit module](https://github.com/robmckinnon/hubbit/blob/master/lib/hu
|
|
161
171
|
|
162
172
|
Time to generate an Active Record model? Get a sample script line like this:
|
163
173
|
|
174
|
+
```rb
|
164
175
|
Morph.script_generate(Project)
|
165
176
|
#=> "rails destroy model Project;
|
166
177
|
# rails generate model Project completed:string deadline:string
|
178
|
+
```
|
167
179
|
|
168
180
|
or specify the generator:
|
169
181
|
|
170
|
-
|
182
|
+
```rb
|
183
|
+
Morph.script_generate(Project, :generator => 'rspec_model')
|
171
184
|
#=> "rails destroy rspec_model Project;
|
172
185
|
# rails generate rspec_model Project completed:string deadline:string
|
186
|
+
```
|
173
187
|
|
174
188
|
You'll have to edit this as it currently sets all data types to be string, and
|
175
189
|
doesn't understand associations.
|
@@ -177,40 +191,58 @@ doesn't understand associations.
|
|
177
191
|
|
178
192
|
## Morph setting hash of attributes via `morph`
|
179
193
|
|
180
|
-
|
194
|
+
```rb
|
195
|
+
class Order
|
196
|
+
include Morph
|
197
|
+
end
|
198
|
+
|
181
199
|
order = Order.new
|
200
|
+
```
|
182
201
|
|
183
202
|
How about adding a hash of attribute values?
|
184
203
|
|
204
|
+
```rb
|
185
205
|
order.morph :drink => 'tea', :spoons_of_sugar => 2, :milk => 'prefer soya thanks'
|
206
|
+
```
|
186
207
|
|
187
208
|
Looks like we got 'em:
|
188
209
|
|
189
|
-
|
210
|
+
```rb
|
211
|
+
order.drink # => "tea"
|
190
212
|
order.spoons_of_sugar # => 2
|
191
|
-
order.milk
|
192
|
-
|
213
|
+
order.milk # => "prefer soya thanks"
|
214
|
+
```
|
193
215
|
|
194
216
|
## Morph obtaining hash of attributes via `morph_attributes`
|
195
217
|
|
196
218
|
Create an item:
|
197
219
|
|
198
|
-
|
220
|
+
```rb
|
221
|
+
class Item
|
222
|
+
include Morph
|
223
|
+
end
|
224
|
+
|
199
225
|
item = Item.new
|
200
226
|
item.morph :name => 'spinach', :cost => 0.50
|
227
|
+
```
|
201
228
|
|
202
229
|
Now an order:
|
203
230
|
|
204
|
-
|
231
|
+
```rb
|
232
|
+
class Order
|
233
|
+
include Morph
|
234
|
+
end
|
235
|
+
|
205
236
|
order = Order.new
|
206
237
|
order.no = 123
|
207
238
|
order.items = [item]
|
239
|
+
```
|
208
240
|
|
209
241
|
Want to retrieve all that as a nested hash of values? No problem:
|
210
242
|
|
211
|
-
|
212
|
-
|
213
|
-
|
243
|
+
```rb
|
244
|
+
order.morph_attributes # => {:items=>[{:name=>"spinach", :cost=>0.5}], :no=>123}
|
245
|
+
```
|
214
246
|
|
215
247
|
|
216
248
|
## Last bits
|
data/lib/morph.rb
CHANGED
@@ -111,7 +111,7 @@ module Chas
|
|
111
111
|
end
|
112
112
|
|
113
113
|
def self.argument_provided? args
|
114
|
-
args.size > 0
|
114
|
+
args.size > 0
|
115
115
|
end
|
116
116
|
|
117
117
|
def self.convert_to_morph_class_name label
|
@@ -132,7 +132,7 @@ module Chas
|
|
132
132
|
end
|
133
133
|
|
134
134
|
module Morph
|
135
|
-
VERSION = '0.
|
135
|
+
VERSION = '0.5.0' unless defined? Morph::VERSION
|
136
136
|
|
137
137
|
class << self
|
138
138
|
def classes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: morph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob McKinnon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
71
|
version: '0'
|
72
72
|
requirements: []
|
73
73
|
rubyforge_project:
|
74
|
-
rubygems_version: 2.
|
74
|
+
rubygems_version: 2.6.4
|
75
75
|
signing_key:
|
76
76
|
specification_version: 4
|
77
77
|
summary: Morph allows you to emerge Ruby class definitions from data or by calling
|