subvirtus 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e8d26ca2032a9d66fb6351b69880a5a65fe57c2
4
- data.tar.gz: ba8e33ef1b4a18bc8195ead9383286a666ce1039
3
+ metadata.gz: 16e5fe894b8ed463d599454b0e07c9ac9e7f0545
4
+ data.tar.gz: 8e2350fd238c3ac608befa6a08315a906d504bbc
5
5
  SHA512:
6
- metadata.gz: 325643b30607e53e7e3e3cde425ea09119078cd3f0ca0cbf8e2eb72411815037929c98a17beb80d967d8a30504e5fdfcc6b79e81b63ec6b6346ef857f3de9748
7
- data.tar.gz: d6c66ed34109c58df528db286e1218e13ad495243e6c4b0ab7b536f03de58c81fdd7a28a594c66a19c14fe189e2834af614dd0aa3899cdc471533db3da675950
6
+ metadata.gz: 3a58373b048b19ee5adb04b8a8cbe6a6b4520ddf3d4b4a6ffb54a4fea7ba05e2c00907441f9d58d287aa51afc115ecdc4aef7689aa411a0fa7ab3e6d1c25acd2
7
+ data.tar.gz: c0b4733310468d64d803b678d43cce42e0dbd5459f5a54460fa0a61a5474be440009abd31f0a5a2126b50311fda91895fbc1be827a636b7fd35a8feb9842a856
@@ -0,0 +1,134 @@
1
+ module Subvirtus
2
+
3
+ module Boolean; end
4
+
5
+ module ClassMethods
6
+
7
+ attr_reader :attributes
8
+ attr_reader :set_attributes
9
+ attr_reader :value_attributes
10
+
11
+ def attribute( name, type = nil, options = {} )
12
+ if @in_values
13
+ @value_attributes ||= []
14
+ @value_attributes << name unless @value_attributes.include? name
15
+ end
16
+ @attributes ||= []
17
+ @attributes << name unless @attributes.include? name
18
+ define_method( "#{ name }=") do |value|
19
+ @set_attributes ||= []
20
+ @set_attributes << name
21
+ instance_variable_set "@#{ name }", value
22
+ end
23
+ define_method( name ) do
24
+ value = instance_variable_get "@#{ name }"
25
+ coercer = options[ :coercer ]
26
+ default = options[ :default ]
27
+ if coercer
28
+ coercer.call value
29
+ elsif value.nil? and not default.nil? and default.is_a? Symbol and respond_to? default
30
+ send default
31
+ else
32
+ if type.is_a? Array
33
+ if value.nil?
34
+ []
35
+ elsif value.is_a? Array
36
+ value.map { |v| determine_value name, v, type.first, options }
37
+ else
38
+ determine_value name, value, type.first, options
39
+ end
40
+ else
41
+ determine_value name, value, type, options
42
+ end
43
+ end
44
+ end
45
+ private :"#{ name }" if options[ :reader ] == :private
46
+ self
47
+ end
48
+ end
49
+
50
+ def self.value_object
51
+ require_relative 'subvirtus/value_object'
52
+ Subvirtus::ValueObject
53
+ end
54
+
55
+ def self.included( base )
56
+ base.extend ClassMethods
57
+ end
58
+
59
+ def attributes
60
+ Hash[ self.class.attributes.map { |v| [ v, send( v ) ] } ] || {}
61
+ end
62
+
63
+ def value_attributes
64
+ self.class.value_attributes
65
+ end
66
+
67
+ def []( key )
68
+ send( key ) if self.class.attributes.include? key
69
+ end
70
+
71
+ private
72
+
73
+ def determine_value( name, value, type, options )
74
+ default = options[ :default ]
75
+ if value.nil? and not default.nil?
76
+ value = default
77
+ elsif type and not value.is_a? type
78
+ if not value.nil? and @set_attributes.include? name
79
+ if value.is_a? Hash and not type.is_a? Hash
80
+ value = type.new value
81
+ else
82
+ value = convert value, type
83
+ end
84
+ end
85
+ end
86
+ value
87
+ end
88
+
89
+ def initialize( params = {} )
90
+ unless params.nil?
91
+ params.each do |name, value|
92
+ self.send( :"#{name}=", value ) if respond_to? "#{ name }="
93
+ end
94
+ end
95
+ self.class.send( :define_method, :to_hash ) do
96
+ Hash[ self.class.attributes.map { |attribute| [ attribute, send( attribute ) ] } ]
97
+ end
98
+ end
99
+
100
+ def convert( value, type )
101
+ case type.to_s
102
+ when 'String'
103
+ value.to_s
104
+ when 'Integer', 'Fixnum', 'Bignum'
105
+ if value.respond_to? :to_i
106
+ value.to_i
107
+ elsif value.is_a? TrueClass or value.is_a? FalseClass
108
+ value ? 1 : 0
109
+ else
110
+ 0
111
+ end
112
+ when 'Float'
113
+ if value.respond_to? :to_f
114
+ value.to_f
115
+ elsif value.is_a? TrueClass or value.is_a? FalseClass
116
+ value ? 1.0 : 0.0
117
+ end
118
+ when 'Subvirtus::Boolean'
119
+ if value.nil?
120
+ false
121
+ elsif value.is_a? String
122
+ value != ''
123
+ elsif value.is_a? Integer
124
+ value != 0
125
+ elsif value.is_a? TrueClass or value.is_a? FalseClass
126
+ value
127
+ elsif value.is_a? Float
128
+ value != 0.0
129
+ else
130
+ true
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,24 @@
1
+ module Subvirtus
2
+ module ValueObject
3
+ module ClassMethods
4
+ def values
5
+ @in_values = true
6
+ yield
7
+ @in_values = false
8
+ self
9
+ end
10
+ end
11
+
12
+ def self.included( base )
13
+ base.include Subvirtus
14
+ base.extend ClassMethods
15
+ end
16
+
17
+ def ==( object )
18
+ self.value_attributes.each do |vattr|
19
+ return false unless object.respond_to?( vattr ) and object.send( vattr ) == send( vattr )
20
+ end
21
+ true
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Subvirtus
2
+ VERSION = '0.0.6'
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subvirtus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cole
@@ -48,6 +48,9 @@ extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
50
  - README.md
51
+ - lib/subvirtus.rb
52
+ - lib/subvirtus/value_object.rb
53
+ - lib/subvirtus/version.rb
51
54
  homepage: http://github.com/primedia/subvirtus
52
55
  licenses: []
53
56
  metadata: {}