idolent 0.0.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.
- data/lib/idolent.rb +154 -0
- metadata +46 -0
data/lib/idolent.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
module Idolent
|
2
|
+
attr_reader :variables
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(LazyClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(variables = {})
|
9
|
+
@variables = variables
|
10
|
+
end
|
11
|
+
|
12
|
+
def read_variable(variable)
|
13
|
+
return @variables[variable.to_s] if @variables.has_key?(variable.to_s)
|
14
|
+
return @variables[variable.to_sym] if @variables.has_key?(variable.to_sym)
|
15
|
+
return nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_variable(variable, value)
|
19
|
+
@variables[variable.to_s] = value if @variables.has_key?(variable.to_s)
|
20
|
+
@variables[variable.to_sym] = value if @variables.has_key?(variable.to_sym)
|
21
|
+
end
|
22
|
+
|
23
|
+
class LazyArray
|
24
|
+
def initialize(array = [], clazz)
|
25
|
+
raise "class #{clazz} must have a load method" unless clazz.respond_to?(:load)
|
26
|
+
raise "class #{clazz} must have a dump method" unless clazz.respond_to?(:dump)
|
27
|
+
@array = array
|
28
|
+
@clazz = clazz
|
29
|
+
end
|
30
|
+
|
31
|
+
def method_missing(method, *args, &block)
|
32
|
+
case method
|
33
|
+
when :each, :select, :collect, :map, :reject, :delete_if
|
34
|
+
@array.send(method) { |e| yield @clazz.load(e) }
|
35
|
+
else
|
36
|
+
@array.send(method, *args, &block)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def first
|
41
|
+
@clazz.load(@array.first)
|
42
|
+
end
|
43
|
+
|
44
|
+
def last
|
45
|
+
@clazz.load(@array.last)
|
46
|
+
end
|
47
|
+
|
48
|
+
def [](index)
|
49
|
+
@clazz.load(@array[index])
|
50
|
+
end
|
51
|
+
|
52
|
+
def []=(index, value)
|
53
|
+
@array[index] = @clazz.dump(value)
|
54
|
+
end
|
55
|
+
|
56
|
+
def <<(value)
|
57
|
+
@array << @clazz.dump(value)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class LazyHash
|
62
|
+
def initialize(hash = {}, clazz)
|
63
|
+
raise "class #{clazz} must have a load method" unless clazz.respond_to?(:load)
|
64
|
+
raise "class #{clazz} must have a dump method" unless clazz.respond_to?(:dump)
|
65
|
+
@hash = hash
|
66
|
+
@clazz = clazz
|
67
|
+
end
|
68
|
+
|
69
|
+
def method_missing(method, *args, &block)
|
70
|
+
case method
|
71
|
+
when :each, :each_pair
|
72
|
+
@hash.send(method) { |k,v| yield k, @clazz.load(v) }
|
73
|
+
when :each_value
|
74
|
+
@hash.send(method) { |v| yield @clazz.load(v) }
|
75
|
+
when :fetch
|
76
|
+
@clazz.load(@hash.send(method, *args))
|
77
|
+
else
|
78
|
+
@hash.send(method, *args, &block)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def [](key)
|
83
|
+
if @hash.has_key?(key.to_s)
|
84
|
+
@clazz.load(@hash[key.to_s])
|
85
|
+
elsif @hash.has_key?(key.to_sym)
|
86
|
+
@clazz.load(@hash[key.to_sym])
|
87
|
+
else
|
88
|
+
nil
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def []=(key, value)
|
93
|
+
if @hash.has_key?(key.to_s)
|
94
|
+
@hash[key.to_s] = @clazz.dump(value)
|
95
|
+
elsif @hash.has_key?(key.to_sym)
|
96
|
+
@hash[key.to_sym] = @clazz.dump(value)
|
97
|
+
else
|
98
|
+
@hash[key] = @clazz.dump(value)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def values
|
103
|
+
@hash.values.collect { |v| @clazz.new(v) }
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
module LazyClassMethods
|
108
|
+
def attr_accessor(*variables)
|
109
|
+
variables.each do |variable|
|
110
|
+
define_method(variable) { read_variable(variable) }
|
111
|
+
define_method("#{variable}=".to_sym) { |value| update_variable(variable, value) }
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def attr_reader(*variables)
|
116
|
+
variables.each do |variable|
|
117
|
+
define_method(variable) { read_variable(variable) }
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def attr_writer(*variables)
|
122
|
+
variables.each do |variable|
|
123
|
+
define_method("#{variable}=".to_sym) { |value| update_variable(variable, value) }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def lazy(variable, clazz, options = {})
|
128
|
+
raise "class #{clazz} must have a load method" unless clazz.respond_to?(:load)
|
129
|
+
raise "class #{clazz} must have a dump method" unless clazz.respond_to?(:dump)
|
130
|
+
define_method(variable) { clazz.load(read_variable(variable)) }
|
131
|
+
define_method("#{variable}=") { |value| update_variable(variable, clazz.dump(value)) }
|
132
|
+
end
|
133
|
+
|
134
|
+
def lazy_array(variable, clazz, options = {})
|
135
|
+
raise "class #{clazz} must have a load method" unless clazz.respond_to?(:load)
|
136
|
+
raise "class #{clazz} must have a dump method" unless clazz.respond_to?(:dump)
|
137
|
+
define_method(variable) { LazyArray.new(read_variable(variable), clazz) }
|
138
|
+
end
|
139
|
+
|
140
|
+
def lazy_hash(variable, clazz, options = {})
|
141
|
+
raise "class #{clazz} must have a load method" unless clazz.respond_to?(:load)
|
142
|
+
raise "class #{clazz} must have a dump method" unless clazz.respond_to?(:dump)
|
143
|
+
define_method(variable) { LazyHash.new(read_variable(variable), clazz) }
|
144
|
+
end
|
145
|
+
|
146
|
+
def load(o)
|
147
|
+
self.new(o)
|
148
|
+
end
|
149
|
+
|
150
|
+
def dump(o)
|
151
|
+
o.variables
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: idolent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David M Good
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-15 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Lazy load your attributes for any class object
|
15
|
+
email: akabigbro@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/idolent.rb
|
21
|
+
homepage: http://github.com/akabigbro/idolent
|
22
|
+
licenses:
|
23
|
+
- Apache License, Version 2.0
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.25
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Idolent
|
46
|
+
test_files: []
|