Class: Protopack::PackageItem

Inherits:
Aduki::Initializable
  • Object
show all
Defined in:
lib/protopack/package_item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/protopack/package_item.rb', line 8

def attributes
  @attributes
end

#default_localeObject

Returns the value of attribute default_locale.



5
6
7
# File 'lib/protopack/package_item.rb', line 5

def default_locale
  @default_locale
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/protopack/package_item.rb', line 3

def description
  @description
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/protopack/package_item.rb', line 2

def id
  @id
end

#localeObject

Returns the value of attribute locale.



4
5
6
# File 'lib/protopack/package_item.rb', line 4

def locale
  @locale
end

#ordinalObject

Returns the value of attribute ordinal.



7
8
9
# File 'lib/protopack/package_item.rb', line 7

def ordinal
  @ordinal
end

#protopack_filenameObject

Returns the value of attribute protopack_filename.



10
11
12
# File 'lib/protopack/package_item.rb', line 10

def protopack_filename
  @protopack_filename
end

#resourcesObject

Returns the value of attribute resources.



9
10
11
# File 'lib/protopack/package_item.rb', line 9

def resources
  @resources
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/protopack/package_item.rb', line 6

def type
  @type
end

Class Method Details

.load(filename) ⇒ Object



48
49
50
51
52
# File 'lib/protopack/package_item.rb', line 48

def self.load filename
  Protopack::PackageItem.new(YAML.load(File.read(filename)).merge(protopack_filename: filename))
rescue
  raise "error reading from file #{filename}"
end

Instance Method Details

#apply!Object



39
40
41
42
43
44
45
46
# File 'lib/protopack/package_item.rb', line 39

def apply!
  factory = existence
  if factory.empty?
    factory.create! load_resources(attributes.to_hash)
  else
    factory.first.update_attributes attributes.to_hash
  end
end

#existenceObject



28
29
30
# File 'lib/protopack/package_item.rb', line 28

def existence
  target_class.existence attributes
end

#load_resources(hsh, d = File.dirname(protopack_filename)) ⇒ Object



32
33
34
35
36
37
# File 'lib/protopack/package_item.rb', line 32

def load_resources hsh, d = File.dirname(protopack_filename)
  (resources || {}).inject(hsh) { |hsh, (k, v)|
    hsh[k] = File.read(File.join d, v)
    hsh
  }
end

#lookup_class(base, list) ⇒ Object



14
15
16
17
18
# File 'lib/protopack/package_item.rb', line 14

def lookup_class base, list
  base = base.const_get list.shift
  return base if list.empty?
  lookup_class base, list
end

#missing?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/protopack/package_item.rb', line 24

def missing?
  existence.empty?
end

#nameObject



12
# File 'lib/protopack/package_item.rb', line 12

def name ; attributes[:name] || attributes["name"] ; end

#target_classObject



20
21
22
# File 'lib/protopack/package_item.rb', line 20

def target_class
  lookup_class Kernel, type.split("::")
end