protopack 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78a62b7195e16bf1e652f062893e09fa386c3cdd
4
- data.tar.gz: 387c89d2bd400de745cd6c740f5527c7db73845f
3
+ metadata.gz: 0cb317f6ebff18ddcfef2fa9f2154020e5fb2e22
4
+ data.tar.gz: d8ee7909c86427a0d10d23eea8dce5e34a5d9667
5
5
  SHA512:
6
- metadata.gz: 22c529ff5dda2505ec3dc2da67de63d0a3786c1542f69ffb6897628cf116785130759e447a8eccb62bed1a7d13101ba68a26e1c2062278ee74c80638720e80d0
7
- data.tar.gz: 30411c3d7f5e79b69330bfc4271cb9113d32450feefc494fcd29f6cf31528f9ab188afd233b15ef2cc2ddc45cd99d141a724b735c2a2c34c338ccb067eb411bd
6
+ metadata.gz: 69d583f898589b222eda2d0f0f9795ec648a107681a491e970d64419e69c365f65990ea106421c0a592934c3cc0da2ce9618c86b0298de1cac3660bbe7bce466
7
+ data.tar.gz: 1abea23055c7ea068b1bc4caea24c6255594625fb9ffa3e8c575f75b9e483b80dceede994383f33abfa6a53eaa3baf77ad9ac7c7c8b78b4f55c83b5e10d2358a
@@ -1,5 +1,5 @@
1
1
  require "yaml"
2
- require "hashie"
2
+ require "aduki"
3
3
  require "protopack/version"
4
4
  require "protopack/package"
5
5
  require "protopack/package_item"
@@ -15,14 +15,22 @@ module Protopack
15
15
 
16
16
  def build_association_table obj
17
17
  export_config(obj)[:associations].inject({ }) { |table, name|
18
- assoc = obj.send name
19
- next table if assoc.blank? || assoc.empty?
20
- attrs = (assoc.respond_to?(:each)) ? array_assoc(assoc) : to_attributes(assoc)
21
- table["#{name}_attributes"] = attrs
18
+ if name.is_a? Hash
19
+ build_association obj, table, name[:get], name[:set]
20
+ else
21
+ build_association obj, table, name, "#{name}_attributes"
22
+ end
22
23
  table
23
24
  }
24
25
  end
25
26
 
27
+ def build_association obj, table, getter, setter
28
+ assoc = obj.send getter
29
+ return if assoc.blank? || assoc.empty?
30
+ attrs = (assoc.respond_to?(:each)) ? array_assoc(assoc) : to_attributes(assoc)
31
+ table[setter] = attrs
32
+ end
33
+
26
34
  def to_package obj, meta={ }
27
35
  obj_name = obj.send(name_method obj)
28
36
  obj_id = obj_name.to_s.gsub(/_/, '-')
@@ -1,16 +1,14 @@
1
- class Protopack::Package
2
- attr_reader :config
3
-
4
- def method_missing m, *args
5
- config.send m, *args
6
- end
7
-
8
- def initialize cfg
9
- @config = (cfg.is_a?(Hashie::Mash) ? cfg : Hashie::Mash.new(cfg))
10
- end
1
+ class Protopack::Package < Aduki::Initializable
2
+ attr_accessor :name
3
+ attr_accessor :title
4
+ attr_accessor :description
5
+ attr_accessor :authors
6
+ attr_accessor :item_files
7
+ attr_accessor :root
8
+ aduki updated: Date
11
9
 
12
10
  def items
13
- config.items.map { |item_file| Protopack::PackageItem.load(item_file) }
11
+ item_files.map { |item_file| Protopack::PackageItem.load(item_file) }
14
12
  end
15
13
 
16
14
  def item id
@@ -48,9 +46,9 @@ class Protopack::Package
48
46
 
49
47
  def self.all
50
48
  Dir.glob("#{config_root}/*/package-config.yml").map { |pkg_cfg|
51
- cfg = Hashie::Mash.new(YAML.load(File.read(pkg_cfg)))
49
+ cfg = YAML.load(File.read(pkg_cfg))
52
50
  root = File.dirname pkg_cfg
53
- cfg["items"] = Dir.glob("#{root}/*item*.yml")
51
+ cfg["item_files"] = Dir.glob("#{root}/*item*.yml")
54
52
  cfg["root"] = root
55
53
  new cfg
56
54
  }
@@ -58,8 +56,8 @@ class Protopack::Package
58
56
 
59
57
  def self.find name
60
58
  root = "#{config_root}/#{name}"
61
- cfg = Hashie::Mash.new(YAML.load(File.read("#{root}/package-config.yml")))
62
- cfg["items"] = Dir.glob("#{root}/*item*.yml")
59
+ cfg = YAML.load(File.read("#{root}/package-config.yml"))
60
+ cfg["item_files"] = Dir.glob("#{root}/*item*.yml")
63
61
  cfg["root"] = root
64
62
  new cfg
65
63
  end
@@ -1,17 +1,15 @@
1
- class Protopack::PackageItem
2
- attr_reader :config
3
-
4
- def method_missing m, *args
5
- config.send m, *args
6
- end
7
-
8
- def initialize cfg
9
- @config = (cfg.is_a?(Hashie::Mash) ? cfg : Hashie::Mash.new(cfg))
10
- end
11
-
12
- def name
13
- attributes.name
14
- end
1
+ class Protopack::PackageItem < Aduki::Initializable
2
+ attr_accessor :id
3
+ attr_accessor :description
4
+ attr_accessor :locale
5
+ attr_accessor :default_locale
6
+ attr_accessor :type
7
+ attr_accessor :ordinal
8
+ attr_accessor :attributes
9
+ attr_accessor :resources
10
+ attr_accessor :protopack_filename
11
+
12
+ def name ; attributes[:name] || attributes["name"] ; end
15
13
 
16
14
  def lookup_class base, list
17
15
  base = base.const_get list.shift
@@ -31,17 +29,24 @@ class Protopack::PackageItem
31
29
  target_class.existence attributes
32
30
  end
33
31
 
32
+ def load_resources hsh, d = File.dirname(protopack_filename)
33
+ (resources || {}).inject(hsh) { |hsh, (k, v)|
34
+ hsh[k] = File.read(File.join d, v)
35
+ hsh
36
+ }
37
+ end
38
+
34
39
  def apply!
35
40
  factory = existence
36
41
  if factory.empty?
37
- factory.create! attributes.to_hash
42
+ factory.create! load_resources(attributes.to_hash)
38
43
  else
39
44
  factory.first.update_attributes attributes.to_hash
40
45
  end
41
46
  end
42
47
 
43
48
  def self.load filename
44
- Protopack::PackageItem.new(Hashie::Mash.new(YAML.load(File.read(filename))))
49
+ Protopack::PackageItem.new(YAML.load(File.read(filename)).merge(protopack_filename: filename))
45
50
  rescue
46
51
  raise "error reading from file #{filename}"
47
52
  end
@@ -1,3 +1,3 @@
1
1
  module Protopack
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -15,8 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.summary = %q{ Store packages of object prototypes on-disk as YML; this gem allows you scan each package for missing items and apply them to your repository. }
16
16
  gem.homepage = "https://github.com/conanite/protopack"
17
17
 
18
- gem.add_dependency 'hashie'
19
- gem.add_dependency 'aduki', '~> 0.2.6', '>= 0.2.6'
18
+ gem.add_dependency 'aduki', '>= 0.2.7'
20
19
  gem.add_development_dependency 'rspec'
21
20
 
22
21
  gem.files = `git ls-files`.split($/)
@@ -1,3 +1,29 @@
1
+ require 'aduki'
2
+
3
+ class Object
4
+ def blank?
5
+ false
6
+ end
7
+ end
8
+
9
+ class String
10
+ def blank?
11
+ strip == ''
12
+ end
13
+ end
14
+
15
+ class NilClass
16
+ def blank?
17
+ true
18
+ end
19
+ end
20
+
21
+ class Hash
22
+ def deep_merge other
23
+ merge other
24
+ end
25
+ end
26
+
1
27
  class Repository
2
28
  def initialize klass, name
3
29
  @klass, @name = klass, name
@@ -20,9 +46,11 @@ class Repository
20
46
  end
21
47
  end
22
48
 
23
- class Widget
49
+ class Widget < Aduki::Initializable
24
50
  @@widgets = []
25
51
 
52
+ attr_accessor :wots, :colour, :height, :density, :name, :description
53
+
26
54
  def self.all
27
55
  @@widgets
28
56
  end
@@ -31,28 +59,41 @@ class Widget
31
59
  @@widgets = []
32
60
  end
33
61
 
34
- def initialize attrs
35
- @attrs = attrs.is_a?(Hash) ? Hashie::Mash.new(attrs) : attrs
62
+ def aduki_after_initialize
36
63
  @@widgets << self
37
64
  end
38
65
 
39
- def method_missing m, *args
40
- @attrs[m.to_s]
66
+ def protopack_export_config
67
+ { fields: [:colour, :height, :density, :name ], associations: [{ get: :mywots, set: :newwots }]}
68
+ end
69
+
70
+ def slice *names
71
+ Hash[*(names.zip(names.map { |n| send n }).flatten)]
72
+ end
73
+
74
+ def mywots
75
+ self.wots
76
+ end
77
+
78
+ def newwots= wots_attrs
79
+ self.wots = wots_attrs.map { |a| Wot::Zit.new a }
41
80
  end
42
81
 
43
82
  def update_attributes attrs
44
- @attrs = attrs
83
+ aduki_apply_attributes attrs
45
84
  end
46
85
 
47
86
  def self.existence attrs
48
- Repository.new Widget, attrs.colour
87
+ Repository.new Widget, attrs["colour"]
49
88
  end
50
89
  end
51
90
 
52
91
  module Wot
53
- class Zit
92
+ class Zit < Aduki::Initializable
54
93
  @@wotzits = []
55
94
 
95
+ attr_accessor :colour, :height, :density, :name
96
+
56
97
  def self.all
57
98
  @@wotzits
58
99
  end
@@ -61,21 +102,24 @@ module Wot
61
102
  @@wotzits = []
62
103
  end
63
104
 
64
- def initialize attrs
65
- @attrs = attrs.is_a?(Hash) ? Hashie::Mash.new(attrs) : attrs
105
+ def aduki_after_initialize
66
106
  @@wotzits << self
67
107
  end
68
108
 
69
- def method_missing m, *args
70
- @attrs.send m, *args
109
+ def protopack_export_config
110
+ { fields: [:colour, :height, :density, :name ], associations: [] }
111
+ end
112
+
113
+ def slice *names
114
+ Hash[*(names.zip(names.map { |n| send n }).flatten)]
71
115
  end
72
116
 
73
117
  def update_attributes attrs
74
- @attrs = attrs
118
+ aduki_apply_attributes attrs
75
119
  end
76
120
 
77
121
  def self.existence attrs
78
- Repository.new Wot::Zit, attrs.colour
122
+ Repository.new Wot::Zit, attrs["colour"]
79
123
  end
80
124
  end
81
125
  end
@@ -0,0 +1,40 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.expand_path('../../spec_helper', __FILE__)
4
+
5
+ # from aduki
6
+ require 'core_ext/array'
7
+ require 'core_ext/hash'
8
+
9
+ describe Protopack::Package do
10
+
11
+ it "exports a simple widget" do
12
+ wz0 = { colour: 41, height: 42, density: 43 }
13
+ wz1 = { colour: 51, height: 52, density: 53 }
14
+ w = Widget.new colour: 1, height: 2, density: 3, name: "Park Lodge Café", newwots: [ wz0, wz1 ]
15
+
16
+ exporter = Protopack::Exporter.new
17
+ hsh = exporter.to_package(w)
18
+ expected = <<YML.strip
19
+ ---
20
+ id: Park Lodge Café
21
+ description: Park Lodge Café
22
+ type: Widget
23
+ attributes:
24
+ colour: 1
25
+ height: 2
26
+ density: 3
27
+ name: Park Lodge Café
28
+ newwots:
29
+ - colour: 41
30
+ height: 42
31
+ density: 43
32
+ - colour: 51
33
+ height: 52
34
+ density: 53
35
+ YML
36
+
37
+ expect(exporter.clean_yaml(hsh).strip).to eq expected
38
+ end
39
+
40
+ end
@@ -4,7 +4,12 @@ require File.expand_path('../../spec_helper', __FILE__)
4
4
 
5
5
  describe Protopack::Package do
6
6
 
7
+ module HasRegion
8
+ attr_accessor :region
9
+ end
10
+
7
11
  before {
12
+ Protopack::PackageItem.send :include, HasRegion
8
13
  Protopack::Package.config_root = File.expand_path('../packages', __FILE__)
9
14
  Widget.destroy_all
10
15
  Wot::Zit.destroy_all
@@ -18,11 +23,11 @@ describe Protopack::Package do
18
23
  p = Protopack::Package.find("standard-widgets")
19
24
  expect(p.name).to eq "standard-widgets"
20
25
 
21
- expect(p.title.en).to eq "Standard Widgets"
22
- expect(p.title.fr).to eq "Widgets standards"
26
+ expect(p.title["en"]).to eq "Standard Widgets"
27
+ expect(p.title["fr"]).to eq "Widgets standards"
23
28
 
24
- expect(p.description.en).to eq "Use these widgets for everyday widgeting"
25
- expect(p.description.fr).to eq "Ces widgets sont utilisables pour votre widgeting quotidien"
29
+ expect(p.description["en"]).to eq "Use these widgets for everyday widgeting"
30
+ expect(p.description["fr"]).to eq "Ces widgets sont utilisables pour votre widgeting quotidien"
26
31
 
27
32
  expect(p.authors).to eq %w{ baz titi Z }
28
33
 
@@ -49,6 +54,14 @@ describe Protopack::Package do
49
54
  expect(Widget.all[2].height).to eq 'tiger'
50
55
  expect(Widget.all[3].height).to eq 'hyena'
51
56
  expect(Widget.all[4].height).to eq 'camel'
57
+
58
+ green = Widget.all[1]
59
+ expect(green.name).to eq "The green widget for obtuse African zebras"
60
+
61
+ black = Widget.all[4]
62
+ black_desc = "<html><p>this is what a black widget looks like</p></html>"
63
+ expect(black.name).to be_nil
64
+ expect(black.description).to eq black_desc
52
65
  end
53
66
 
54
67
  it "should install all items from a package subject to filtering" do
@@ -0,0 +1 @@
1
+ <html><p>this is what a black widget looks like</p></html>
@@ -5,3 +5,5 @@ attributes:
5
5
  colour: black
6
6
  height: camel
7
7
  density: rhetorical
8
+ resources:
9
+ description: black-description.html
@@ -2,6 +2,7 @@ id: green-widget
2
2
  type: Widget
3
3
  region: Africa
4
4
  attributes:
5
+ name: The green widget for obtuse African zebras
5
6
  colour: green
6
7
  height: zebra
7
8
  density: obtuse
@@ -11,4 +11,3 @@ authors:
11
11
  - titi
12
12
  - Z
13
13
  updated: 2013-03-15
14
-
metadata CHANGED
@@ -1,49 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protopack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Conan Dalton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-31 00:00:00.000000000 Z
11
+ date: 2020-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: hashie
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: aduki
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: 0.2.6
34
17
  - - ">="
35
18
  - !ruby/object:Gem::Version
36
- version: 0.2.6
19
+ version: 0.2.7
37
20
  type: :runtime
38
21
  prerelease: false
39
22
  version_requirements: !ruby/object:Gem::Requirement
40
23
  requirements:
41
- - - "~>"
42
- - !ruby/object:Gem::Version
43
- version: 0.2.6
44
24
  - - ">="
45
25
  - !ruby/object:Gem::Version
46
- version: 0.2.6
26
+ version: 0.2.7
47
27
  - !ruby/object:Gem::Dependency
48
28
  name: rspec
49
29
  requirement: !ruby/object:Gem::Requirement
@@ -80,10 +60,12 @@ files:
80
60
  - lib/protopack/version.rb
81
61
  - protopack.gemspec
82
62
  - spec/models.rb
63
+ - spec/protopack/exporter_spec.rb
83
64
  - spec/protopack/package_spec.rb
84
65
  - spec/protopack/packages/advanced-widgets/lavender-widget-item.yml
85
66
  - spec/protopack/packages/advanced-widgets/magenta-widget-item.yml
86
67
  - spec/protopack/packages/advanced-widgets/package-config.yml
68
+ - spec/protopack/packages/standard-widgets/black-description.html
87
69
  - spec/protopack/packages/standard-widgets/black-widget-item.yml
88
70
  - spec/protopack/packages/standard-widgets/blue-widget-item.yml
89
71
  - spec/protopack/packages/standard-widgets/green-widget-item.yml
@@ -118,10 +100,12 @@ summary: Store packages of object prototypes on-disk as YML; this gem allows you
118
100
  each package for missing items and apply them to your repository.
119
101
  test_files:
120
102
  - spec/models.rb
103
+ - spec/protopack/exporter_spec.rb
121
104
  - spec/protopack/package_spec.rb
122
105
  - spec/protopack/packages/advanced-widgets/lavender-widget-item.yml
123
106
  - spec/protopack/packages/advanced-widgets/magenta-widget-item.yml
124
107
  - spec/protopack/packages/advanced-widgets/package-config.yml
108
+ - spec/protopack/packages/standard-widgets/black-description.html
125
109
  - spec/protopack/packages/standard-widgets/black-widget-item.yml
126
110
  - spec/protopack/packages/standard-widgets/blue-widget-item.yml
127
111
  - spec/protopack/packages/standard-widgets/green-widget-item.yml