structure 0.27.8 → 0.28.0

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 +16 -19
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87ccc13eb4c4c3ebb122b04e5c90c499c7e3a840
4
- data.tar.gz: a642d9034c315181ff036020e24446cc99814e10
3
+ metadata.gz: b00ee1d574dcbdc3668117ac11a31d88fe6e716e
4
+ data.tar.gz: 78d66f77d545b0f073abe82154b25c26a297fdf1
5
5
  SHA512:
6
- metadata.gz: bdfb94870cf3cc49631f0ae1daec590cbae633944e8753857e57440849e6ea85c9119fd4005cf4e438b6f4749677fce5b93204140b09c0c19200217fca62e9bd
7
- data.tar.gz: a8b143921b69c92d6ef49cf3853e9ce3f5381bcc6ee9d2637e2dcf6db7f16161ce9190cbb0b197f983e4b2a4be164bde283801d4ea84834274f25440a0d8c45a
6
+ metadata.gz: 2d72544e4291e8f7ea6a614725a0b19fecabfb5777a80f5fab7d339fa777b631aa49bfd15918e6623aac54a59d9351f0315c1845bdeedd0cf43c426500e731d4
7
+ data.tar.gz: 00c05b68d2db9b84ca6acd6933f2613d105b751dc996ca74a73676ecfd748388de2cc16d5a50939b20b0386f30eefd03f6d90fe6cc437e6a782ff47b756dad4d
data/structure.rb CHANGED
@@ -40,30 +40,25 @@ module Structure
40
40
  module ClassMethods
41
41
  attr_reader :attribute_names
42
42
 
43
- def to_struct(&blk)
44
- class_name = name || to_s.gsub(/\W/, '')
43
+ def double(&blk)
44
+ klass = Class.new(self)
45
45
 
46
- if Struct.const_defined?(class_name, false)
47
- return Struct.const_get(class_name, false)
48
- end
46
+ (private_instance_methods(false) + protected_instance_methods(false) - [:initialize])
47
+ .each do |mth|
48
+ klass.send(:undef_method, mth)
49
+ end
49
50
 
50
- klass = Struct.new(class_name, *attribute_names) do
51
+ klass.module_eval do
51
52
  def initialize(data = {})
52
- data.each { |key, val| self.send("#{key}=", val) }
53
+ data.each { |key, val| instance_variable_set(:"@#{key}", val.freeze) }
53
54
  end
54
- end
55
-
56
- klass.module_eval(&blk) if block_given?
57
55
 
58
- included_modules.each do |m|
59
- next if m == Structure
60
- klass.send(:include, m) unless klass.include?(m)
61
- end
62
-
63
- attribute_names.each do |name|
64
- if instance_methods(false).include?(:"#{name}?")
65
- klass.module_eval "def #{name}?; #{name}; end"
56
+ attribute_names.each do |name|
57
+ module_eval "def _#{name}; @#{name}; end"
58
+ private "_#{name}"
66
59
  end
60
+
61
+ module_eval(&blk) if block_given?
67
62
  end
68
63
 
69
64
  klass
@@ -73,10 +68,12 @@ module Structure
73
68
  subclass.instance_variable_set(:@attribute_names, @attribute_names.dup)
74
69
  end
75
70
 
71
+ private
72
+
76
73
  def attribute(name, &blk)
77
74
  name = name.to_s
78
75
  module_eval "def #{name}?; #{name}; end" if name.chomp!('?')
79
- module_eval "def #{name}; @#{name} ||= _#{name}; end"
76
+ module_eval "def #{name}; @#{name} ||= _#{name}.freeze; end"
80
77
  define_method("_#{name}", blk)
81
78
  private "_#{name}"
82
79
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.27.8
4
+ version: 0.28.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-11 00:00:00.000000000 Z
11
+ date: 2014-07-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: me@hakanensari.com