Class: EfoNelfo::PostType

Inherits:
Object
  • Object
show all
Includes:
HasMany, Properties
Defined in:
lib/efo_nelfo/post_type.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasMany

#add, #associations, #find_association, #has_association?, included, #to_a

Methods included from Properties

#has_property?, included, #initialize_attributes, #properties, #to_a

Constructor Details

#initialize(*args) ⇒ PostType

Returns a new instance of PostType.



44
45
46
47
48
49
50
# File 'lib/efo_nelfo/post_type.rb', line 44

def initialize(*args)
  if args && args.first.is_a?(Array)
    initialize_attributes Hash[properties.keys.zip(args.first)]
  else
    initialize_attributes(*args)
  end
end

Class Method Details

.for(type, version) ⇒ Object



9
10
11
12
# File 'lib/efo_nelfo/post_type.rb', line 9

def for(type, version)
  klass = "EfoNelfo::V#{version_to_namespace(version)}::#{type}"
  const_get(klass) rescue nil
end

.from(hash) ⇒ Object



34
35
36
# File 'lib/efo_nelfo/post_type.rb', line 34

def from(hash)
  self.for(hash[:post_type], hash[:version]).new(hash)
end

.parse(csv) ⇒ Object



38
39
40
# File 'lib/efo_nelfo/post_type.rb', line 38

def parse(csv)
  new EfoNelfo::Reader::CSV.new(data: csv).first
end

.post_typeObject



30
31
32
# File 'lib/efo_nelfo/post_type.rb', line 30

def post_type
  name.split(/::/).last
end

.versionObject

Extracts version number from class namespace. Example: EfoNelfo::V41::Some::Class.version # => “4.1”



22
23
24
# File 'lib/efo_nelfo/post_type.rb', line 22

def version
  (version_from_class.to_f / 10).to_s
end

.version_from_classObject



26
27
28
# File 'lib/efo_nelfo/post_type.rb', line 26

def version_from_class
  self.to_s.match(/::V(?<version>\d+)::/)[:version]
end

.version_to_namespace(version) ⇒ Object

Converts version to module version name Example: version_to_namespace(“4.2”) # => “42”



16
17
18
# File 'lib/efo_nelfo/post_type.rb', line 16

def version_to_namespace(version)
  version.gsub('.', '')
end

Instance Method Details

#post_typeObject



52
53
54
# File 'lib/efo_nelfo/post_type.rb', line 52

def post_type
  self.class.post_type
end

#to_csvObject



60
61
62
63
64
65
66
# File 'lib/efo_nelfo/post_type.rb', line 60

def to_csv
  CSV.generate EfoNelfo::Reader::CSV::CSV_OPTIONS do |csv|
    to_a.each do |row|
      csv << row unless row.empty?
    end
  end
end

#versionObject



56
57
58
# File 'lib/efo_nelfo/post_type.rb', line 56

def version
  self.class.version
end