Class: Reddy::Literal

Inherits:
Object
  • Object
show all
Defined in:
lib/reddy/literal.rb

Defined Under Namespace

Classes: Encoding, Language

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents, encoding) ⇒ Literal

Returns a new instance of Literal.



127
128
129
130
131
132
133
# File 'lib/reddy/literal.rb', line 127

def initialize(contents, encoding)
  @contents = contents.to_s
  unless encoding.is_a?(Encoding) || encoding.is_a?(Encoding::Null)
    raise TypeError, "#{encoding.inspect} should be an instance of Encoding"
  end
  @encoding = encoding
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



126
127
128
# File 'lib/reddy/literal.rb', line 126

def contents
  @contents
end

#encodingObject

Returns the value of attribute encoding.



126
127
128
# File 'lib/reddy/literal.rb', line 126

def encoding
  @encoding
end

Class Method Details

.build_from(object) ⇒ Object



143
144
145
# File 'lib/reddy/literal.rb', line 143

def self.build_from(object)
  new(object.to_s, infer_encoding_for(object))
end

.build_from_language(object) ⇒ Object



173
174
175
# File 'lib/reddy/literal.rb', line 173

def self.build_from_language(object)
  new(object.to_s, infer_language_for(object))
end

.infer_encoding_for(object) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/reddy/literal.rb', line 147

def self.infer_encoding_for(object)
  case object
  when Integer; Encoding.integer
  when Float;   Encoding.float
  else          Encoding.string
  end
end

.infer_language_for(object) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/reddy/literal.rb', line 157

def self.infer_language_for(object)
  inferred_lang = object.language
  case inferred_lang
  when :dutch; Language.new("nl")
  when :english; Language.new("en")
  when :farsi; Langauge.new("fa")
  when :french; Language.new("fr")
  when :german; Language.new("de")
  when :pinyin; Language.new("zh-CN")
  when :portugese; Language.new("pt")
  when :russian; Language.new("ru")
  when :spanish; Language.new("es")
  when :swedish; Language.new("sv")
  end
end

.typed(contents, encoding) ⇒ Object



139
140
141
# File 'lib/reddy/literal.rb', line 139

def self.typed(contents, encoding)
  new(contents, Encoding.coerce(encoding))
end

.untyped(contents, language = nil) ⇒ Object



135
136
137
# File 'lib/reddy/literal.rb', line 135

def self.untyped(contents, language = nil)
  new(contents, Language.coerce(language))
end

Instance Method Details

#==(obj) ⇒ Object



182
183
184
# File 'lib/reddy/literal.rb', line 182

def == (obj)
  obj.is_a?(self.class) && obj.contents == @contents && obj.encoding == @encoding
end

#langObject



207
208
209
# File 'lib/reddy/literal.rb', line 207

def lang
  encoding.is_a?(Language) ? encoding : nil
end

#to_n3Object



186
187
188
# File 'lib/reddy/literal.rb', line 186

def to_n3
  encoding.format_as_n3(@contents)
end

#to_ntriplesObject

alias_method breaks subclasses! Beware! Here be dragons!



191
192
193
# File 'lib/reddy/literal.rb', line 191

def to_ntriples
  to_n3
end

#to_sObject



203
204
205
# File 'lib/reddy/literal.rb', line 203

def to_s
  @contents.to_s
end

#to_trixObject



195
196
197
# File 'lib/reddy/literal.rb', line 195

def to_trix
  encoding.format_as_trix(@contents)
end

#xmlliteral?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/reddy/literal.rb', line 199

def xmlliteral?
  encoding.xmlliteral?
end