structure 0.27.5 → 0.27.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/structure.rb +23 -12
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bfa61be65533087a6a80a31bfac5e2ad42e4265
4
- data.tar.gz: 95de6560a9bfc44d96996316f4882013f81cc063
3
+ metadata.gz: 7923734fab4cbf99ced503dd98c06fd8c6216c23
4
+ data.tar.gz: 8d8b1dda5952d0fb8a9ff5ca9ea790c34d489304
5
5
  SHA512:
6
- metadata.gz: 0620ac53aa5f5fc3b0251c9fe146df6e8c18506e85adc53a0f4c86db4342f533bfbae716c007f541bb12de955e6b7f3bdac4de2bf4ddd17552995defd2b038bc
7
- data.tar.gz: e35884dfea3921545a678245fdad78513f0bce1be1727adedbf85da8d5d30b843b4ae12eaa7b8ac3fe653313c68425a3fd012684ac342bffd82a0fecac4c3016
6
+ metadata.gz: 3fbdcbf61a7d398c76d5296832ae31e9c1e9808c3ce4d2c27b8227faf14092ca35cc1a2fed2ffea092060355107bfee4e91b0087ebddf5449f1aab5fe7b11785
7
+ data.tar.gz: 446a6f29b1944961e814f2b8fcdf5fe567be66c83f5dc5f604d822686fb667b6f9c6397fbe5550f885854be7d870296ead5da8fa662b76adb9045dde81d5bf0a
data/structure.rb CHANGED
@@ -1,16 +1,18 @@
1
1
  module Structure
2
2
  def self.included(base)
3
- base
4
- .extend(ClassMethods)
5
- .instance_variable_set(:@attribute_names, [])
3
+ base.extend(ClassMethods).instance_variable_set(:@attribute_names, [])
6
4
  end
7
5
 
8
6
  def attributes
9
- self.class.attribute_names.reduce({}) { |ret, name|
7
+ attribute_names.reduce({}) { |ret, name|
10
8
  ret.update(name => self.send(name))
11
9
  }
12
10
  end
13
11
 
12
+ def attribute_names
13
+ self.class.attribute_names
14
+ end
15
+
14
16
  def ==(other)
15
17
  attributes == other.attributes
16
18
  end
@@ -37,13 +39,25 @@ module Structure
37
39
  attr_reader :attribute_names
38
40
 
39
41
  def to_struct
40
- return Struct.const_get(name, false) if Struct.const_defined?(name, false)
42
+ class_name = name || to_s.gsub(/\W/, '')
41
43
 
42
- Struct.new(name, *attribute_names) do
44
+ if Struct.const_defined?(class_name, false)
45
+ return Struct.const_get(class_name, false)
46
+ end
47
+
48
+ klass = Struct.new(class_name, *attribute_names) do
43
49
  def initialize(data = {})
44
50
  data.each { |key, val| self.send("#{key}=", val) }
45
51
  end
46
52
  end
53
+
54
+ attribute_names.each do |name|
55
+ if instance_methods(false).include?(:"#{name}?")
56
+ klass.module_eval "def #{name}?; #{name} end"
57
+ end
58
+ end
59
+
60
+ klass
47
61
  end
48
62
 
49
63
  def inherited(subclass)
@@ -51,15 +65,12 @@ module Structure
51
65
  end
52
66
 
53
67
  def attribute(name, &blk)
68
+ name = name.to_s
69
+ module_eval "def #{name}?; #{name}; end" if name.chomp!('?')
70
+ module_eval "def #{name}; @#{name} ||= _#{name}; end"
54
71
  define_method("_#{name}", blk)
55
72
  private "_#{name}"
56
73
 
57
- module_eval <<-END
58
- def #{name}
59
- @#{name} ||= _#{name}
60
- end
61
- END
62
-
63
74
  @attribute_names << name
64
75
  end
65
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.27.5
4
+ version: 0.27.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari