array_model 1.0.2 → 1.0.3
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/lib/array_model.rb +36 -36
- data/lib/array_model/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: f6a326d4c7ab542f6b7d0ba1ec87342b850450fc
|
4
|
+
data.tar.gz: 5d1f1795ef9afcf734be5053b232b1ab0e82fbc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57dfb8f862d24d0e6b3fa159c67d1b9ac27c726680051260dbd1f904445018532d4449846d8030de07347d533868ff692e8bf9c96930efbd38f76e47663f327f
|
7
|
+
data.tar.gz: 0343679998a7654456cab84afe2befba932be8db6dc42d9dab8cecc3c99c126ba81ac531cabef88a53f6bc249022cdd398a73229e468f115851a3214b8f3ce62
|
data/lib/array_model.rb
CHANGED
@@ -14,23 +14,23 @@ require "array_model/version"
|
|
14
14
|
#
|
15
15
|
# Example:
|
16
16
|
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
17
|
+
# USERS = [
|
18
|
+
# { name: 'Nathan', year: 1984 },
|
19
|
+
# { name: 'Dave', year: 1987 }
|
20
|
+
# ]
|
21
21
|
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
22
|
+
# class User < ArrayModel
|
23
|
+
# model_data USERS
|
24
|
+
# attr_model_reader :name
|
25
|
+
# attr_model_reader :year
|
26
26
|
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
27
|
+
# def age
|
28
|
+
# Time.now.year - year
|
29
|
+
# end
|
30
|
+
# end
|
31
31
|
#
|
32
|
-
#
|
33
|
-
#
|
32
|
+
# User[0].age # => 32
|
33
|
+
# User[1].name # => "Dave"
|
34
34
|
#
|
35
35
|
class ArrayModel
|
36
36
|
# get the object with the key :k:
|
@@ -39,7 +39,7 @@ class ArrayModel
|
|
39
39
|
# any field by using the :primary_key option when calling :model_data:
|
40
40
|
# when the class is defined
|
41
41
|
#
|
42
|
-
#
|
42
|
+
# Users['reednj'] # => #<Users:0x007fe693866808>
|
43
43
|
#
|
44
44
|
def self.[](k)
|
45
45
|
if @data_key.nil?
|
@@ -70,10 +70,10 @@ class ArrayModel
|
|
70
70
|
# prefered to accessing the hash directly, but this can be useful
|
71
71
|
# in certain cases
|
72
72
|
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
73
|
+
# u = Users['reednj']
|
74
|
+
# u.username # => 'reednj'
|
75
|
+
# u.values(:username) # => 'reednj'
|
76
|
+
# u[:username] # => 'reednj'
|
77
77
|
#
|
78
78
|
def [](k)
|
79
79
|
values[k]
|
@@ -82,7 +82,7 @@ class ArrayModel
|
|
82
82
|
# returns the raw Hash that provides the data for the model
|
83
83
|
# object
|
84
84
|
#
|
85
|
-
#
|
85
|
+
# Users['reednj'].values # => {:username => 'reednj', ...}
|
86
86
|
#
|
87
87
|
def values
|
88
88
|
@item_data
|
@@ -101,12 +101,12 @@ class ArrayModel
|
|
101
101
|
# of the key in the hash, if it doesn't have the same name as the
|
102
102
|
# method
|
103
103
|
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
104
|
+
# class Users < ArrayModel
|
105
|
+
# ...
|
106
|
+
# attr_model_reader :username
|
107
|
+
# attr_model_reader :user_id, :key => :userId
|
108
|
+
# ...
|
109
|
+
# end
|
110
110
|
#
|
111
111
|
def self.attr_model_reader(name, options = {})
|
112
112
|
define_method name.to_sym do
|
@@ -118,11 +118,11 @@ class ArrayModel
|
|
118
118
|
# once. No options can be passed when using this method to add
|
119
119
|
# the readers
|
120
120
|
#
|
121
|
-
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
121
|
+
# class Users < ArrayModel
|
122
|
+
# ...
|
123
|
+
# attr_model_readers [:username, :user_id]
|
124
|
+
# ...
|
125
|
+
# end
|
126
126
|
#
|
127
127
|
def self.attr_model_readers(keys)
|
128
128
|
keys.each {|k| attr_model_reader k }
|
@@ -140,11 +140,11 @@ class ArrayModel
|
|
140
140
|
# in the hash when it is accessed later via the subscript operator. If this option
|
141
141
|
# is ommited then the data will be accessable simply by the array index
|
142
142
|
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
143
|
+
# class Users < ArrayModel
|
144
|
+
# # USER_LIST is a const containing an array of hashes
|
145
|
+
# model_data USER_LIST, :primary_key => :username
|
146
|
+
# ...
|
147
|
+
# end
|
148
148
|
#
|
149
149
|
def self.model_data(data, options = nil)
|
150
150
|
options ||= {}
|
data/lib/array_model/version.rb
CHANGED