Class: Kinbote::Attribute

Inherits:
Object
  • Object
show all
Includes:
Kinfile, Util
Defined in:
lib/kinbote/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Kinfile

#create_file, #create_files, #delete_file, #delete_files, #file_size, #files, #has_file?, #init_files, #reset_file_size

Methods included from Util

#dir_from_file, #file_without_extension, #output_path, #print_state, #slug_from_file, #slugify, #sorted_attributes, #type_from_file, #varify, #view_path

Constructor Details

#initialize(name, page, values) ⇒ Attribute

Returns a new instance of Attribute.



8
9
10
11
12
13
# File 'lib/kinbote/attribute.rb', line 8

def initialize(name, page, values)
  @name = name.to_s
  @pages = []
  @values = []
  add_page(page, values)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/kinbote/attribute.rb', line 6

def name
  @name
end

#pagesObject (readonly)

Returns the value of attribute pages.



6
7
8
# File 'lib/kinbote/attribute.rb', line 6

def pages
  @pages
end

Instance Method Details

#add_page(page, values) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/kinbote/attribute.rb', line 15

def add_page(page, values)
  @pages.delete_if{|p| p.slug == page.slug}
  @pages << page
  values.each do |v|
    val = $site.find_value(v, self)
    val ? val.add_page(page) : @values << Value.new(v, page, self)
  end
end

#has_value?(v, page = nil) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/kinbote/attribute.rb', line 45

def has_value?(v, page=nil)
  values_for(page).map{|vv| vv.value}.include?(v)
end

#rem_page(page) ⇒ Object



24
25
26
27
28
# File 'lib/kinbote/attribute.rb', line 24

def rem_page(page)
  values_for(page).map{|value| value.rem_page(page)}
  values_for(page).delete_if{|v| v.pages.size == 0}
  @pages.delete_if{|p| p.slug == page.slug}
end

#to_sObject



50
# File 'lib/kinbote/attribute.rb', line 50

def to_s; @name; end

#valuesObject



30
31
32
33
# File 'lib/kinbote/attribute.rb', line 30

def values
  @values.sort!{|x, y| x.value <=> y.value}
  @values
end

#values_for(page = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/kinbote/attribute.rb', line 35

def values_for(page=nil)
  return @values if !page
  slug = (page.is_a?(String) ? page : page.slug)
  vals = []
  @values.each do |v|
    vals << v if v.has_page?(slug)
  end
  vals.sort{|x, y| x.value <=> y.value}
end

#varnameObject



49
# File 'lib/kinbote/attribute.rb', line 49

def varname; varify(@name); end