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/classes.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)
Param =
Class.new Struct.new(:name, :value)

Constants included from PhoneGap

PhoneGap::PHONEGAP_APIS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TemplateHelper

generate_and_write

Methods included from PhoneGap

#add_stock_phonegap_apis, #legacy_plugins, #phonegap_version, #phonegap_version=

Methods included from Helpers

#is_file?

Constructor Details

#initialize(*args) ⇒ Config

Returns a new instance of Config.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/confetti/config.rb', line 29

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
  @plugin_set       = TypedSet.new Plugin # defined in PhoneGap module
  @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



18
19
20
21
22
23
24
25
26
27
# File 'lib/confetti/config.rb', line 18

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.



9
10
11
# File 'lib/confetti/config.rb', line 9

def access_set
  @access_set
end

#authorObject (readonly)

Returns the value of attribute author.



9
10
11
# File 'lib/confetti/config.rb', line 9

def author
  @author
end

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/confetti/config.rb', line 9

def content
  @content
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/confetti/config.rb', line 7

def description
  @description
end

#feature_setObject (readonly)

Returns the value of attribute feature_set.



9
10
11
# File 'lib/confetti/config.rb', line 9

def feature_set
  @feature_set
end

#heightObject

Returns the value of attribute height.



7
8
9
# File 'lib/confetti/config.rb', line 7

def height
  @height
end

#icon_setObject (readonly)

Returns the value of attribute icon_set.



9
10
11
# File 'lib/confetti/config.rb', line 9

def icon_set
  @icon_set
end

#licenseObject (readonly)

Returns the value of attribute license.



9
10
11
# File 'lib/confetti/config.rb', line 9

def license
  @license
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/confetti/config.rb', line 9

def name
  @name
end

#packageObject

Returns the value of attribute package.



7
8
9
# File 'lib/confetti/config.rb', line 7

def package
  @package
end

#plist_icon_setObject

Returns the value of attribute plist_icon_set.



7
8
9
# File 'lib/confetti/config.rb', line 7

def plist_icon_set
  @plist_icon_set
end

#plugin_setObject (readonly)

Returns the value of attribute plugin_set.



9
10
11
# File 'lib/confetti/config.rb', line 9

def plugin_set
  @plugin_set
end

#preference_setObject (readonly)

Returns the value of attribute preference_set.



9
10
11
# File 'lib/confetti/config.rb', line 9

def preference_set
  @preference_set
end

#splash_setObject (readonly)

Returns the value of attribute splash_set.



9
10
11
# File 'lib/confetti/config.rb', line 9

def splash_set
  @splash_set
end

#version_codeObject

Returns the value of attribute version_code.



7
8
9
# File 'lib/confetti/config.rb', line 7

def version_code
  @version_code
end

#version_stringObject

Returns the value of attribute version_string.



7
8
9
# File 'lib/confetti/config.rb', line 7

def version_string
  @version_string
end

#viewmodesObject (readonly)

Returns the value of attribute viewmodes.



9
10
11
# File 'lib/confetti/config.rb', line 9

def viewmodes
  @viewmodes
end

#widthObject

Returns the value of attribute width.



7
8
9
# File 'lib/confetti/config.rb', line 7

def width
  @width
end

#xml_docObject (readonly)

Returns the value of attribute xml_doc.



9
10
11
# File 'lib/confetti/config.rb', line 9

def xml_doc
  @xml_doc
end

Instance Method Details

#biggest_iconObject



122
123
124
# File 'lib/confetti/config.rb', line 122

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



224
225
226
227
228
229
230
# File 'lib/confetti/config.rb', line 224

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

#default_iconObject



196
197
198
199
200
201
# File 'lib/confetti/config.rb', line 196

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



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

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



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

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

#filter_images(images, filter) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/confetti/config.rb', line 210

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/confetti/config.rb', line 166

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)


162
163
164
# File 'lib/confetti/config.rb', line 162

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

#iconObject



118
119
120
# File 'lib/confetti/config.rb', line 118

def icon
  @icon_set.first
end

#orientationObject

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



132
133
134
135
136
137
138
139
140
141
# File 'lib/confetti/config.rb', line 132

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/confetti/config.rb', line 48

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)
      when "plugin"
        next if attr["name"].nil? or attr["name"].empty?
        plugin = Plugin.new(attr["name"], attr["version"])
        ele.each_element('param') do |param|
          p_attr = param.attributes
          plugin.param_set << Param.new(p_attr["name"], p_attr["value"])
        end
        @plugin_set << plugin
      end
    end
  end
end

#preference(name) ⇒ Object

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



145
146
147
148
149
# File 'lib/confetti/config.rb', line 145

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



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

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

#splashObject



126
127
128
# File 'lib/confetti/config.rb', line 126

def splash
  @splash_set.first
end