Class: Confetti::Config

Inherits:
Object
  • Object
show all
Extended by:
TemplateHelper
Includes:
Helpers, PhoneGap
Defined in:
lib/confetti/config.rb,
lib/confetti/config/image.rb,
lib/confetti/config/feature.rb

Defined Under Namespace

Classes: Feature, FileError, FiletypeError, Image, XMLError

Constant Summary collapse

Author =

classes that represent child elements

Class.new Struct.new(:name, :href, :email)
Name =
Class.new Struct.new(:name, :shortname)
License =
Class.new Struct.new(:text, :href)
Content =
Class.new Struct.new(:src, :type, :encoding)
Preference =
Class.new Struct.new(:name, :value, :readonly)
Access =
Class.new Struct.new(:origin, :subdomains, :browserOnly)

Constants included from PhoneGap

PhoneGap::PHONEGAP_APIS, PhoneGap::Plugin

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TemplateHelper

generate_and_write

Methods included from PhoneGap

#add_stock_phonegap_apis, #phonegap_version, #phonegap_version=, #plugins

Methods included from Helpers

#is_file?

Constructor Details

#initialize(*args) ⇒ Config

Returns a new instance of Config.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/confetti/config.rb', line 44

def initialize(*args)
  @author           = Author.new
  @name             = Name.new
  @license          = License.new
  @content          = Content.new
  @icon_set         = TypedSet.new Image
  @plist_icon_set   = [] 
  @feature_set      = TypedSet.new Feature
  @splash_set       = TypedSet.new Image
  @preference_set   = TypedSet.new Preference
  @access_set       = TypedSet.new Access
  @viewmodes        = []

  if args.length > 0 && is_file?(args.first)
    populate_from_xml args.first
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

handle bad generate/write calls



24
25
26
27
28
29
30
31
32
33
# File 'lib/confetti/config.rb', line 24

def method_missing(method_name, *args)
  bad_call = /^(generate)|(write)_(.*)$/
  matches = method_name.to_s.match(bad_call)

  if matches
    raise FiletypeError, "#{ matches[3] } not supported"
  else
    super method_name, *args
  end
end

Instance Attribute Details

#access_setObject (readonly)

Returns the value of attribute access_set.



15
16
17
# File 'lib/confetti/config.rb', line 15

def access_set
  @access_set
end

#authorObject (readonly)

Returns the value of attribute author.



15
16
17
# File 'lib/confetti/config.rb', line 15

def author
  @author
end

#contentObject (readonly)

Returns the value of attribute content.



15
16
17
# File 'lib/confetti/config.rb', line 15

def content
  @content
end

#descriptionObject

Returns the value of attribute description.



13
14
15
# File 'lib/confetti/config.rb', line 13

def description
  @description
end

#feature_setObject (readonly)

Returns the value of attribute feature_set.



15
16
17
# File 'lib/confetti/config.rb', line 15

def feature_set
  @feature_set
end

#heightObject

Returns the value of attribute height.



13
14
15
# File 'lib/confetti/config.rb', line 13

def height
  @height
end

#icon_setObject (readonly)

Returns the value of attribute icon_set.



15
16
17
# File 'lib/confetti/config.rb', line 15

def icon_set
  @icon_set
end

#licenseObject (readonly)

Returns the value of attribute license.



15
16
17
# File 'lib/confetti/config.rb', line 15

def license
  @license
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/confetti/config.rb', line 15

def name
  @name
end

#packageObject

Returns the value of attribute package.



13
14
15
# File 'lib/confetti/config.rb', line 13

def package
  @package
end

#plist_icon_setObject

Returns the value of attribute plist_icon_set.



13
14
15
# File 'lib/confetti/config.rb', line 13

def plist_icon_set
  @plist_icon_set
end

#preference_setObject (readonly)

Returns the value of attribute preference_set.



15
16
17
# File 'lib/confetti/config.rb', line 15

def preference_set
  @preference_set
end

#splash_setObject (readonly)

Returns the value of attribute splash_set.



15
16
17
# File 'lib/confetti/config.rb', line 15

def splash_set
  @splash_set
end

#version_codeObject

Returns the value of attribute version_code.



13
14
15
# File 'lib/confetti/config.rb', line 13

def version_code
  @version_code
end

#version_stringObject

Returns the value of attribute version_string.



13
14
15
# File 'lib/confetti/config.rb', line 13

def version_string
  @version_string
end

#viewmodesObject (readonly)

Returns the value of attribute viewmodes.



15
16
17
# File 'lib/confetti/config.rb', line 15

def viewmodes
  @viewmodes
end

#widthObject

Returns the value of attribute width.



13
14
15
# File 'lib/confetti/config.rb', line 13

def width
  @width
end

#xml_docObject (readonly)

Returns the value of attribute xml_doc.



15
16
17
# File 'lib/confetti/config.rb', line 15

def xml_doc
  @xml_doc
end

Instance Method Details

#biggest_iconObject



128
129
130
# File 'lib/confetti/config.rb', line 128

def biggest_icon
  @icon_set.max { |a,b| a.width.to_i <=> b.width.to_i }
end

#boolean_value(value, default = false) ⇒ Object

convert string (attribute) to boolean



230
231
232
233
234
235
236
# File 'lib/confetti/config.rb', line 230

def boolean_value value, default=false
  if default
    value != "false"
  else
    value == "true"
  end
end

#default_iconObject



202
203
204
205
206
207
# File 'lib/confetti/config.rb', line 202

def default_icon
  @icon_set.each do |icon|
    return icon if icon.src.match /^icon(\.[a-zA-Z0-9]+)+$/i
  end
  nil
end

#default_splashObject



209
210
211
212
213
214
# File 'lib/confetti/config.rb', line 209

def default_splash
  @splash_set.each do |splash|
    return splash if splash.src.match /^splash(\.[a-zA-Z0-9]+)$/i
  end
  nil
end

#feature(name) ⇒ Object



164
165
166
# File 'lib/confetti/config.rb', line 164

def feature name
  @feature_set.detect { |feature| feature.name == name }
end

#filter_images(images, filter) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/confetti/config.rb', line 216

def filter_images images, filter
  imgs = images.clone

  # filter can have multiple criteria
  filter.each_pair do |name, value|
    imgs = imgs.reject do |img|
      !img.respond_to?(name) || img.send(name) != value
    end
  end

  imgs
end

#find_best_fit_img(images, opts = {}) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/confetti/config.rb', line 172

def find_best_fit_img images, opts = {}
  opts['width']     ||= nil
  opts['height']    ||= nil
  opts['role']      ||= nil
  opts['density']      ||= nil
  opts['state']      ||= nil
  opts['platform']  ||= nil

  # filters to look through sets for
  filters = [
    {'height' => opts['height'], 'width' => opts['width']},
    {'platform' => opts['platform'], 'density' => opts['density']},
    {'platform' => opts['platform'], 'state' => opts['state']},
    {'platform' => opts['platform'], 'role' => opts['role']},
    {'platform' => opts['platform']}
  ]

  matches = nil

  filters.each do |filter|
    matches = filter_images(images, filter)

    if matches.length == 1
      break
    end
  end

  matches.first unless matches.empty?
end

#full_access?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/confetti/config.rb', line 168

def full_access?
  @access_set.detect { |a| a.origin == '*' }
end

#iconObject



124
125
126
# File 'lib/confetti/config.rb', line 124

def icon
  @icon_set.first
end

#orientationObject

simple helper for grabbing chosen orientation, or the default returns one of :portrait, :landscape, or :default



138
139
140
141
142
143
144
145
146
147
# File 'lib/confetti/config.rb', line 138

def orientation
  values = [:portrait, :landscape, :default]
  choice = preference :orientation

  unless choice and values.include?(choice)
    :default
  else
    choice
  end
end

#populate_from_xml(xml_file) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/confetti/config.rb', line 62

def populate_from_xml(xml_file)
  begin
    file = File.read(xml_file)
    config_doc = REXML::Document.new(file).root
  rescue REXML::ParseException
    raise XMLError, "malformed config.xml"
  rescue Errno::ENOENT
    raise FileError, "file #{ xml_file } doesn't exist"
  end

  if config_doc.nil?
    raise XMLError, "no doc parsed"
  end

  # save reference to xml doc
  @xml_doc = config_doc

  @package = config_doc.attributes["id"]
  @version_string = config_doc.attributes["version"]
  @version_code = config_doc.attributes["versionCode"]

  config_doc.elements.each do |ele|
    attr = ele.attributes

    case ele.namespace

    # W3C widget elements
    when "http://www.w3.org/ns/widgets"
      case ele.name
      when "name"
        @name = Name.new(ele.text.nil? ? "" : ele.text.strip, attr["shortname"])
      when "author"
        @author = Author.new(ele.text.nil? ? "" : ele.text.strip, attr["href"], attr["email"])
      when "description"
        @description = ele.text.nil? ? "" : ele.text.strip
      when "icon"
        @icon_set << Image.new(attr["src"], attr["height"], attr["width"], attr)
        # used for the info.plist file
        @plist_icon_set << attr["src"]
      when "feature"
        @feature_set  << Feature.new(attr["name"], attr["required"])
      when "preference"
        @preference_set << Preference.new(attr["name"], attr["value"], attr["readonly"])
      when "license"
        @license = License.new(ele.text.nil? ? "" : ele.text.strip, attr["href"])
      when "access"
        sub = boolean_value(attr["subdomains"], true)
        browserOnly = boolean_value(attr["browserOnly"])
        @access_set << Access.new(attr["origin"], sub, browserOnly)
      end

    # PhoneGap extensions (gap:)
    when "http://phonegap.com/ns/1.0"
      case ele.name
      when "splash"
        next if attr["src"].nil? or attr["src"].empty?
        @splash_set << Image.new(attr["src"], attr["height"], attr["width"], attr)
      end
    end
  end
end

#preference(name) ⇒ Object

helper to retrieve a preference’s value returns nil if the preference doesn’t exist



151
152
153
154
155
# File 'lib/confetti/config.rb', line 151

def preference name
  pref = preference_obj(name)

  pref && pref.value && pref.value.to_sym
end

#preference_obj(name) ⇒ Object

mostly an internal method to help with weird cases in particular #phonegap_version



159
160
161
162
# File 'lib/confetti/config.rb', line 159

def preference_obj name
  name = name.to_s
  @preference_set.detect { |pref| pref.name == name }
end

#splashObject



132
133
134
# File 'lib/confetti/config.rb', line 132

def splash
  @splash_set.first
end