moosex 0.0.1 → 0.0.2

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: 11498c2543b354be73e14d5f4a7acf0b743cc8ca
4
- data.tar.gz: 14d5a801de841fb2edc262b1341ab203db2ee5e2
3
+ metadata.gz: 853ceb9084c1f328f72ef7a1c8c5ccfac1047afd
4
+ data.tar.gz: 4577ed6c0db01b29bc735e7e1dfcdf9ffdffb001
5
5
  SHA512:
6
- metadata.gz: 06879aad4c8e8bc01947fae38781909c5a8042b7e36880290dbed4c9ff4972d7e7f74538690f13da062b987f235fa898334fd62dee9e4332229c3260c82d0534
7
- data.tar.gz: 7afaf26b30e7af09da4f43acbd6d2b1ba2887db37771cbd6325fadb44c6823f2e0c197ee53fc08720e36cdb731a48d9059beeb60dae74ac1c19be40cb3a04fc6
6
+ metadata.gz: 61ae609b6b0c19053e78941b50991e5f88255a4bcf713d443fa48cb171ddbdd4710ea8e592e673eadd163730b97e41be4f79b9bed6bb85b5e651041074b1109c
7
+ data.tar.gz: 57c88e944b04b96657f6f7cc07fbc7f5001fc81b33a6584025efc57dba08b16448f019d74c50689e13e05440d501fc67b6eebd303141cfeaa1cc9b8dd036b8ba
@@ -0,0 +1,9 @@
1
+ 0.0.2 - 2014-01-31
2
+ - fix one important bug, not it is possible use in more than one class
3
+ - add require option
4
+ - supports :ro, :rw, :rwp ( read-only, read-write and read-write-private) options
5
+ - supports default value as lambda or constant
6
+ - supports isa as class/module name or lambda
7
+
8
+ 0.0.1 - 2014-01-31
9
+ - first release
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- moosex (0.0.1)
4
+ moosex (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,27 +1,61 @@
1
- # Moosex
1
+ # MooseX
2
2
 
3
3
  A postmodern object system for Ruby
4
4
 
5
+ ```ruby
6
+ require 'moosex'
7
+
5
8
  class Point
6
9
  include MooseX
7
10
 
8
11
  has :x , {
9
- :is => :rw,
10
- :isa => Integer,
11
- :default => 0,
12
+ :is => :rw, # read-write
13
+ :isa => Integer, # should be Integer
14
+ :default => 0, # default value is 0 (constant)
12
15
  }
13
16
 
14
17
  has :y , {
15
18
  :is => :rw,
16
19
  :isa => Integer,
17
- :default => lambda { 0 },
20
+ :default => lambda { 0 }, # you should specify a lambda
18
21
  }
19
22
 
20
23
  def clear
21
- self.x= 0
22
- self.y= 0
24
+ self.x= 0 # to run with type-check you must
25
+ self.y= 0 # use the setter instad @x=
23
26
  end
24
27
  end
28
+
29
+ class Foo
30
+ include MooseX
31
+
32
+ has :bar, {
33
+ :is => :rwp, # read-write-private (private setter)
34
+ :isa => Integer,
35
+ :required => true # you should require in the constructor
36
+ }
37
+ end
38
+
39
+ class Baz
40
+ include MooseX
41
+
42
+ has :bam, {
43
+ :is => :ro,
44
+ :isa => lambda {|x| # you should add your own validator
45
+ raise 'x should be less than 100' if x > 100
46
+ },
47
+ :required => true
48
+ }
49
+
50
+ end
51
+
52
+ # now you have a generic constructor
53
+ p1 = Point.new # x and y will be 0
54
+ p2 = Point.new( :x => 5 ) # y will be 0
55
+ p3 = Point.new( :x => 5, :y => 4)
56
+ foo = Foo.new( :bar => 123 ) # without bar will raise exception
57
+ baz = Baz.new( :bam => 99 ) # if bam > 100 will raise exception
58
+ ```
25
59
 
26
60
  ## Installation
27
61
 
@@ -11,11 +11,15 @@ module MooseX
11
11
 
12
12
  o.extend(MooseX::Core)
13
13
 
14
- o.class_exec {
15
- @@meta = MooseX::Meta.new()
16
- }
17
- o.define_singleton_method :__meta do
18
- class_variable_get "@@meta".to_sym
14
+ o.class_exec do
15
+ meta = MooseX::Meta.new()
16
+
17
+ #class_variable_set "@@meta".to_sym, meta
18
+
19
+ define_singleton_method :__meta do
20
+ meta
21
+ # class_variable_get "@@meta".to_sym
22
+ end
19
23
  end
20
24
 
21
25
  def initialize(args={})
@@ -30,14 +34,25 @@ module MooseX
30
34
 
31
35
  def has(attr_name, attr_options)
32
36
  attr = MooseX::Attribute.new(attr_name, attr_options)
37
+
33
38
  g = attr.generate_getter
34
39
 
35
- define_method attr_name, &g
36
-
37
- s = attr.generate_setter
40
+ define_method attr_name, &g
38
41
 
39
- define_method "#{attr_name}=", &s
42
+ s = attr.generate_setter
43
+
44
+ if attr_options[:is].eql? :rw
40
45
 
46
+ define_method "#{attr_name}=", &s
47
+
48
+ elsif attr_options[:is].eql? :rwp
49
+
50
+ define_method "#{attr_name}=", &s
51
+
52
+ private "#{attr_name}="
53
+
54
+ end
55
+
41
56
  __meta.add(attr)
42
57
  end
43
58
 
@@ -57,12 +72,25 @@ module MooseX
57
72
  if args.has_key? @attr_symbol
58
73
  value = args[ @attr_symbol ]
59
74
  elsif @options[:required]
60
- raise "ops, attr #{@attr_symbol} is required"
75
+ raise "attr \"#{@attr_symbol}\" is required"
61
76
  else
62
77
  value = (@options[:default].is_a? Proc) ? @options[:default].call : @options[:default]
63
78
  end
64
79
 
65
- object.send( setter, value )
80
+ if @options[:is].eql? :ro
81
+
82
+ # TODO: remove redundancy
83
+
84
+ inst_variable_name = "@#{@attr_symbol}".to_sym
85
+ type_check = generate_type_check
86
+ type_check.call(value)
87
+ object.instance_variable_set inst_variable_name, value
88
+
89
+ else
90
+
91
+ object.send( setter, value )
92
+
93
+ end
66
94
  end
67
95
 
68
96
  def generate_getter
@@ -81,9 +109,12 @@ module MooseX
81
109
 
82
110
  def generate_type_check
83
111
  if @options.has_key? :isa
84
- klass = @options[:isa]
112
+ isa = @options[:isa]
113
+
114
+ return isa if isa.is_a? Proc
115
+
85
116
  return lambda do |new_value|
86
- raise "isa check for \"#{@attr_symbol}\" failed: lol is not #{klass}!" unless new_value.is_a? klass
117
+ raise "isa check for \"#{@attr_symbol}\" failed: lol is not #{isa}!" unless new_value.is_a? isa
87
118
  end
88
119
  end
89
120
 
@@ -1,3 +1,3 @@
1
1
  module Moosex
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -22,7 +22,28 @@ class Point
22
22
  end
23
23
  end
24
24
 
25
- describe "Point" do
25
+ class Foo
26
+ include MooseX
27
+
28
+ has :bar, {
29
+ :is => :rwp,
30
+ :isa => Integer,
31
+ :required => true
32
+ }
33
+ end
34
+
35
+ class Baz
36
+ include MooseX
37
+
38
+ has :bam, {
39
+ :is => :ro,
40
+ :isa => lambda {|x| raise 'x should be less than 100' if x > 100},
41
+ :required => true
42
+ }
43
+
44
+ end
45
+
46
+ describe "MooseX" do
26
47
  describe "should has an intelligent constructor" do
27
48
  it "without arguments, should initialize with default values" do
28
49
  p = Point.new
@@ -41,6 +62,42 @@ describe "Point" do
41
62
  p.x.should == 5
42
63
  p.y.should == 4
43
64
  end
65
+
66
+ it "should require bar if necessary" do
67
+ expect {
68
+ Foo.new
69
+ }.to raise_error("attr \"bar\" is required")
70
+ end
71
+
72
+ it "should require bar if necessary" do
73
+ foo = Foo.new( :bar => 123 )
74
+ foo.bar.should == 123
75
+ end
76
+
77
+ it "should not be possible update bar (setter private)" do
78
+ foo = Foo.new( :bar => 123 )
79
+ expect {
80
+ foo.bar = 1024
81
+ }.to raise_error(NoMethodError)
82
+ end
83
+
84
+ it "should require bam if necessary" do
85
+ baz = Baz.new( :bam => 99 )
86
+ baz.bam.should == 99
87
+ end
88
+
89
+ it "should not be possible update baz (read only)" do
90
+ baz = Baz.new( :bam => 99 )
91
+ expect {
92
+ baz.bam = 1024
93
+ }.to raise_error(NoMethodError)
94
+ end
95
+
96
+ it "should run the lambda isa" do
97
+ expect {
98
+ baz = Baz.new( :bam => 199 )
99
+ }.to raise_error(/x should be less than 100/)
100
+ end
44
101
  end
45
102
 
46
103
  describe "should create a getter and a setter" do
@@ -52,9 +109,17 @@ describe "Point" do
52
109
 
53
110
  it "for x, with type check" do
54
111
  p = Point.new
55
- expect { p.x = "lol" }.to raise_error('isa check for "x" failed: lol is not Integer!')
112
+ expect {
113
+ p.x = "lol"
114
+ }.to raise_error('isa check for "x" failed: lol is not Integer!')
56
115
  end
57
116
 
117
+ it "for x, with type check" do
118
+ expect {
119
+ Point.new(:x => "lol")
120
+ }.to raise_error('isa check for "x" failed: lol is not Integer!')
121
+ end
122
+
58
123
  it "clear should clean attributes" do
59
124
  p = Point.new( :x => 5, :y => 4)
60
125
  p.clear
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moosex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tiago Peczenyj
@@ -63,6 +63,7 @@ extensions: []
63
63
  extra_rdoc_files: []
64
64
  files:
65
65
  - .gitignore
66
+ - Changelog
66
67
  - Gemfile
67
68
  - Gemfile.lock
68
69
  - LICENSE