Class: Bliss::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/bliss/format.rb

Constant Summary collapse

@@keywords =
%w{ tag_name_required content_required tag_name_type content_type tag_name_format content_format tag_name_values content_values  }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ Format

Returns a new instance of Format.



7
8
9
# File 'lib/bliss/format.rb', line 7

def initialize(filepath)
  self.specifications = YAML.load_file(filepath)
end

Class Method Details

.settings_to_constraints(depth, settings) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bliss/format.rb', line 54

def self.settings_to_constraints(depth, settings)
  # TODO perhaps the Constraint model should handle this
  # e.g., constraint.add_depth (as array)
  # then internally it creates xpath-like depth

  current_constraints = []
  depth_name = nil
  content_values = nil
  #puts "#{depth.join('/')}: #{settings.inspect}"
  settings.each_pair { |setting, value|
    case setting
      when "tag_name_required"
        if value == true
          depth_name ||= depth.join('/')
          current_constraints.push(Bliss::Constraint.new(depth_name, :tag_name_required))
        end
      when "tag_name_values"
        depth_name = depth[0..-2].join('/')
        depth_name << "/" if depth_name.size > 0
        depth_name << "(#{value.join('|')})" # TODO esto funciona solo en el ultimo step del depth :/
      when "content_values"
        current_constraints.push(Bliss::Constraint.new(depth_name, :content_values, {:possible_values => value}))
    end
  }
  current_constraints.each {|cc|
    cc.depth = depth_name
  }
  current_constraints
end

Instance Method Details

#constraintsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bliss/format.rb', line 21

def constraints
  return [] if not (@specs.is_a? Hash and @specs.size > 0)
  return @constraints if @constraints

  @constraints = []

  @specs.recurse(true) do |depth, value|
    if value.is_a? Hash and !@@keywords.include?(depth.last)
      settings = value.select { |key| @@keywords.include?(key) }
    end
    #settings = @specs.value_at_chain(depth).select{|key| @@keywords.include?(key) }
    if settings.is_a? Hash and !@@keywords.include?(depth.last)
      settings.merge!({"tag_name_required" => true}) if not settings.has_key?("tag_name_required")

      # TODO this is an ugly way to move tag_name_values to the end!
      settings.store('tag_name_values', settings.delete('tag_name_values')) if settings.has_key?('tag_name_values')
      settings.store('content_values', settings.delete('content_values')) if settings.has_key?('content_values')

      #puts settings.inspect

      #depth_name = nil
      #setting_to_constraints(depth, settings).each { |cc|
        #cc.depth = depth_name
      #  @constraints.push(cc) #Bliss::Constraint.new(depth_name, cc.setting))
      #}
      @constraints.concat(Bliss::Format.settings_to_constraints(depth, settings))

    end
  end

  return @constraints
end

#detailsObject



113
114
115
# File 'lib/bliss/format.rb', line 113

def details
  @constraints.collect(&:detail)
end

#error_detailsObject



117
118
119
# File 'lib/bliss/format.rb', line 117

def error_details
  @constraints.select {|c| c.state == :not_passed }.collect(&:detail)
end

#keywordsObject

TODO for debugging only!



12
13
14
# File 'lib/bliss/format.rb', line 12

def keywords
  @@keywords
end

#specifications=(specs = {}) ⇒ Object Also known as: specs=



16
17
18
# File 'lib/bliss/format.rb', line 16

def specifications=(specs={})
  @specs = specs.dup
end

#to_check_constraintsObject

constraint set model? constraints.valid.with_depth([‘root’, ‘ads’]) ???



103
104
105
106
107
108
109
110
111
# File 'lib/bliss/format.rb', line 103

def to_check_constraints
  # raise error if not depth.is_a? Array
  begin
    to_check_constraints = constraints.select {|c| [:not_checked, :passed].include?(c.state) }
    to_check_constraints
  rescue
    []
  end
end