Class: Onoma::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/onoma/property.rb

Constant Summary collapse

TYPES =
%i[boolean item item_list choice choice_list string_list
date decimal integer nomenclature string symbol].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nomenclature, name, type, options = {}) ⇒ Property

New item



9
10
11
12
13
14
15
16
17
18
# File 'lib/onoma/property.rb', line 9

def initialize(nomenclature, name, type, options = {})
  @nomenclature = nomenclature
  @name = name.to_sym
  @type = type
  raise "Invalid type: #{@type.inspect}" unless TYPES.include?(@type)
  @fallbacks = options[:fallbacks] if options[:fallbacks]
  @default = options[:default] if options[:default]
  @required = !!options[:required]
  @source = options[:choices] if reference? && options[:choices]
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



6
7
8
# File 'lib/onoma/property.rb', line 6

def default
  @default
end

#fallbacksObject (readonly)

Returns the value of attribute fallbacks.



6
7
8
# File 'lib/onoma/property.rb', line 6

def fallbacks
  @fallbacks
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/onoma/property.rb', line 6

def name
  @name
end

#nomenclatureObject (readonly)

Returns the value of attribute nomenclature.



6
7
8
# File 'lib/onoma/property.rb', line 6

def nomenclature
  @nomenclature
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/onoma/property.rb', line 6

def source
  @source
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/onoma/property.rb', line 6

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object



94
95
96
# File 'lib/onoma/property.rb', line 94

def <=>(other)
  name <=> other.name
end

#choicesObject

Returns list of choices for a given property



69
70
71
72
73
74
75
# File 'lib/onoma/property.rb', line 69

def choices
  if inline_choices?
    @source || []
  elsif item_reference?
    @nomenclature.sibling(@source).all.map(&:to_sym)
  end
end

#choices_nomenclatureObject



64
65
66
# File 'lib/onoma/property.rb', line 64

def choices_nomenclature
  @source
end

#human_nameObject Also known as: humanize

Return human name of property



88
89
90
# File 'lib/onoma/property.rb', line 88

def human_name
  "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])
end

#inline_choices?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/onoma/property.rb', line 48

def inline_choices?
  choice? || choice_list?
end

#item_reference?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/onoma/property.rb', line 52

def item_reference?
  item? || item_list?
end

#list?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/onoma/property.rb', line 60

def list?
  choice_list? || item_list? || string_list?
end

#reference?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/onoma/property.rb', line 56

def reference?
  choice_list? || item_list? || string_list? || choice? || item?
end

#required?Boolean

Returns if property is required

Returns:

  • (Boolean)


44
45
46
# File 'lib/onoma/property.rb', line 44

def required?
  @required
end

#selectionObject



77
78
79
80
81
82
83
84
85
# File 'lib/onoma/property.rb', line 77

def selection
  if inline_choices?
    choices.collect do |c|
      [c, c]
    end
  elsif item_reference?
    @nomenclature.sibling(@source).selection
  end
end

#to_xml_attrsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/onoma/property.rb', line 26

def to_xml_attrs
  attrs = {}
  attrs[:name] = @name.to_s
  attrs[:type] = @type.to_s
  if @source
    attrs[:choices] = if inline_choices?
                        @source.join(', ')
                      else
                        @source.to_s
                      end
  end
  attrs[:required] = 'true' if @required
  attrs[:fallbacks] = @fallbacks.join(', ') if @fallbacks
  attrs[:default] = @default.to_s if @default
  attrs
end