onoma 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Rakefile +11 -9
  4. data/bin/console +3 -3
  5. data/config/locales/arb.yml +6455 -4930
  6. data/config/locales/cmn.yml +7227 -0
  7. data/config/locales/deu.yml +7227 -0
  8. data/config/locales/eng.yml +2073 -534
  9. data/config/locales/fra.yml +2093 -452
  10. data/config/locales/ita.yml +7227 -0
  11. data/config/locales/jpn.yml +6412 -4887
  12. data/config/locales/por.yml +6261 -4736
  13. data/config/locales/spa.yml +6076 -4551
  14. data/db/migrate/20151211114316_add_beta_varieties.xml +1 -1
  15. data/db/migrate/20151211124757_add_allium_schoenoprasum_varieties.xml +2 -2
  16. data/db/migrate/20151211153218_add_zea_mays_varieties.xml +1 -1
  17. data/db/migrate/20151216100708_add_lifter_equipment_variants.xml +1 -1
  18. data/db/migrate/20151223164501_update_organic_fertilizer.xml +4 -0
  19. data/db/migrate/20151223171801_update_indicators.xml +4 -0
  20. data/db/migrate/20160104145418_add_matters.xml +14 -0
  21. data/db/migrate/20160104160843_change_product_nature_variants_drain.xml +4 -0
  22. data/db/migrate/20160105095120_add_plugin_cap_varieties.xml +261 -0
  23. data/db/migrate/20160106095232_add_orchard_plugin_cap_varieties.xml +405 -0
  24. data/db/migrate/20160108142009_add_product_nature_variants_nematicide.xml +6 -0
  25. data/db/migrate/20160113144856_update_prunus_avium_parent.xml +32 -0
  26. data/db/migrate/20160115090243_change_plant_varieties.xml +7 -0
  27. data/db/migrate/20160119115401_add_shipping_note_document.xml +4 -0
  28. data/db/migrate/20160128093149_fixes_invalid_parent.xml +102 -0
  29. data/db/migrate/20160128154202_add_product_natures_protective_canvas.xml +6 -0
  30. data/db/migrate/20160204115942_change_protective_net_nature.xml +4 -0
  31. data/db/migrate/20160204122030_change_cover_implanter_abilities.xml +6 -0
  32. data/db/migrate/20160206163031_simplifies_activity_families.xml +135 -0
  33. data/db/migrate/20160207142352_fix_spelling_mistakes_on_associate_accounts.xml +7 -0
  34. data/db/migrate/20160210124624_fix_invalid_abilities_on_product_natures.xml +5 -0
  35. data/db/migrate/20160224234901_add_seedling_sorting.xml +4 -0
  36. data/db/migrate/20160310145038_add_rhodes_grass.xml +5 -0
  37. data/db/migrate/20160315152217_remove_exchange_natures.xml +4 -0
  38. data/db/migrate/20160402125244_add_some_heavy_equipment.xml +14 -0
  39. data/db/migrate/20160408223901_change_zea_variety.xml +14 -0
  40. data/db/migrate/20160410082230_add_chemical_classifications.xml +486 -0
  41. data/db/migrate/20160425100840_add_product_nature_corn_topper.xml +6 -0
  42. data/db/migrate/20160427115742_add_production_systems.xml +9 -0
  43. data/db/migrate/20160428085808_add_rows_count_indicator.xml +4 -0
  44. data/db/migrate/20160503131928_add_equipment_indicators.xml +34 -0
  45. data/db/migrate/20160503155351_fix_taxes.xml +15 -0
  46. data/db/migrate/20160504143733_add_units.xml +6 -0
  47. data/db/migrate/20160509100749_change_item_corn_topper.xml +4 -0
  48. data/db/migrate/20160516075201_add_plant_analysis.xml +4 -0
  49. data/db/migrate/20160520144901_update_plant_analysis.xml +5 -0
  50. data/db/migrate/20160525073342_update_spreader_trailer_product_nature.xml +4 -0
  51. data/db/migrate/20160526075245_update_spreader_trailer_abilities.xml +4 -0
  52. data/db/migrate/20160526092246_update_product_natures_and_variants_for_vegetables.xml +37 -0
  53. data/db/migrate/20160527090330_update_product_natures_seed.xml +5 -0
  54. data/db/migrate/20160531071525_add_procedure_actions.xml +6 -0
  55. data/db/migrate/20160531094927_add_items_for_pollination_pocedure.xml +9 -0
  56. data/db/migrate/20160605122001_add_worker_register.xml +4 -0
  57. data/db/migrate/20160608075549_update_spatial_reference_systems.xml +5437 -0
  58. data/db/migrate/20160608202134_add_missing_procedure_actions.xml +6 -0
  59. data/db/migrate/20160613154204_add_product_nature_variants_hoe_weeder.xml +6 -0
  60. data/db/migrate/20160615121350_add_pivots_in_nomenclature.xml +11 -0
  61. data/db/migrate/20160615122805_add_daily_average_working_time_indicator_and_lifespan_indicators.xml +6 -0
  62. data/db/migrate/20160617142409_add_items_for_hay_bales.xml +8 -0
  63. data/db/migrate/20160622121241_add_procedure_action_conservation_plowing.xml +4 -0
  64. data/db/migrate/20160624073237_add_musca_varieties.xml +5 -0
  65. data/db/migrate/20160627095008_add_weeding_kit.xml +5 -0
  66. data/db/migrate/20160630075849_update_product_nature_additive.xml +4 -0
  67. data/db/migrate/20160630124401_fix_coop_variants.xml +9 -0
  68. data/db/migrate/20160719094049_add_new_procedure_actions.xml +6 -0
  69. data/db/migrate/20160719100836_add_issue_nature.xml +4 -0
  70. data/db/migrate/20160720144130_update_equipment_varieties.xml +33 -0
  71. data/db/migrate/20160721124220_update_product_natures_varieties.xml +58 -0
  72. data/db/migrate/20160725182008_fix_abilities_on_old_equipment.xml +4 -0
  73. data/db/migrate/20160726173309_rename_equipments_to_equipment.xml +6 -0
  74. data/db/migrate/20160727092830_update_malus_pumila_variety_name.xml +4 -0
  75. data/db/migrate/20160802123639_update_unit_name_for_equipment_product_nature_variants.xml +39 -0
  76. data/db/migrate/20160816083232_add_issue_natures.xml +6 -0
  77. data/db/migrate/20160819082951_Add_plant_varieties.xml +10 -0
  78. data/db/migrate/20160829072846_add_procedure_action.xml +4 -0
  79. data/db/migrate/20160830153251_add_new_varieties.xml +6 -0
  80. data/db/migrate/20160909225801_add_missing_stock_variation_accounts.xml +17 -0
  81. data/db/migrate/20160911091901_update_product_nature_categories.xml +53 -0
  82. data/db/migrate/20160912182501_fix_product_nature_categories.xml +5 -0
  83. data/db/migrate/20160914114043_add_tool_maintaining_support.xml +7 -0
  84. data/db/migrate/20160915143142_add_pest_related_indicators.xml +5 -0
  85. data/db/migrate/20160919210501_add_transportation_actions.xml +4 -0
  86. data/db/migrate/20160928113101_add_missing_general_accounts.xml +108 -0
  87. data/db/migrate/20160930091201_add_missing_tax_natures.xml +17 -0
  88. data/db/migrate/20161017090516_fix_product_nature_categories_stock_movement_account.xml +5 -0
  89. data/db/migrate/20161019133613_add_ednotif_eu_country_code.xml +33 -0
  90. data/db/migrate/20161019145441_add_ednotif_animal_indicators.xml +22 -0
  91. data/db/migrate/20161022103401_add_portuguese_chart_of_account.xml +34 -0
  92. data/db/migrate/20161024115013_add_ednotif_indicators_to_cow_product_natures.xml +7 -0
  93. data/db/migrate/20161030174001_add_missing_cap_2016_varieties.xml +13 -0
  94. data/db/migrate/20161103111303_add_plants_sorting_action.xml +4 -0
  95. data/db/migrate/20161202083820_add_new_plant_varieties.xml +13 -0
  96. data/db/migrate/20161212181852_add_procedure_categories.xml +5 -0
  97. data/db/migrate/20161214093347_add_procedure_actions.xml +10 -0
  98. data/db/migrate/20161219171601_add_missing_vat_accounts.xml +4 -0
  99. data/db/migrate/20161219231801_add_outgoing_payment_report.xml +4 -0
  100. data/db/migrate/20170104161824_add_indicators_for_plant_analysis.xml +6 -0
  101. data/db/migrate/20170201180806_set_animal_group_population_counting_to_unitary.xml +23 -0
  102. data/db/migrate/20170203132125_update_momenclature_for_animal_housing_mulching_procedure.xml +7 -0
  103. data/db/migrate/20170206125311_update_nomenclature_for_animal_housing_disinfection_procedure.xml +5 -0
  104. data/db/migrate/20170206163322_update_disinfectant_product_variant.xml +4 -0
  105. data/db/migrate/20170214125925_clean_cow_indicators.xml +8 -0
  106. data/db/migrate/20170216100144_add_algerian_vat.xml +10 -0
  107. data/db/migrate/20170219210901_add_fixed_asset_registry.xml +4 -0
  108. data/db/migrate/20170220211447_add_procedure_category.xml +4 -0
  109. data/db/migrate/20170220222412_add_procedure_action.xml +4 -0
  110. data/db/migrate/20170221102405_add_milking_robot_in_nomenclature.xml +6 -0
  111. data/db/migrate/20170222113119_add_animal_varieties.xml +29 -0
  112. data/db/migrate/20170306154901_add_missing_outstanding_account.xml +4 -0
  113. data/db/migrate/20170308232701_add_fr_pcga_profit_and_loss.xml +4 -0
  114. data/db/migrate/20170312112401_add_fr_pcga_balance_sheet.xml +4 -0
  115. data/db/migrate/20170312172301_add_fr_pcg82_balance_sheet.xml +5 -0
  116. data/db/migrate/20170320073301_add_journal_entry_sheet.xml +8 -0
  117. data/db/migrate/20170320144338_add_sundry_debtors_and_creditors_usage.xml +4 -0
  118. data/db/migrate/20170322223101_add_loan_registry.xml +4 -0
  119. data/db/migrate/20170324084701_add_fixed_asset_sheet.xml +4 -0
  120. data/db/migrate/20170328144501_add_pnt_loan_accounts.xml +5 -0
  121. data/db/migrate/20170328165900_update_foliar_spray_product_nature.xml +4 -0
  122. data/db/migrate/20170328170000_add_indicator.xml +4 -0
  123. data/db/migrate/20170328170100_update_product_nature_seedling.xml +6 -0
  124. data/db/migrate/20170328170200_add_indicators.xml +4 -0
  125. data/db/migrate/20170328170300_add_indicators2.xml +4 -0
  126. data/db/migrate/20170328170400_update_seedling_product_natures.xml +4 -0
  127. data/db/migrate/20170405135101_add_account_journal_entry_sheet.xml +4 -0
  128. data/db/migrate/20170407214236_add_portuguese_account_config.xml +4 -0
  129. data/db/migrate/20170524094443_add_urtica_variety.xml +5 -0
  130. data/db/migrate/20170614130921_add_varieties.xml +117 -0
  131. data/db/migrate/20170627075545_add_varieties2.xml +37 -0
  132. data/db/migrate/20170627081533_add_pending_deposit_payments_account_number_for_pt_accounting_system.xml +4 -0
  133. data/db/migrate/20170718131724_add_issue_natures.xml +54 -0
  134. data/db/migrate/20170803164344_add_procedure_nature_and_category.xml +5 -0
  135. data/db/reference.xml +7904 -939
  136. data/lib/onoma/database.rb +77 -32
  137. data/lib/onoma/item.rb +74 -24
  138. data/lib/onoma/migration/actions/base.rb +1 -1
  139. data/lib/onoma/migration/actions/item_change.rb +5 -5
  140. data/lib/onoma/migration/actions/item_creation.rb +4 -4
  141. data/lib/onoma/migration/actions/item_merging.rb +5 -1
  142. data/lib/onoma/migration/actions/item_removal.rb +1 -1
  143. data/lib/onoma/migration/actions/nomenclature_change.rb +3 -3
  144. data/lib/onoma/migration/actions/nomenclature_creation.rb +4 -4
  145. data/lib/onoma/migration/actions/nomenclature_removal.rb +3 -3
  146. data/lib/onoma/migration/actions/property_creation.rb +2 -2
  147. data/lib/onoma/migration.rb +114 -3
  148. data/lib/onoma/nomenclature.rb +79 -49
  149. data/lib/onoma/property.rb +14 -14
  150. data/lib/onoma/reference.rb +1 -1
  151. data/lib/onoma/reflection.rb +8 -3
  152. data/lib/onoma/relation.rb +25 -3
  153. data/lib/onoma/version.rb +1 -1
  154. data/lib/onoma.rb +31 -62
  155. data/onoma.gemspec +15 -15
  156. metadata +123 -7
  157. data/lib/onoma/migration/base.rb +0 -45
  158. data/lib/onoma/migrator/reference.rb +0 -77
  159. data/lib/onoma/migrator/translation.rb +0 -30
  160. data/lib/onoma/migrator.rb +0 -3
@@ -1,11 +1,122 @@
1
- class ::Hash
1
+ require 'active_support/core_ext/string'
2
+ require 'active_support/core_ext/array'
3
+ require 'benchmark'
2
4
 
5
+ class ::Hash
3
6
  def simple_print
4
7
  map { |k, v| "#{k}: #{v.inspect}" }.join(', ')
5
8
  end
6
-
7
9
  end
8
10
 
11
+ module Onoma
12
+ # Migration instance represents a migration file
13
+ class Migration
14
+ class << self
15
+ # Parse en XML migration to a Onoma::Migration object
16
+ def parse(file)
17
+ # puts "Parse #{file}"
18
+ f = File.open(file, 'rb')
19
+ document = Nokogiri::XML(f) do |config|
20
+ config.strict.nonet.noblanks.noent
21
+ end
22
+ f.close
23
+ root = document.root
24
+ number = file.basename.to_s.split('_').first.to_i
25
+ new(number, root['name']) do |m|
26
+ root.children.each do |child|
27
+ next unless child.is_a? Nokogiri::XML::Element
28
+ m.add "Onoma::Migration::Actions::#{child.name.underscore.classify}".constantize.new(child)
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ attr_reader :number, :name
35
+
36
+ def initialize(number, name)
37
+ @number = number
38
+ @name = name
39
+ @actions = []
40
+ yield self if block_given?
41
+ end
42
+
43
+ def label
44
+ "#{number} #{name}"
45
+ end
46
+
47
+ def add(action)
48
+ @actions << action
49
+ end
50
+
51
+ def each_action(&block)
52
+ @actions.each(&block)
53
+ end
54
+
55
+ def inspect
56
+ "#<#{self.class.name}:#{format('%#x', object_id)} ##{number} #{name.inspect} (#{@actions.size} actions)>"
57
+ end
58
+
59
+ def migrate(conn)
60
+ puts ''
61
+ puts "== #{label}: migrating ".ljust(80, '=')
62
+ duration = Benchmark.measure do
63
+ each_action do |action|
64
+ puts "-- #{action.label}"
65
+ conn.exec_action(action)
66
+ end
67
+ conn.version = number
68
+ end
69
+ puts "== #{label}: migrated (#{duration.real.round(4)}s)".ljust(80, '=')
70
+ # puts "Write DB in #{Onoma.reference_path.relative_path_from(Onoma.root)}".yellow
71
+ if @steps
72
+ versions_dir = Onoma.root.join('tmp', 'versions')
73
+ FileUtils.mkdir_p(versions_dir)
74
+ conn.copy(versions_dir.join("#{number}.xml"))
75
+ end
76
+ end
77
+ end
78
+
79
+ # Migrator is a tool class to launch migrations
80
+ class Migrator
81
+ class << self
82
+ def migrate
83
+ Onoma::Migrator.new(Onoma.reference_path, Onoma.migrations_path).migrate
84
+ end
85
+ end
86
+
87
+ attr_accessor :database_path, :migrations_path
88
+
89
+ def initialize(database_path, migrations_path)
90
+ @database_path = database_path
91
+ @migrations_path = migrations_path
92
+ end
93
+
94
+ # Returns list of Onoma::Migration
95
+ def migrations
96
+ Dir.glob(@migrations_path.join('*.xml')).sort.collect do |f|
97
+ Onoma::Migration.parse(Pathname.new(f))
98
+ end
99
+ end
100
+
101
+ # Returns list of migrations since last done
102
+ def missing_migrations
103
+ last_version = connection.version
104
+ migrations.select do |m|
105
+ m.number > last_version
106
+ end
107
+ end
108
+
109
+ def migrate
110
+ missing_migrations.each do |migration|
111
+ migration.migrate(connection)
112
+ end
113
+ connection.write
114
+ end
115
+
116
+ def connection
117
+ @connection ||= Database.open(@database_path)
118
+ end
119
+ end
120
+ end
9
121
 
10
122
  require 'onoma/migration/actions'
11
- require 'onoma/migration/base'
@@ -6,7 +6,7 @@ module Onoma
6
6
  class Nomenclature
7
7
  attr_reader :properties, :items, :name, :roots
8
8
  attr_accessor :name, :notions, :translateable, :forest_right
9
- alias_method :property_natures, :properties
9
+ alias property_natures properties
10
10
 
11
11
  # Instanciate a new nomenclature
12
12
  def initialize(name, options = {})
@@ -24,15 +24,16 @@ module Onoma
24
24
  def harvest(element, options = {})
25
25
  notions = element.attr('notions').to_s.split(/\s*\,\s*/).map(&:to_sym)
26
26
  options[:notions] = notions if notions.any?
27
- options[:translateable] = !(element.attr('translateable').to_s == 'false')
27
+ options[:translateable] = element.attr('translateable').to_s != 'false'
28
28
  name = element.attr('name').to_s
29
29
  nomenclature = new(name, options)
30
- for property in element.xpath('xmlns:properties/xmlns:property')
30
+ element.xpath('xmlns:properties/xmlns:property').each do |property|
31
31
  nomenclature.harvest_property(property)
32
32
  end
33
- for item in element.xpath('xmlns:items/xmlns:item')
33
+ element.xpath('xmlns:items/xmlns:item').each do |item|
34
34
  nomenclature.harvest_item(item)
35
35
  end
36
+ nomenclature.list.each(&:fetch_parent)
36
37
  nomenclature.rebuild_tree!
37
38
  nomenclature
38
39
  end
@@ -96,7 +97,7 @@ module Onoma
96
97
  name = element.attr('name').to_s
97
98
  parent = attributes[:parent] || (element.key?('parent') ? element['parent'] : nil)
98
99
  attributes = element.attributes.each_with_object(HashWithIndifferentAccess.new) do |(k, v), h|
99
- next if %w(name parent).include?(k)
100
+ next if %w[name parent].include?(k)
100
101
  h[k] = cast_property(k, v.to_s)
101
102
  end
102
103
  attributes[:parent] = parent if parent
@@ -115,7 +116,7 @@ module Onoma
115
116
  if element.has_attribute?('default')
116
117
  options[:default] = element.attr('default').to_sym
117
118
  end
118
- options[:required] = !!(element.attr('required').to_s == 'true')
119
+ options[:required] = !!(element.attr('required').to_s != 'true')
119
120
  # options[:inherit] = !!(element.attr('inherit').to_s == 'true')
120
121
  if type == :list
121
122
  type = element.has_attribute?('nomenclature') ? :item_list : :choice_list
@@ -134,11 +135,11 @@ module Onoma
134
135
  elsif element.has_attribute?('nomenclature')
135
136
  options[:choices] = element.attr('nomenclature').to_s.strip.to_sym
136
137
  else
137
- fail MissingChoices, "[#{@name}] Property #{name} must have nomenclature as choices"
138
+ raise MissingChoices, "[#{@name}] Property #{name} must have nomenclature as choices"
138
139
  end
139
140
  end
140
141
  unless Property::TYPES.include?(type)
141
- fail ArgumentError, "Property #{name} type is unknown: #{type.inspect}"
142
+ raise ArgumentError, "Property #{name} type is unknown: #{type.inspect}"
142
143
  end
143
144
  add_property(name, type, options)
144
145
  end
@@ -147,7 +148,7 @@ module Onoma
147
148
  def add_item(name, attributes = {}, options = {})
148
149
  i = Item.new(self, name, attributes)
149
150
  if @items[i.name]
150
- fail "Item #{i.name} is already defined in nomenclature #{@name}"
151
+ raise "Item #{i.name} is already defined in nomenclature #{@name}"
151
152
  end
152
153
  @items[i.name] = i
153
154
  @roots << i unless i.parent?
@@ -162,7 +163,7 @@ module Onoma
162
163
  new_parent = changes[:parent]
163
164
  new_name = changes[:name]
164
165
  changes.each do |k, v|
165
- next if [:parent, :name].include? k
166
+ next if %i[parent name].include? k
166
167
  i.set(k, v)
167
168
  end
168
169
  if has_parent
@@ -176,7 +177,7 @@ module Onoma
176
177
 
177
178
  def rename_item(name, new_name)
178
179
  if @items[new_name]
179
- fail "Item #{new_name} is already defined in nomenclature #{@name}. Use merging instead."
180
+ raise "Item #{new_name} is already defined in nomenclature #{@name}. Use merging instead."
180
181
  end
181
182
  i = find!(name)
182
183
  i.children.each do |child|
@@ -232,7 +233,7 @@ module Onoma
232
233
  def add_property(name, type, options = {})
233
234
  p = Property.new(self, name, type, options)
234
235
  if @properties[p.name]
235
- fail "Property #{p.name} is already defined in nomenclature #{@name}"
236
+ raise "Property #{p.name} is already defined in nomenclature #{@name}"
236
237
  end
237
238
  @properties[p.name] = p
238
239
  @references = nil
@@ -245,32 +246,32 @@ module Onoma
245
246
 
246
247
  def check!
247
248
  # Check properties
248
- for property in @properties.values
249
+ @properties.values.each do |property|
249
250
  if property.choices_nomenclature && !property.inline_choices? && !Onoma[property.choices_nomenclature.to_s]
250
- fail InvalidProperty, "[#{name}] #{property.name} nomenclature property must refer to an existing nomenclature. Got #{property.choices_nomenclature.inspect}. Expecting: #{Onoma.names.inspect}"
251
+ raise InvalidProperty, "[#{name}] #{property.name} nomenclature property must refer to an existing nomenclature. Got #{property.choices_nomenclature.inspect}. Expecting: #{Onoma.names.inspect}"
251
252
  end
252
253
  next unless property.type == :choice && property.default
253
254
  unless property.choices.include?(property.default)
254
- fail InvalidProperty, "The default choice #{property.default.inspect} is invalid (in #{name}##{property.name}). Pick one from #{property.choices.sort.inspect}."
255
+ raise InvalidProperty, "The default choice #{property.default.inspect} is invalid (in #{name}##{property.name}). Pick one from #{property.choices.sort.inspect}."
255
256
  end
256
257
  end
257
258
 
258
259
  # Check items
259
- for item in list
260
- for property in @properties.values
260
+ list.each do |item|
261
+ @properties.values.each do |property|
261
262
  choices = property.choices
262
263
  if item.property(property.name) && property.type == :choice
263
264
  # Cleans for parametric reference
264
265
  name = item.property(property.name).to_s.split(/\(/).first.to_sym
265
266
  unless choices.include?(name)
266
- fail InvalidProperty, "The given choice #{name.inspect} is invalid (in #{self.name}##{item.name}). Pick one from #{choices.sort.inspect}."
267
+ raise InvalidProperty, "The given choice #{name.inspect} is invalid (in #{self.name}##{item.name}). Pick one from #{choices.sort.inspect}."
267
268
  end
268
269
  elsif item.property(property.name) && property.type == :list && property.choices_nomenclature
269
270
  for name in item.property(property.name) || []
270
271
  # Cleans for parametric reference
271
272
  name = name.to_s.split(/\(/).first.to_sym
272
273
  unless choices.include?(name)
273
- fail InvalidProperty, "The given choice #{name.inspect} is invalid (in #{self.name}##{item.name}). Pick one from #{choices.sort.inspect}."
274
+ raise InvalidProperty, "The given choice #{name.inspect} is invalid (in #{self.name}##{item.name}). Pick one from #{choices.sort.inspect}."
274
275
  end
275
276
  end
276
277
  end
@@ -282,7 +283,11 @@ module Onoma
282
283
  end
283
284
 
284
285
  def inspect
285
- "Onoma::#{name.to_s.classify}"
286
+ "Nomen::#{name.to_s.classify}"
287
+ end
288
+
289
+ def table_name
290
+ @name.to_s.pluralize
286
291
  end
287
292
 
288
293
  # Returns hash with items in tree: {a => nil, b => {c => nil}}
@@ -303,7 +308,7 @@ module Onoma
303
308
  def human_name(options = {})
304
309
  "nomenclatures.#{name}.name".t(options.merge(default: ["labels.#{name}".to_sym, name.to_s.humanize]))
305
310
  end
306
- alias_method :humanize, :human_name
311
+ alias humanize human_name
307
312
 
308
313
  def new_boundaries(count = 2)
309
314
  boundaries = []
@@ -326,12 +331,12 @@ module Onoma
326
331
  # List all item names. Can filter on a given item name and its children
327
332
  def to_a(item_name = nil)
328
333
  if item_name.present? && @items[item_name]
329
- return @items[item_name].self_and_children.map(&:name)
334
+ @items[item_name].self_and_children.map(&:name)
330
335
  else
331
- return @items.keys.sort
336
+ @items.keys.sort
332
337
  end
333
338
  end
334
- alias_method :all, :to_a
339
+ alias all to_a
335
340
 
336
341
  def <=>(other)
337
342
  name <=> other.name
@@ -351,7 +356,7 @@ module Onoma
351
356
 
352
357
  # Returns a list for select as an array of pair (array)
353
358
  def selection(item_name = nil)
354
- items = (item_name ? @items[item_name].self_and_children : @items.values)
359
+ items = (item_name ? find!(item_name).self_and_children : @items.values)
355
360
  items.collect do |item|
356
361
  [item.human_name, item.name.to_s]
357
362
  end.sort do |a, b|
@@ -361,21 +366,27 @@ module Onoma
361
366
 
362
367
  # Returns a list for select as an array of pair (hash)
363
368
  def selection_hash(item_name = nil)
364
- @items[item_name].self_and_children.map do |item|
369
+ items = (item_name ? find!(item_name).self_and_children : @items.values)
370
+ items.collect do |item|
365
371
  { label: item.human_name, value: item.name }
366
372
  end.sort { |a, b| a[:label].lower_ascii <=> b[:label].lower_ascii }
367
373
  end
368
374
 
369
375
  # Returns a list for select, without specified items
370
376
  def select_without(already_imported)
371
- selection = @items.values.collect do |item|
377
+ ActiveSupport::Deprecation.warn 'Nomen::Nomenclature#select_without method is deprecated. Please use Nomen::Nomenclature#without method instead.'
378
+ select_options = @items.values.collect do |item|
372
379
  [item.human_name, item.name.to_s] unless already_imported[item.name.to_s]
373
380
  end
374
- selection.compact!
375
- selection.sort! do |a, b|
381
+ select_options.compact!
382
+ select_options.sort! do |a, b|
376
383
  a.first <=> b.first
377
384
  end
378
- selection
385
+ select_options
386
+ end
387
+
388
+ def degree_of_kinship(a, b)
389
+ a.degree_of_kinship_with(b)
379
390
  end
380
391
 
381
392
  # Return first item name
@@ -388,26 +399,36 @@ module Onoma
388
399
  first(item_name)
389
400
  end
390
401
 
391
- # Return the Item for the given name
402
+ # Return the Item for the given name. Returns nil if no item found
392
403
  def find(item_name)
393
404
  @items[item_name]
394
405
  end
395
- alias_method :item, :find
396
-
397
- def property(property_name)
398
- @properties[property_name]
399
- end
406
+ alias item find
400
407
 
408
+ # Return the Item for the given name. Raises Nomen::ItemNotFound if no item
409
+ # found in nomenclature
401
410
  def find!(item_name)
402
- unless i = @items[item_name]
403
- fail "Cannot find item #{item_name} in #{name}"
404
- end
411
+ i = find(item_name)
412
+ raise ItemNotFound, "Cannot find item #{item_name.inspect} in #{name}" unless i
405
413
  i
406
414
  end
407
415
 
416
+ # Returns +true+ if an item exists in the nomenclature that matches the
417
+ # name, or +false+ otherwise. The argument can take two forms:
418
+ # * String/Symbol - Find an item with this primary name
419
+ # * Nomen::Item - Find an item with the same name of the item
420
+ def exists?(item)
421
+ @items[item.respond_to?(:name) ? item.name : item].present?
422
+ end
423
+ alias include? exists?
424
+
425
+ def property(property_name)
426
+ @properties[property_name]
427
+ end
428
+
408
429
  # Returns list of items as an Array
409
430
  def list
410
- Onoma::Relation.new(@items.values)
431
+ Onoma::Relation.new(self, @items.values)
411
432
  end
412
433
 
413
434
  # Iterates on items
@@ -441,6 +462,13 @@ module Onoma
441
462
  end
442
463
  end
443
464
 
465
+ def without(*names)
466
+ excluded = names.flatten.compact.map(&:to_sym)
467
+ list.reject do |item|
468
+ excluded.include?(item.name)
469
+ end
470
+ end
471
+
444
472
  def find_by(properties)
445
473
  items = where(properties)
446
474
  return nil unless items.any?
@@ -468,11 +496,11 @@ module Onoma
468
496
  def cast_options(options)
469
497
  return {} if options.nil?
470
498
  hash = options.each_with_object({}) do |(k, v), h|
471
- if properties[k]
472
- h[k.to_sym] = cast_property(k, v.to_s)
473
- else
474
- h[k.to_sym] = v
475
- end
499
+ h[k.to_sym] = if properties[k]
500
+ cast_property(k, v.to_s)
501
+ else
502
+ v
503
+ end
476
504
  end
477
505
  end
478
506
 
@@ -481,7 +509,7 @@ module Onoma
481
509
  if property = properties[name]
482
510
  if property.type == :choice || property.type == :item
483
511
  if value =~ /\,/
484
- fail InvalidProperty, 'A property nature of choice type cannot contain commas'
512
+ raise InvalidProperty, 'A property nature of choice type cannot contain commas'
485
513
  end
486
514
  value = value.strip.to_sym
487
515
  elsif property.list?
@@ -492,14 +520,16 @@ module Onoma
492
520
  value = value.to_d
493
521
  elsif property.type == :integer
494
522
  value = value.to_i
523
+ elsif property.type == :date
524
+ value = (value.blank? ? nil : value.to_date)
495
525
  elsif property.type == :symbol
496
526
  unless value =~ /\A\w+\z/
497
- fail InvalidProperty, "A property '#{name}' must contains a symbol. /[a-z0-9_]/ accepted. No spaces. Got #{value.inspect}"
527
+ raise InvalidProperty, "A property '#{name}' must contains a symbol. /[a-z0-9_]/ accepted. No spaces. Got #{value.inspect}"
498
528
  end
499
529
  value = value.to_sym
500
530
  end
501
- elsif !%w(name parent aliases).include?(name.to_s)
502
- fail ArgumentError, "Undefined property '#{name}' in #{@name}"
531
+ elsif !%w[name parent aliases].include?(name.to_s)
532
+ raise ArgumentError, "Undefined property '#{name}' in #{@name}"
503
533
  end
504
534
  value
505
535
  end
@@ -1,8 +1,8 @@
1
1
  module Onoma
2
2
  class Property
3
- TYPES = [:boolean, :item, :item_list, :choice, :choice_list, :string_list,
4
- :date, :decimal, :integer, :nomenclature, :string, :symbol]
5
-
3
+ TYPES = %i[boolean item item_list choice choice_list string_list
4
+ date decimal integer nomenclature string symbol].freeze
5
+
6
6
  attr_reader :nomenclature, :name, :type, :fallbacks, :default, :source
7
7
 
8
8
  # New item
@@ -10,7 +10,7 @@ module Onoma
10
10
  @nomenclature = nomenclature
11
11
  @name = name.to_sym
12
12
  @type = type
13
- fail "Invalid type: #{@type.inspect}" unless TYPES.include?(@type)
13
+ raise "Invalid type: #{@type.inspect}" unless TYPES.include?(@type)
14
14
  @fallbacks = options[:fallbacks] if options[:fallbacks]
15
15
  @default = options[:default] if options[:default]
16
16
  @required = !!options[:required]
@@ -28,11 +28,11 @@ module Onoma
28
28
  attrs[:name] = @name.to_s
29
29
  attrs[:type] = @type.to_s
30
30
  if @source
31
- if inline_choices?
32
- attrs[:choices] = @source.join(', ')
33
- else
34
- attrs[:choices] = @source.to_s
35
- end
31
+ attrs[:choices] = if inline_choices?
32
+ @source.join(', ')
33
+ else
34
+ @source.to_s
35
+ end
36
36
  end
37
37
  attrs[:required] = 'true' if @required
38
38
  attrs[:fallbacks] = @fallbacks.join(', ') if @fallbacks
@@ -68,19 +68,19 @@ module Onoma
68
68
  # Returns list of choices for a given property
69
69
  def choices
70
70
  if inline_choices?
71
- return @source || []
71
+ @source || []
72
72
  elsif item_reference?
73
- return @nomenclature.sibling(@source).all.map(&:to_sym)
73
+ @nomenclature.sibling(@source).all.map(&:to_sym)
74
74
  end
75
75
  end
76
76
 
77
77
  def selection
78
78
  if inline_choices?
79
- return choices.collect do |c|
79
+ choices.collect do |c|
80
80
  [c, c]
81
81
  end
82
82
  elsif item_reference?
83
- return @nomenclature.sibling(@source).selection
83
+ @nomenclature.sibling(@source).selection
84
84
  end
85
85
  end
86
86
 
@@ -89,7 +89,7 @@ module Onoma
89
89
  "nomenclatures.#{nomenclature.name}.properties.#{name}".t(default: ["nomenclatures.#{nomenclature.name}.properties.#{name}".to_sym, "properties.#{name}".to_sym, "enumerize.#{nomenclature.name}.#{name}".to_sym, "labels.#{name}".to_sym, name.humanize])
90
90
  end
91
91
 
92
- alias_method :humanize, :human_name
92
+ alias humanize human_name
93
93
 
94
94
  def <=>(other)
95
95
  name <=> other.name
@@ -7,7 +7,7 @@ module Onoma
7
7
  @type = type
8
8
  @property = property
9
9
  @foreign_nomenclature = foreign_nomenclature
10
- fail "Invalid nomenclature: #{@foreign_nomenclature.inspect}" unless @foreign_nomenclature.is_a?(Nomenclature)
10
+ raise "Invalid nomenclature: #{@foreign_nomenclature.inspect}" unless @foreign_nomenclature.is_a?(Nomenclature)
11
11
  end
12
12
 
13
13
  def nomenclature
@@ -2,24 +2,29 @@ module Onoma
2
2
  class Reflection
3
3
  attr_reader :active_record, :name, :class_name, :foreign_key, :scope, :options, :nomenclature, :klass
4
4
 
5
+ # name: things
6
+ # class_name: Thing
7
+ # klass: Nomen::Thing
5
8
  def initialize(active_record, name, options = {})
6
9
  @options = options
7
10
  @name = name.to_s
8
11
  @active_record = active_record
9
12
  @class_name = options[:class_name] || name.to_s.classify
10
- @foreign_key = options[:foreign_key] || name
13
+ @foreign_key = (options[:foreign_key] || name).to_s
11
14
  @scope = options[:scope]
12
15
  @nomenclature = class_name.underscore.pluralize
13
16
  @klass = Onoma.find(@nomenclature)
14
17
  end
15
18
 
16
- alias_method :model, :active_record
19
+ alias model active_record
17
20
 
18
21
  def macro
19
22
  :belongs_to
20
23
  end
21
24
 
22
- # Returns true if self and other_aggregation have the same name attribute, active_record attribute, and other_aggregation has an options hash assigned to it.
25
+ # Returns true if self and other_aggregation have the same name attribute,
26
+ # active_record attribute, and other_aggregation has an options hash
27
+ # assigned to it.
23
28
  def ==(other_aggregation)
24
29
  other_aggregation.is_a?(self.class) &&
25
30
  name == other_aggregation.name &&
@@ -1,9 +1,31 @@
1
1
  module Onoma
2
2
  class Relation < Array
3
- alias_method :find_each, :each
3
+ attr_reader :nomenclature
4
4
 
5
- def select(*args, &block)
6
- self.class.new(super(*args, &block))
5
+ alias find_each each
6
+
7
+ def initialize(nomenclature, *args)
8
+ super(*args)
9
+ @nomenclature = nomenclature
10
+ end
11
+
12
+ %w[drop drop_while select reject reverse slice_after slice_before slice_when sort].each do |meth|
13
+ define_method meth do |*args, &block|
14
+ self.class.new(@nomenclature, super(*args, &block))
15
+ end
16
+ end
17
+
18
+ def selection(sorted = true)
19
+ unit_selec = collect do |item|
20
+ [item.human_name, item.name.to_s]
21
+ end
22
+ if sorted
23
+ unit_selec.sort! do |a, b|
24
+ a.first.lower_ascii <=> b.first.lower_ascii
25
+ end
26
+ end
27
+
28
+ unit_selec
7
29
  end
8
30
  end
9
31
  end
data/lib/onoma/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Onoma
2
- VERSION = "0.0.0"
2
+ VERSION = '0.1.0'.freeze
3
3
  end