cim_attributes 1.1.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.
Files changed (2) hide show
  1. data/lib/cim_attributes.rb +83 -0
  2. metadata +64 -0
@@ -0,0 +1,83 @@
1
+ module CIMAttributes
2
+
3
+ VERSION = '1.1.0'
4
+
5
+ module ClassMethods
6
+
7
+ def cim_attr_reader(*args)
8
+ raise ArgumentError, 'no args for cim_attr_reader' unless args.size > 0
9
+ opts = if args.last.is_a?(Hash)
10
+ args.pop
11
+ else
12
+ {}
13
+ end
14
+ args.each do |attr|
15
+
16
+ ## class methods
17
+
18
+ target, message = *(if self.respond_to? :define_singleton_method
19
+ [self, :define_singleton_method]
20
+ else
21
+ [class << self; self; end, :define_method]
22
+ end)
23
+
24
+ target.send message, attr do
25
+ self.instance_variable_get("@#{attr}") || if self.superclass.respond_to?(attr)
26
+ self.superclass.send(attr)
27
+ else
28
+ nil
29
+ end
30
+ end
31
+
32
+ target.send message, "#{attr}=" do |val|
33
+ self.instance_variable_set("@#{attr}", val)
34
+ end
35
+
36
+ target.send message, "with_#{attr}" do |val, &blk|
37
+ if val || (val = self.send(attr))
38
+ blk.call(val)
39
+ else
40
+ raise("no #{attr} defined for #{self} and none passed in")
41
+ end
42
+ end
43
+
44
+ ## instance methods
45
+
46
+ self.send :define_method, attr do
47
+ self.instance_variable_get("@#{attr}") || self.class.send(attr)
48
+ end
49
+
50
+ self.send :define_method, "#{attr}=" do |val|
51
+ self.instance_variable_set("@#{attr}", val)
52
+ end
53
+
54
+ self.send :define_method, "ensure_#{attr}!" do
55
+ if !self.send(attr)
56
+ raise("no #{attr} defined for #{self}")
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ def cim_attr_writer(*args)
63
+ raise ArgumentError, 'no args for cim_attr_writer' unless args.size > 0
64
+ opts = if args.last.is_a?(Hash)
65
+ args.pop
66
+ else
67
+ {}
68
+ end
69
+ end
70
+
71
+ def cim_attr_accessor(*args)
72
+ raise ArgumentError, 'no args for cim_attr_accessor' unless args.size > 0
73
+ cim_attr_reader(*args)
74
+ cim_attr_writer(*args)
75
+ end
76
+
77
+ end
78
+
79
+ def self.included(base)
80
+ base.extend(ClassMethods)
81
+ end
82
+
83
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cim_attributes
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 0
9
+ version: 1.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Ben Lund
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-06-16 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Ruby module to have shared or distinct attributes among classes, sub-classes, instances and methods
22
+ email: ben@benlund.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/cim_attributes.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/benlund/cim_attributes
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.7
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Like attr_accessor, cattr_accessor, class_inheritable_accessor, but allows you to set the attribute for all instances, or just instances of a sub-class, or just a particular instance.
63
+ test_files: []
64
+