husky 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/husky/entity.rb +31 -13
- data/lib/husky/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c41bb95e59ad9b0b2b65042b56014623fef5b7e
|
4
|
+
data.tar.gz: bcb31362d91d1172647d631905c8a051757d8f71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b6c405cbea1bb6ac5ec07bee2c2789c235f7e58a079324dfc6a502118569f9861814c24368bb15dbc322ed7fe15be11dfc5c6ca53b9f5ffff2003abfa551e02
|
7
|
+
data.tar.gz: acad03874d6bab7f0983d14359556751ead6207c44a7ceba73f09c6a648b2f8539f4dc9fa80d3261eb21307403a9b0e37c590853d89fd67058dd4079b675a902
|
data/lib/husky/entity.rb
CHANGED
@@ -1,29 +1,47 @@
|
|
1
|
-
require '
|
1
|
+
require 'delegate'
|
2
2
|
|
3
3
|
module Husky
|
4
4
|
|
5
|
-
class Entity
|
6
|
-
extend Forwardable
|
5
|
+
class Entity < SimpleDelegator
|
7
6
|
|
8
7
|
class << self
|
9
8
|
|
10
|
-
def fields(*field_names)
|
11
|
-
field_names.each do |field_name|
|
12
|
-
def_delegators :_data, field_name
|
13
|
-
def_delegators :_data, "#{field_name}="
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
9
|
def wrap(items)
|
18
10
|
items.map { |item| new(item) }
|
19
11
|
end
|
20
12
|
|
13
|
+
def new_from_hash(hash)
|
14
|
+
raise hash_error(hash) unless hash.is_a? Hash
|
15
|
+
instance = allocate
|
16
|
+
instance.initialize_from_hash(hash)
|
17
|
+
instance
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def hash_error(hash)
|
23
|
+
"Entity#initialize_from_hash expects a hash as an argument, and you provided a #{hash.class.name} (#=> #{hash})."
|
24
|
+
end
|
25
|
+
|
21
26
|
end
|
22
27
|
|
23
|
-
attr_reader :
|
28
|
+
attr_reader :object
|
29
|
+
|
30
|
+
def initialize(object)
|
31
|
+
@object = object
|
32
|
+
super
|
33
|
+
end
|
24
34
|
|
25
|
-
def
|
26
|
-
|
35
|
+
def initialize_from_hash(post_data)
|
36
|
+
post_data.each_pair do |key, value|
|
37
|
+
instance_variable_set("@#{key}", value)
|
38
|
+
end
|
39
|
+
|
40
|
+
post_data.each_pair do |key, value|
|
41
|
+
self.class.send(:define_method, key) do
|
42
|
+
instance_variable_get("@#{key}")
|
43
|
+
end
|
44
|
+
end
|
27
45
|
end
|
28
46
|
|
29
47
|
end
|
data/lib/husky/version.rb
CHANGED