defgen 0.0.1 → 0.0.2
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.
- data/README.md +39 -2
- data/lib/defgen/cli.rb +2 -3
- data/lib/defgen/decorators/getter_decorator.rb +8 -0
- data/lib/defgen/defaultfile_parser.rb +3 -2
- data/lib/defgen/property.rb +1 -0
- data/lib/defgen/property/attributes.rb +8 -2
- data/lib/defgen/version.rb +1 -1
- data/spec/unit/decorators/getter_decorator_spec.rb +5 -0
- data/spec/unit/defaultfile_parser_spec.rb +20 -0
- data/spec/unit/property/attributes_spec.rb +9 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -49,6 +49,44 @@ The DSL should be pretty self explanatory call the `type` of the object followed
|
|
49
49
|
|
50
50
|
###Customisation
|
51
51
|
|
52
|
+
Sometimes you want to have a different getter so you can follow conventions like having a boolean `isValid` and `setValid:`, which sounds nicer than `isValid` and `setIsValid:`
|
53
|
+
|
54
|
+
Simple...
|
55
|
+
|
56
|
+
bool 'valid', :getter => 'isValid'
|
57
|
+
|
58
|
+
which generates
|
59
|
+
|
60
|
+
// NSUserDefaults+PASProperties.h
|
61
|
+
|
62
|
+
@property (nonatomic, assign, getter=pas_isValid) BOOL pas_valid;
|
63
|
+
|
64
|
+
// NSUserDefaults+PASProperties.m
|
65
|
+
|
66
|
+
- (BOOL)pas_isValid;
|
67
|
+
{
|
68
|
+
return [self boolForKey:PASValid];
|
69
|
+
}
|
70
|
+
|
71
|
+
- (void)setPas_valid:(BOOL)isValid;
|
72
|
+
{
|
73
|
+
[self setBool:isValid forKey:PASValid];
|
74
|
+
}
|
75
|
+
|
76
|
+
---
|
77
|
+
|
78
|
+
If you don't like my choice of using `strong` for object properties you can override this in the same way
|
79
|
+
|
80
|
+
array 'colours', :ownership => :weak
|
81
|
+
|
82
|
+
which generates
|
83
|
+
|
84
|
+
// NSUserDefaults+PASProperties.h
|
85
|
+
|
86
|
+
@property (nonatomic, weak) NSArray *pas_colours;
|
87
|
+
|
88
|
+
###Advanced Customisation
|
89
|
+
|
52
90
|
If you don't like the supplied boiler plate then you can tweak it.
|
53
91
|
|
54
92
|
####Step 1. Call generate the stubs
|
@@ -143,8 +181,7 @@ This will generate the templates in your current project so that you can edit as
|
|
143
181
|
|
144
182
|
##Wish List
|
145
183
|
|
146
|
-
-
|
147
|
-
- Support for overriding ownership qualifiers
|
184
|
+
- Dry runs to show what will be generated
|
148
185
|
|
149
186
|
##Licence
|
150
187
|
|
data/lib/defgen/cli.rb
CHANGED
@@ -11,10 +11,9 @@ module Defgen
|
|
11
11
|
|
12
12
|
desc "parse", 'Parse a "Defaultfile" and modify the Xcodeproj'
|
13
13
|
method_option :filename, default: 'Defaultfile',
|
14
|
-
|
15
|
-
|
14
|
+
aliases: '-f',
|
15
|
+
desc: 'Specify the input file path'
|
16
16
|
def parse
|
17
|
-
|
18
17
|
path = Dir['*.xcodeproj'].first
|
19
18
|
|
20
19
|
if path.nil?
|
@@ -20,13 +20,14 @@ module Defgen
|
|
20
20
|
[ 'double', 'double', 'double ', :assign, false ],
|
21
21
|
[ 'url', 'URL', 'NSURL *', :strong, false ]
|
22
22
|
].each do |method_name, prefix, objc_type, ownership, object_setter|
|
23
|
-
define_method method_name do |name|
|
23
|
+
define_method method_name do |name, options={}|
|
24
24
|
Property.new do |property, attributes|
|
25
25
|
property.name = name
|
26
26
|
property.object_setter = object_setter
|
27
27
|
property.message_prefix = prefix
|
28
28
|
property.objc_type = objc_type
|
29
|
-
attributes.ownership = ownership
|
29
|
+
attributes.ownership = options[:ownership] || ownership
|
30
|
+
attributes.getter = options[:getter]
|
30
31
|
|
31
32
|
@properties << property
|
32
33
|
end
|
data/lib/defgen/property.rb
CHANGED
@@ -3,7 +3,7 @@ module Defgen
|
|
3
3
|
class UnsupportedOwnershipError < ArgumentError; end
|
4
4
|
class Attributes
|
5
5
|
VALID_OWNERSHIPS = [:strong, :weak, :assign, :unsafe_unretained, :retain, :copy]
|
6
|
-
attr_accessor :ownership
|
6
|
+
attr_accessor :ownership, :getter, :property
|
7
7
|
|
8
8
|
def initialize
|
9
9
|
@ownership = :strong
|
@@ -22,7 +22,13 @@ module Defgen
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def to_s
|
25
|
-
"(#{['nonatomic', ownership].compact.join(', ')})"
|
25
|
+
"(#{['nonatomic', ownership, formatted_getter].compact.join(', ')})"
|
26
|
+
end
|
27
|
+
|
28
|
+
def formatted_getter
|
29
|
+
unless getter.nil? || getter.empty?
|
30
|
+
"getter=#{property.prefix.downcase}_#{getter}"
|
31
|
+
end
|
26
32
|
end
|
27
33
|
end
|
28
34
|
end
|
data/lib/defgen/version.rb
CHANGED
@@ -38,5 +38,25 @@ module Defgen
|
|
38
38
|
property.attributes.ownership.should eq(ownership)
|
39
39
|
end
|
40
40
|
end
|
41
|
+
|
42
|
+
it "can handle a custom getter" do
|
43
|
+
property = nil
|
44
|
+
|
45
|
+
expect do
|
46
|
+
property = defaultfile_parser.parse("bool 'valid', :getter => 'isValid'").first
|
47
|
+
end.to_not raise_error
|
48
|
+
|
49
|
+
property.attributes.getter.should eq('isValid')
|
50
|
+
end
|
51
|
+
|
52
|
+
it "can handle overriding ownership" do
|
53
|
+
property = nil
|
54
|
+
|
55
|
+
expect do
|
56
|
+
property = defaultfile_parser.parse("array 'myCollection', :ownership => :weak").first
|
57
|
+
end.to_not raise_error
|
58
|
+
|
59
|
+
property.attributes.ownership.should eq(:weak)
|
60
|
+
end
|
41
61
|
end
|
42
62
|
end
|
@@ -6,7 +6,9 @@ module Defgen
|
|
6
6
|
describe Attributes do
|
7
7
|
subject(:attributes) { Attributes.new }
|
8
8
|
|
9
|
+
it { should respond_to :property }
|
9
10
|
it { should respond_to :ownership }
|
11
|
+
it { should respond_to :getter }
|
10
12
|
|
11
13
|
Attributes::VALID_OWNERSHIPS.each do |semantic|
|
12
14
|
it "creates the predicate method #{semantic}?" do
|
@@ -43,6 +45,13 @@ module Defgen
|
|
43
45
|
attributes.to_s.should eq('(nonatomic)')
|
44
46
|
end
|
45
47
|
end
|
48
|
+
|
49
|
+
it "uses a custom getter" do
|
50
|
+
FakeProperty = Struct.new(:prefix)
|
51
|
+
attributes.property = FakeProperty.new('ps')
|
52
|
+
attributes.getter = 'isValid'
|
53
|
+
attributes.to_s.should eq('(nonatomic, strong, getter=ps_isValid)')
|
54
|
+
end
|
46
55
|
end
|
47
56
|
end
|
48
57
|
end
|