Module: Nova::Helper::ObjCDSL

Included in:
Nova::Helper
Defined in:
lib/helpers/dsl.rb

Constant Summary collapse

SYSTEM_LIBS =
{
    foundation: "Foundation/Foundation.h",
    uikit: "UIKit/UIKit.h"
}

Instance Method Summary collapse

Instance Method Details

#_class(name) ⇒ Object

interface



150
151
152
# File 'lib/helpers/dsl.rb', line 150

def _class(name)
    raw "@class #{name};"
end

#class_method(*options) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/helpers/dsl.rb', line 181

def class_method(*options)
    options = options.flatten
    raise "Method options.count < 2" unless options.count > 1
    return_type = options.shift
    start = "+ (#{return_type})"
    if options.length == 1
        start += "#{options[0]}"
    else
        raise "Not valid method signature" unless options.count % 3 == 0
        options.permutation(3) do |a,b,c|
            start += "#{a}:(#{b})#{c} "
        end
    end
    if block_given?
        start += "{"
        raw start
        yield
        raw "}"
    else
        start += ";"
        raw start
    end
end

#comment(content = nil) ⇒ Object



67
68
69
# File 'lib/helpers/dsl.rb', line 67

def comment(content=nil)
    raw "// #{content}"
end

#created_at(*time) ⇒ Object

Metadata



37
38
39
# File 'lib/helpers/dsl.rb', line 37

def created_at(*time)
    @created_at = Date.parse(time.join('-'))
end

#define(type, content) ⇒ Object



101
102
103
# File 'lib/helpers/dsl.rb', line 101

def define(type, content)
    raw "#define #{type}  #{content}"
end

#define_str(type, content) ⇒ Object



105
106
107
# File 'lib/helpers/dsl.rb', line 105

def define_str(type, content)
    define type, "@\"#{content}\""
end

#enum(name, values, options = {}) ⇒ Object

Structure / Enum



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/helpers/dsl.rb', line 117

def enum(name, values, options = {})
    base = options[:base] || "NSInteger"
    raw "typedef NS_ENUM(#{base}, #{name}) {"
    if options[:bitmask]
        raw "  #{name}None = 0,"
        if values.kind_of? Hash
            values.keys.each_with_index do |val, idx|
                comment values[val].to_s
                raw "  #{name}#{val} = 1 << #{idx},"
            end
        else
            values.each_with_index do |val, idx|
                raw "  #{name}#{val} = 1 << #{idx},"
            end
        end
        raw "  #{name}All = 0b#{"1" * (values.count + 1)}"
    else
        if values.kind_of? Hash
            values.keys.each_with_index do |val, idx|
                comment values[val].to_s
                raw "  #{name}#{val},"
            end
        else
            values.each do |val|
                raw "  #{name}#{val},"
            end
        end
    end
    raw "};"
end

#file_header(project = nil, user = nil, copyright = nil) ⇒ Object

Comments



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/helpers/dsl.rb', line 48

def file_header(project=nil, user=nil, copyright=nil)
    if @created_at == nil
        created_at DateTime.now
    end
    if @hash == nil
        hash data
    end
    raw "//
//  #{File.basename(filename)}
//  #{project || "Nova"}
//
//  Created by #{user || "Nova" } on #{@created_at.strftime("%Y/%-m/%-d")}.
//  Copyright (c) #{@created_at.strftime("%Y")}#{copyright || user || "Nova"}. All rights reserved.
//
//  Hash: #{@hash}
//
//  WARNNING: Generated by Nova, Donnot edit this file directly.\n"
end

#hash(data) ⇒ Object



41
42
43
44
# File 'lib/helpers/dsl.rb', line 41

def hash(data)
    raise "Hash something" if data == nil
    @hash = Digest::MD5.hexdigest data.to_s
end

#implement(name) ⇒ Object



175
176
177
178
179
# File 'lib/helpers/dsl.rb', line 175

def implement(name)
    raw "@implementation #{name}"
    yield if block_given?
    raw "@end"
end

#import(*libs) ⇒ Object

Macros



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/helpers/dsl.rb', line 73

def import(*libs)
    libs.flatten.each do |lib|
        if SYSTEM_LIBS[lib.to_sym]
            lib = SYSTEM_LIBS[lib.to_sym]
            raw "#import <#{lib}>"
        else
            if lib.kind_of? String
                if lib.to_s.index(".h")
                    raw "#import \"#{lib}\""
                else
                    raw "#import \"#{lib}/#{lib}.h\""
                end
            else
                if lib.kind_of? Symbol
                    if lib.to_s.index(".h")
                        raw "#import <#{lib}>"
                    else
                        raw "#import <#{lib}/#{lib}.h>"
                    end
                else
                    raise "WTF you passed for import ? #{lib}"
                end
            end
        end
    end
    raw
end

#interface(name, superclass = "NSObject", protocols = []) ⇒ Object



161
162
163
164
165
166
# File 'lib/helpers/dsl.rb', line 161

def interface(name, superclass = "NSObject", protocols = [])
    ptc = if protocols.empty? then "" else "<#{protocols.join(',')}>" end
    raw "@interface #{name}#{ptc} : #{superclass}"
    yield if block_given?
    raw "@end"
end

#method(*options) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/helpers/dsl.rb', line 205

def method(*options)
    options = options.flatten
    raise "Method options.count < 2" unless options.count > 1
    return_type = options.shift
    start = "- (#{return_type})"
    if options.length == 1
        start += "#{options[0]}"
    else
        raise "Not valid method signature" unless options.count % 3 == 0
        options.permutation(3) do |a,b,c|
            start += "#{a}:(#{b})#{c} "
        end
    end
    if block_given?
        start += "{\n"
        raw start
        yield
        raw "}"
    else
        start += ";"
        raw start
    end
end

#mode(mode) ⇒ Object

Language Switching



21
22
23
# File 'lib/helpers/dsl.rb', line 21

def mode(mode)
    @mode = mode
end

#objcObject



25
26
27
28
29
# File 'lib/helpers/dsl.rb', line 25

def objc
    mode :objc
    yield if block_given?
    @str
end

#objc?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/helpers/dsl.rb', line 31

def objc?
    @mode == nil or @mode == :objc
end

#property(name, type, options = {}) ⇒ Object



168
169
170
171
172
173
# File 'lib/helpers/dsl.rb', line 168

def property(name, type, options = {})
    atomic_str = options[:atomic] ? "atomic" : "nonatomic"
    policy_str = options[:policy] || :strong
    star_str   = (policy_str.to_s == "assign") ? "" : "*"
    raw "@property (#{atomic_str}, #{policy_str}) #{type} #{star_str} #{name};"
end

#protocol(name, superclass = nil) ⇒ Object



154
155
156
157
158
159
# File 'lib/helpers/dsl.rb', line 154

def protocol(name, superclass = nil)
    super_str = if superclass then "<#{superclass}>" else "" end
    raw "@protocol #{name}#{super_str}"
    yield if block_given?
    raw "@end"
end

#raw(content = "") ⇒ Object

Basic



12
13
14
15
16
17
# File 'lib/helpers/dsl.rb', line 12

def raw(content = "")
    @str ||= ""
    content += "\n"
    @str += content
    content
end

#typedef(src, des) ⇒ Object

Type



111
112
113
# File 'lib/helpers/dsl.rb', line 111

def typedef(src, des)
    raw "typedef #{src} #{des};"
end